php th,thikphp 控制器

控制器定义

类名和文件名一样,

渲染输出

渲染输出使用return输出

namespace app\admin\controller;

use app\admin\model\User;

class Index

{

public function Index(){

$data = array(

'ming' => 'ming',

'ming' => 'xiao'

);

return json($data);

}

}

复制代码

此时页面渲染出json文件b2729bf2785607ef9e1e1472f903ce0d.png

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

使用halt输出

namespace app\admin\controller;

use app\admin\model\User;

class Index

{

public function Index(){

$data = array(

'ming' => 'ming',

'ming' => 'xiao'

);

halt("输出测试");

return json($data);

}

}

复制代码

使用halt 输出

028f6d534c1e594ffae649601ae532e0.png

多级控制器

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

namespace app\admin\controller\Index;

class Blog

{

public function index(){

}

public function read($id){

var_dump(url('index/blog/read', ['id' => 5, 'name' => 'ming']));

return $id;

}

}

复制代码

定义了Index命名空间下的子控制器 Blog

目录结构f18232630bb5b4a8d3e16654f176b1bc.png

定义路由规则

use think\facade\Route;

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

Route::rule('/', 'Index/index');

复制代码

访问index路由下的blog目录

基础控制器

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

系统会提供一个

app\BaseController

复制代码

基础控制器

目录文件如下33fac7849bed6103544cda387b626137.png

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

app\BaseController

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

更改命名空间

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/"

复制代码

控制器验证

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

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

复制代码

控制器中间件

编写控制器

namespace app\index\middleware;

class Hello

{

public function handle($request, \Closure $next){

$request->hello = 'ming';

return $next($request);

}

}

复制代码

使用路由注册控制器

use think\facade\Route;

Route::rule('ming', 'index/index')->middleware(

[

app\index\middleware\Hello::class

]

);

复制代码

说明中间件注册成功

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值