首頁  >  文章  >  php框架  >  一文詳解thinkphp控制器的定義與使用

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

藏色散人
藏色散人轉載
2021-08-23 15:29:563986瀏覽

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

控制器定義

#類別名稱和檔案名稱一樣,

渲染輸出

渲染輸出使用return輸出

<?php namespace app\admin\controller;
use app\admin\model\User;

class Index
{

    public function Index(){
        $data = array(
            &#39;ming&#39; => 'ming',
            'ming' => 'xiao'
        );
        return json($data);
    }

}

此時頁面渲染出json檔案
一文詳解thinkphp控制器的定義與使用

#不能在控制器中中斷程式碼。 。
使用halt輸出

<?php namespace app\admin\controller;
use app\admin\model\User;

class Index
{

    public function Index(){
        $data = array(
            &#39;ming&#39; => 'ming',
            'ming' => 'xiao'
        );
        halt("输出测试");
        return json($data);
    }

}

使用halt 輸出

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

#多級控制器

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

<?php namespace app\admin\controller\Index;


class Blog
{
    public function index(){

    }

    public function read($id){
        var_dump(url(&#39;index/blog/read&#39;, [&#39;id&#39; => 5, 'name' => 'ming']));
        return $id;
    }
}

定義了Index命名空間下的子控制器Blog
目錄結構
一文詳解thinkphp控制器的定義與使用

定義路由規則

<?php use think\facade\Route;

Route::rule(&#39;blog/:id&#39;, &#39;index.blog/read&#39;);
Route::rule(&#39;/&#39;, &#39;Index/index&#39;);

存取index路由下的blog目錄

基礎控制器

控制器都會有一個基礎控制器
系統會提供一個

app\BaseController

基礎控制器

#目錄檔案如下
一文詳解thinkphp控制器的定義與使用

所有的控制都有一個基礎控制類別
appBaseController

由於是多重應用模式。 。基礎類別移到目錄下
一文詳解thinkphp控制器的定義與使用

更改命名空間

namespace app\index\controller;

use think\App;
use think\exception\ValidateException;
use think\Validate;
<?php namespace app\index\controller;

use think\Request;

class Index extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $action = $this->request->action();
        $path = $this->app->getBasePath();
        var_dump($action);
        var_dump($path);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}

輸出內容

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

控制器驗證

<?php namespace app\index\controller;

use think\exception\ValidateException;
use think\Request;

class Index extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        try {
            $this->validate( [
                'name'  => 'thinkphp',
                'email' => 'thinkphp@qq.com',
            ],  'app\index\validate\User');
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            dump($e->getError());
        }
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}

這樣控制器驗證

空控制器

空控制器是當找不到的方法的時候呼叫的方法

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

資源控制器

建立restful控制器
輸入

php think make:controller index@Blog

產生資源控制器
產生api

<?php namespace app\index\controller;

use think\Request;

class Blog
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}

#註冊資源路由即可

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

控制器中間件

編寫控制器

<?php namespace app\index\middleware;

class Hello
{
    public function handle($request, \Closure $next){
        $request->hello = 'ming';
        return $next($request);
    }
}

使用路由註冊控制器

<?php use think\facade\Route;

Route::rule(&#39;ming&#39;, &#39;index/index&#39;)->middleware(
    [
        app\index\middleware\Hello::class
    ]
);

訪問http://localhost:8082/index/ming  
出現ming

說明中間件註冊成功。

以上是一文詳解thinkphp控制器的定義與使用的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:segmentfault.com。如有侵權,請聯絡admin@php.cn刪除