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( 'ming' => 'ming', 'ming' => 'xiao' ); return json($data); } }
At this time, the page renders a json file
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( 'ming' => 'ming', 'ming' => 'xiao' ); halt("输出测试"); return json($data); } }
Use halt output
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('index/blog/read', ['id' => 5, 'name' => 'ming'])); return $id; } }
in the namespace to define the sub-controller Blog under the Index namespace
Directory structure
Define routing rules
<?php use think\facade\Route; Route::rule('blog/:id', 'index.blog/read'); Route::rule('/', 'Index/index');
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
All controls have a basic control class
appBaseController
Because it is a multi-application mode. . Move the basic class to the directory
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('ming', 'index/index')->middleware( [ app\index\middleware\Hello::class ] );
Visit http://localhost:8082/index/ming
If ming
appears, it means the middleware registration is successful .