亚洲最大看欧美片,亚洲图揄拍自拍另类图片,欧美精品v国产精品v呦,日本在线精品视频免费

  • 站長資訊網(wǎng)
    最全最豐富的資訊網(wǎng)站

    一文詳解thinkphp控制器的定義和使用

    下面由thinkphp框架教程欄目給大家介紹thinkphp控制器的定義和使用,希望對需要的朋友有所幫助!

    控制器定義

    類名和文件名一樣,

    渲染輸出

    渲染輸出使用return輸出

    <?php namespace appadmincontroller; use appadminmodelUser;  class Index {      public function Index(){         $data = array(             'ming' => 'ming',             'ming' => 'xiao'         );         return json($data);     }  }

    此時(shí)頁面渲染出json文件
    一文詳解thinkphp控制器的定義和使用

    不能在控制器中中斷代碼。。
    使用halt輸出

    <?php namespace appadmincontroller; use appadminmodelUser;  class Index {      public function Index(){         $data = array(             'ming' => 'ming',             'ming' => 'xiao'         );         halt("輸出測試");         return json($data);     }  }

    使用halt 輸出

    一文詳解thinkphp控制器的定義和使用

    多級控制器

    多級控制器 多級控制器直接在命名空間中使用

    <?php   namespace appadmincontrollerIndex;   class Blog {     public function index(){      }      public function read($id){         var_dump(url('index/blog/read', ['id' => 5, 'name' => 'ming']));         return $id;     } }

    定義了Index命名空間下的子控制器 Blog
    目錄結(jié)構(gòu)
    一文詳解thinkphp控制器的定義和使用

    定義路由規(guī)則

    <?php use thinkfacadeRoute;  Route::rule('blog/:id', 'index.blog/read'); Route::rule('/', 'Index/index');

    訪問index路由下的blog目錄

    基礎(chǔ)控制器

    控制器都會有一個(gè)基礎(chǔ)控制器
    系統(tǒng)會提供一個(gè)

    appBaseController

    基礎(chǔ)控制器

    目錄文件如下
    一文詳解thinkphp控制器的定義和使用

    所有的控制都有一個(gè)基礎(chǔ)控制類
    appBaseController

    由于是多應(yīng)用模式。?;A(chǔ)類移動到目錄下
    一文詳解thinkphp控制器的定義和使用

    更改命名空間

    namespace appindexcontroller;  use thinkApp; use thinkexceptionValidateException; use thinkValidate;
    <?php  namespace appindexcontroller;  use thinkRequest;  class Index extends BaseController {     /**      * 顯示資源列表      *      * @return thinkResponse      */     public function index()     {         $action = $this->request->action();         $path = $this->app->getBasePath();         var_dump($action);         var_dump($path);     }      /**      * 顯示創(chuàng)建資源表單頁.      *      * @return thinkResponse      */     public function create()     {         //     }      /**      * 保存新建的資源      *      * @param  thinkRequest  $request      * @return thinkResponse      */     public function save(Request $request)     {         //     }      /**      * 顯示指定的資源      *      * @param  int  $id      * @return thinkResponse      */     public function read($id)     {         //     }      /**      * 顯示編輯資源表單頁.      *      * @param  int  $id      * @return thinkResponse      */     public function edit($id)     {         //     }      /**      * 保存更新的資源      *      * @param  thinkRequest  $request      * @param  int  $id      * @return thinkResponse      */     public function update(Request $request, $id)     {         //     }      /**      * 刪除指定資源      *      * @param  int  $id      * @return thinkResponse      */     public function delete($id)     {         //     } }

    輸出內(nèi)容

    string(5) "index" string(43) "/home/ming/PhpstormProjects/untitled12/app/"

    控制器驗(yàn)證

    <?php  namespace appindexcontroller;  use thinkexceptionValidateException; use thinkRequest;  class Index extends BaseController {     /**      * 顯示資源列表      *      * @return thinkResponse      */     public function index()     {         try {             $this->validate( [                 'name'  => 'thinkphp',                 'email' => 'thinkphp@qq.com',             ],  'appindexvalidateUser');         } catch (ValidateException $e) {             // 驗(yàn)證失敗 輸出錯(cuò)誤信息             dump($e->getError());         }     }      /**      * 顯示創(chuàng)建資源表單頁.      *      * @return thinkResponse      */     public function create()     {         //     }      /**      * 保存新建的資源      *      * @param  thinkRequest  $request      * @return thinkResponse      */     public function save(Request $request)     {         //     }      /**      * 顯示指定的資源      *      * @param  int  $id      * @return thinkResponse      */     public function read($id)     {         //     }      /**      * 顯示編輯資源表單頁.      *      * @param  int  $id      * @return thinkResponse      */     public function edit($id)     {         //     }      /**      * 保存更新的資源      *      * @param  thinkRequest  $request      * @param  int  $id      * @return thinkResponse      */     public function update(Request $request, $id)     {         //     }      /**      * 刪除指定資源      *      * @param  int  $id      * @return thinkResponse      */     public function delete($id)     {         //     } }

    這樣控制器驗(yàn)證

    空控制器

    空控制器是當(dāng)找不到的方法的時(shí)候調(diào)用的方法

        public function __call($name, $arguments)     {         // TODO: Implement __call() method.         return 'error request';     }

    資源控制器

    創(chuàng)建restful控制器
    輸入

    php think make:controller index@Blog

    生成資源控制器
    生成api

    <?php  namespace appindexcontroller;  use thinkRequest;  class Blog {     /**      * 顯示資源列表      *      * @return thinkResponse      */     public function index()     {         //     }      /**      * 保存新建的資源      *      * @param  thinkRequest  $request      * @return thinkResponse      */     public function save(Request $request)     {         //     }      /**      * 顯示指定的資源      *      * @param  int  $id      * @return thinkResponse      */     public function read($id)     {         //     }      /**      * 保存更新的資源      *      * @param  thinkRequest  $request      * @param  int  $id      * @return thinkResponse      */     public function update(Request $request, $id)     {         //     }      /**      * 刪除指定資源      *      * @param  int  $id      * @return thinkResponse      */     public function delete($id)     {         //     } }

    注冊資源路由即可

    Route::resource('blog', 'Blog');

    控制器中間件

    編寫控制器

    <?php   namespace appindexmiddleware;  class Hello {     public function handle($request, Closure $next){         $request->hello = 'ming';         return $next($request);     } }

    使用路由注冊控制器

    <?php  use thinkfacadeRoute;  Route::rule('ming', 'index/index')->middleware(     [         appindexmiddlewareHello::class     ] );

    訪問 http://localhost:8082/index/ming
    出現(xiàn) ming

    說明中間件注冊成功。

    推薦:《最新的10個(gè)thinkphp視頻教程》

    贊(0)
    分享到: 更多 (0)
    網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號