最新消息: 关于Git&GitHub 版本控制你了解多少?
您现在的位置是:群英 > 开发技术 > PHP语言 >
thinkphp控制器是什么?如何使用?
PHP中文网发表于 2021-08-24 17:45 次浏览

    这篇文章主要给大家分享的是thinkphp控制器的内容,对于新手刚接触PHP,可能对thinkphp控制器是什么以及thinkphp控制器的用法不是很了解,对此下文就给大家介绍一下thinkphp控制器,有这方面学习需要的朋友可以参考。

    控制器定义

    类名和文件名一样,

    渲染输出

    渲染输出使用return输出

<?php
namespace appadmincontroller;
use appadminmodelUser;

class Index
{

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

}

    此时页面渲染出json文件

    不能在控制器中中断代码。。        

    使用halt输出

<?php
namespace appadmincontroller;
use appadminmodelUser;

class Index
{

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

}

    使用halt 输出

    多级控制器

    多级控制器 多级控制器直接在命名空间中使用

<?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    
目录结构

    定义路由规则

<?php
use thinkfacadeRoute;

Route::rule('blog/:id', 'index.blog/read');
Route::rule('/', 'Index/index');

    访问index路由下的blog目录

    基础控制器

    控制器都会有一个基础控制器        

    系统会提供一个

appBaseController

    基础控制器

    目录文件如下

    所有的控制都有一个基础控制类    
appBaseController

    由于是多应用模式。。基础类移动到目录下

    更改命名空间

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

    /**
     * 显示创建资源表单页.
     *
     * @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)
    {
        //
    }
}

    输出内容

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

    控制器验证

<?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) {
            // 验证失败 输出错误信息
            dump($e->getError());
        }
    }

    /**
     * 显示创建资源表单页.
     *
     * @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)
    {
        //
    }
}

    这样控制器验证

    空控制器

    空控制器是当找不到的方法的时候调用的方法

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

    资源控制器

    创建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     

    出现 ming

    说明中间件注册成功。

    关于thinkphp控制器的内容就介绍到这了,上述对于thinkphp控制器的操作有详细的介绍,希望本文能对大家有帮助,想要了解更多thinkphp控制器的内容,请关注群英网络其它相关文章。

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
上一篇:没有了
相关信息推荐