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

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

藏色散人
Release: 2021-08-25 09:01:57
forward
4085 people have browsed it

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

}
Copy after login

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

}
Copy after login

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;
    }
}
Copy after login

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;);
Copy after login

Access the blog directory under the index route

Basic Controller

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

app\BaseController
Copy after login

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;
Copy after login
<?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)
    {
        //
    }
}
Copy after login

Output content

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

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

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';
    }
Copy after login

Resource Controller

Create restful control Device
Input

php think make:controller index@Blog
Copy after login

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

Register resource routing

Route::resource('blog', 'Blog');
Copy after login

Controller middleware

Write the controller

<?php namespace app\index\middleware;

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

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
    ]
);
Copy after login

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!

Related labels:
php
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template