Home  >  Article  >  PHP Framework  >  An article explaining the definition and use of thinkphp controller in detail

An article explaining the definition and use of thinkphp controller in detail

藏色散人
藏色散人forward
2021-08-23 15:29:564007browse

The following is the thinkphp framework tutorial column to introduce the definition and use of the thinkphp controller. I hope it will be helpful to friends in need!

Controller definition

The class name is the same as the file name,

Rendering output

Rendering output uses return output

<?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);
    }

}

At this time, the page renders a json file
An article explaining the definition and use of thinkphp controller in detail

The code cannot be interrupted in the controller. .
Use halt output

<?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);
    }

}

Use halt output

An article explaining the definition and use of thinkphp controller in detail

Multi-level controller

Multi-level controller Multi-level controller directly Use

<?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;
    }
}

in the namespace to define the sub-controller Blog under the Index namespace
Directory structure
An article explaining the definition and use of thinkphp controller in detail

Define routing rules

<?php use think\facade\Route;

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

Access the blog directory under the index route

Basic Controller

The controller will have a basic controller
The system will provide a

app\BaseController

Basic Controller

The directory files are as follows
An article explaining the definition and use of thinkphp controller in detail

All controls have a basic control class
appBaseController

Because it is a multi-application mode. . Move the basic class to the directory
An article explaining the definition and use of thinkphp controller in detail

Change the namespace

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)
    {
        //
    }
}

Output content

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

Controller verification

<?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)
    {
        //
    }
}

This controller Verification

Empty Controller

The empty controller is the method called when the method cannot be found

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

Resource Controller

Create restful control Device
Input

php think make:controller index@Blog

Generate resource controller
Generate 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)
    {
        //
    }
}

Register resource routing

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

Controller middleware

Write the controller

<?php namespace app\index\middleware;

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

Use routing to register the controller

<?php use think\facade\Route;

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

Visit http://localhost:8082/index/ming
If ming

appears, it means the middleware registration is successful .

The above is the detailed content of An article explaining the definition and use of thinkphp controller in detail. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete