zoukankan      html  css  js  c++  java
  • 一周试用yii开发一个带各种该有功能的web程序(二)

      上篇随笔写完的是yii能使用简单的命令创建出一个基本的架构,我们只需要在这个架构上进行代码编写,扩展功能。而生成的一个小型系统是可以操作的,但是不是我们想要的,所以,这篇结合源码讲如何创建出我们自己的页面,并进行操作。

      那么yii是怎么处理这么一套流程的呢?

      大致执行过程。

      1. 请求访问index.php

      2. 由index.php作为一个初始化项目环境脚本,加载config/文件夹下的配置,构造出请求特征。

      3. 根据请求的url,找到对应的控制器。

      4. 在根据请求的url,找到请求的在控制器中的处理方法,在该方法中执行业务逻辑。

      5. 调用模版。还是根据控制器的URL特点,找到指定模版的位置,参数传入,执行代码,返回body给调用处。

      6. 完成一次请求。

    先看以上流程提及的可能使用的文件及文件夹。

    index.php自动生成的代码:

    <?php
    
    // change the following paths if necessary
    $yii=dirname(__FILE__).'/../framework/yii.php';
    $config=dirname(__FILE__).'/protected/config/main.php';
    
    // remove the following lines when in production mode
    defined('YII_DEBUG') or define('YII_DEBUG',true);
    // specify how many levels of call stack should be shown in each log message
    defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);
    
    require_once($yii);
    Yii::createWebApplication($config)->run();

    这里项目的几个目录,我们在框架中已经定义的比较死了。

    views为模版目录,下面会根据控制器进行创建一个文件夹,还会在文件夹下在创建一个对应控制方法的模版。

    controllers为控制器文件夹,控制器文件名为:控制器Controller.php,如SiteController.php而文件中只有一个类,类名也为控制器Controller,如SiteController

    控制器中的行为方法为“action行为”,形如:

    public function actionIndex()

    那么就在views中存在了这么一个模版,/views/site/index.php

    当然,所有这些,其实不用强记,只为了解,因为yii给我们提供了命令行工具,自动创建一个满足以上这些规则的控制器,模版。

    部分源码实现:

    //默认行为
    public $defaultAction='index';
    //默认的控制器
    public $defaultController='site';
    //查找对应控制器
    public function runController($route)//查找对应的控制器
        {
            if(($ca=$this->createController($route))!==null)//创建一个控制器和得到控制器ID。
            {
                list($controller,$actionID)=$ca;
                $oldController=$this->_controller;
                $this->_controller=$controller;//将最新的控制器ID作为奔雷的控制器属性,供全局使用。
                $controller->init();//控制器初始化。到底调用的那个类的方法。
                $controller->run($actionID);//运行对应的控制方法。
                $this->_controller=$oldController;//重置当前控制器为初始值。
            }
            }
    //控制器类名称结合
    $className=ucfirst($id).'Controller';
                $classFile=$basePath.DIRECTORY_SEPARATOR.$className.'.php';
    //控制器方法的处理
    $id[0]=strtolower($id[0]);
                        return array(
                            new $className($controllerID.$id,$owner===$this?null:$owner),
                            $this->parseActionParams($route),
                        );

    这里主要实现的是真正的业务逻辑,下面说如何用命令行方式创建出另外的控制器,另外的模版。

    进入cmd模式,执行shell命令。

    D:phpworkspacemy_demoindex>php protected/yiic.php shell
    Yii Interactive Tool v1.1 (based on Yii v1.1.10)
    Please type 'help' for help. Type 'exit' to quit.
    >> help

    输入help,查看到有如下命令:

    At the prompt, you may enter a PHP statement or one of the following commands:
     - controller
     - crud
     - form
     - help
     - model

    这里我们使用控制器。

    >> controller admin/post
          mkdir D:/php/workspace/my_demo/index/protected/controllers/admin
       generate PostController.php
          mkdir D:/php/workspace/my_demo/index/protected/views/admin
          mkdir D:/php/workspace/my_demo/index/protected/views/admin/post
       generate index.php
    
    Controller 'admin/post' has been created in the following file:

    提示我们说创建了这些东西。那么查看是否生成

    views里:

    也就是说,我们可以通过命令生成符合yii命名规则的要求文件。

    那么,如果我们想将原来的博客系统换成我们自己的网站,由yii系统我们知道我们需要修改views中site的index.php。

    修改index.php内容为:

    <html>
    <head>
    </head>
    <body>
    <center>这是我自己的第一个页面,虽然将原来的大段内容删除了。不在是一个博客</center>
    <a href="/admin/post/index/">指向admin/post/index.php的内容</a>
    </body>
    </html>

    查看页面内容:

    但是,为什么会有其他内容?我希望看到的是一个干净的页面啊。

  • 相关阅读:
    【cocos2d-js网络教程篇】cocos2d-js http网络请求
    COCOS2D
    Laravel5中的Session
    Laravel 下配置 Redis 让缓存、Session 各自使用不同的 Redis 数据库
    cocos-js Http方式网络请求
    Python语法31[module/package+import]
    cocos2d-js callFunc传参
    安装pygame for Python3.5
    阿里云vsftp安装和简单的配置
    Git代码行统计命令集
  • 原文地址:https://www.cnblogs.com/CLTANG/p/2638755.html
Copyright © 2011-2022 走看看