Controller defines
Class name and file name as
Render Output
Render output using output return
<?php
namespace app\admin\controller;
use app\admin\model\User;
class Index
{
public function Index(){
$data = array(
'ming' => 'ming',
'ming' => 'xiao'
);
return json($data);
}
}
The page rendering a json file
You can not break the code 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 controllers
Multi-level controllers use multi-level controller directly in the namespace
<?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;
}
}
It defines the sub-controller in the Index namespace Blog
Directory Structure
Define routing rules
<?php
use think\facade\Route;
Route::rule('blog/:id', 'index.blog/read');
Route::rule('/', 'Index/index');
blog directory access route index
Base controller
The controller will have a base controller
system will provide a
app\BaseController
Base controller
Directory files are as follows
All controls have a base control class
app \ BaseController
Because it is multi-application mode. . Mobile base 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/"
The 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' => '[email protected]',
], '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)
{
//
}
}
Such verification controller
Air Controller
The controller method is empty when the method can not find the time to call
public function __call($name, $arguments)
{
// TODO: Implement __call() method.
return 'error request';
}
Resource Controller
Create a restful controller
input
php think make:controller index@Blog
Generating resource controller
generates 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)
{
//
}
}
Sign up routing resources to
Route::resource('blog', 'Blog');
Controller Middleware
Write controller
<?php
namespace app\index\middleware;
class Hello
{
public function handle($request, \Closure $next){
$request->hello = 'ming';
return $next($request);
}
}
Use Routing Registry controller
<?php
use think\facade\Route;
Route::rule('ming', 'index/index')->middleware(
[
app\index\middleware\Hello::class
]
);
Visit http: // localhost: 8082 / index
/ ming appear ming
Description middleware successfully registered