Yii框架后续

      关于Yii框架遗留的知识点。

     1.url路由方式

         (1).问号传参(默认)

                eg: http://localhost/项目/app/index.php

                      http://localhost/项目/app/index.php?r=控制器/方法

                      http://localhost/项目/app/index.php?r=控制器/方法/名/值

                      <a href="index.php?r=控制器/方法"></a>

                      <img src="images/xxx.jpg">

                       <link href="css/xxx.css">

                    <script src="assets/jquery/jquery-1.4.js">

         (2).pathinfo的方式

                eg:   http://localhost/项目/app/index.php

                        http://localhost/项目/app/index.php/控制器/方法

                        http://localhost/项目/app/index.php/控制器/方法/名/值

                        <a href="入口文件地址/控制器/方法"></a>

                         <img src="根目录地址/images/xxx.jpg">

                         <link href="根目录地址/css/xxx.css">

                        <script src="根目录地址/assets/jquery/jquery-1.4.js">

                 注:Yii框架默认的传参方式时url传参,必须在配置文件上开启pathinfo传参方式,并且不能加伪静态后缀.Html,因为没有封装。

                          修改配置文件:开启pathinfo访问模式:打开config->main.php找到下面的这些

/*

  'urlManager'=>array(

     .....

  ),

*/

去掉上边代码的注释


 

        2.设置错误页面(当网站出错时,跳转到哪个页面。在配置文件里修改)

            

 'errorHandler'=>array(

                 // use 'site/error' action to display errors

                      'errorAction'=>'error/index',

                  ),

 

                 在controllers、views下新建一个错误页面

 


 

        3.自定义系统常量

             :在入口文件中定义

            (1)、APP常量(写链接地址、跳转地址)

                define("APP","/项目/app/index.php");

                define("APP","/bbs/app/index.php");

                define("APP","/php/mvc/day_08/bbs/app/index.php");

            (2)、ROOT常量(引用css、images、assets)

                define("ROOT","/项目/app");

                define("ROOT","/bbs/app");

                define("ROOT","/php/mvc/day_08/bbs/app");


 

         4.重定向

            $this->redirect("跳转地址");

            $this->redirect("index.php?r=控制器/方法");

            $this->redirect(APP."/控制器/方法");

 


 

      5.自定义类的做法

             (1)、在components目录下新建自定义类

             (2)、控制器通过类名直接调用该类既可(不用包含,系统自动包含)

             (3)、配置文件中,设置自动包含文件的目录

                

 'import'=>array(
                           'application.models.*',//models目录下的文件自动包含
'application.components.*',//components目录下的文件自动包含
'application.data.*',//例子:比如data目录下所有文件自动包含 ),

          6.验证码的做法

(1)、系统自带的验证码

   /项目/framework/web/widgets/captcha/CCaptchaAction.php

   1)控制器中的代码

public function actions()

     {

return array(

   "名  "=>array("class"=>"system.web.widgets.captcha.CCaptchaAction"),

);

     }

   2)视图的图片

     <img src="<?php echo APP?>/login/名">

(2)、自定义验证码类

   /项目/app/protected/components/Image.php

   1)控制器中的代码

  public function actions()

     {

return array(

   "名"=>array("class"=>"application.components.Image"),

);

     }

        2)视图的图片

           <img src="<?php echo APP?>/login/名">

7.Yii类里的常用属性

      (1)、数据库封装类的对象 

        Yii::app()->db; 

(2)、获得错误对象的句柄 

         Yii::app()->errorHandler; 

         Yii::app()->errorHandler->error; 

(3)、获得或设置session 

      $变量 = Yii::app()->session["名"]; 

       Yii::app()->session["名"] = 值; 

(4)、获得或设置cookie 

       $变量 = Yii::app()->cookie["名"]; 

      Yii::app()->cookie["名"] = 值;

       8.Yii缓存:文件缓存

(1)、页面缓存

(2)、片段缓存(局部缓存)

(3)、数据缓存(变量缓存)

(4)、动态缓存(局部不缓存)

修改配置文件,开启缓存

"components"=>array(

   //开启缓存

   "cache"=>array(

"class"=>"system.caching.CFileCache"

   ),

   ......

   ......

);

片段缓存(局部缓存)

1>、在视图页面添加如下代码  

 <?php

if($this->beginCache("别名",array("duration"=>时间)))

        {

   ?>

         缓存内容

   <?php

   $this->endCache();

}
   ?>

页面缓存

1>、在控制器中添加如下代码 

public function filters()
{
  return array(

     array(

"system.web.widgets.COutputCache",

    "duration"=>时间

     )
  );
} 

数据缓存(变量缓存) 

1>、在控制器中添加如下代码

   1)向缓存中添加数据

     Yii::app()->cache->set(名,值,时间);

   2)获得缓存中指定的数据

     $变量 = Yii::app()->cache->get(名);

   3)删除缓存中指定的数据

     Yii::app()->cache->delete(名);

   4)清空缓存

     Yii::app()->cache->flush();

动态缓存(局部不缓存)

1>、首先开启页面缓存 

2>、在控制器中添加如下方法 

   public function 方法名()

   {

return 不想缓存的数据;

   } 

3、在视图上添加如下代码

   echo $this->renderDynamic("方法名");

 

看到最后的亲们,有好礼啦。这是自己编写的Yii框架手册,拿走,不谢......   (链接:http://pan.baidu.com/s/1kUNdhHH 密码:xypq)

关于Yii框架的暂时说到这里。下期见喽......

 

 

 

原文地址:https://www.cnblogs.com/xiaonannan/p/6807377.html