一、函数库和类库

    项目中的常用的函数库要封装到项目Common/function.php中  在项目中可以直接调用  [ 函数();]

    import方法是ThinkPHP内建的类库导入方法,提供了方便和灵活的文件导入机制,完全可以替代PH的                  require和include方法。

   例如:

     import("MyApp.Action.UserAction");

     impot("Think.Util.Array");

     import("ORG.Util.Page");

 

二、权限把控

    class BaseAction extends Action{

        public _initialize(){

             echo "先进行登录验证";

         }

       }

      class IndexAction extends BaseAction{
          public function index(){

            //访问前先执行登录验证

             $this->display();

         }

     }

    例子::

  

 生成跨项目的url

 __ROOT__."/admin.php/Index/index

判断请求类型:

 $this->isGet();//是否是get请求

 $this->isPost();//是否是post请求

tp中空操作(_empty())

<?php

    class CityAction extends Action{

         public function _empty($name){

               //把所有城市的操作解析到city方法中

               $this->city($name);

          }

           //protected方法

          protected function city($name){

                     echo $name;

              }

   }

?>

  空模块的操作

 

  1. <?php
  2.     class EmptyAction extends Action{
  3.         public function index(){
  4.             //根据当前模块名来判断要执行那个城市的操作
  5.              $cityName = MODULE_NAME;
  6.             $this->city($cityName);
  7.         }
  8.         //注意 city方法 本身是 protected 方法
  9.         protected function city($name){
  10.             //和$name这个城市相关的处理
  11.              echo '当前城市' . $name;
  12.         }
  13.     }
  14. ?>

项目中使用空模块的例子:

ThinkPHP支持伪静态URL设置

'URL_HTML_SUFFIX' => '.shtml'

URL路由

  1. 'URL_ROUTER_ON'   => true, //开启路由
  2. 'URL_ROUTE_RULES' => array( //定义路由规则
  3.     'news/:year/:month/:day' => array('News/archive', 'status=1'),
  4.     'news/:id'               => 'News/read',
  5.     'news/read/:id'          => '/news/:1',
  6. ),

  

访问localhost/tpshop/index.php/news/10 就会访问Index/show方法

posted on 2016-05-18 16:56  晨曦年华  阅读(569)  评论(0编辑  收藏  举报