類名和文件名同樣,php
渲染輸出使用return輸出thinkphp
<?php
namespace app\admin\controller;
use app\admin\model\User;
class Index
{
public function Index(){
$data = array(
'ming' => 'ming',
'ming' => 'xiao'
);
return json($data);
}
}
複製代碼
此時頁面渲染出json文件 json
不能在控制器中中斷代碼。。 使用halt輸出api
<?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);
}
}
複製代碼
使用halt 輸出bash
多級控制器 多級控制器直接在命名空間中使用restful
<?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;
}
}
複製代碼
定義了Index命名空間下的子控制器 Blog 目錄結構 app
定義路由規則測試
<?php
use think\facade\Route;
Route::rule('blog/:id', 'index.blog/read');
Route::rule('/', 'Index/index');
複製代碼
訪問index路由下的blog目錄ui
控制器都會有一個基礎控制器 系統會提供一個this
app\BaseController
複製代碼
基礎控制器
目錄文件以下
全部的控制都有一個基礎控制類 app\BaseController
因爲是多應用模式。。基礎類移動到目錄下
更改命名空間
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/"
複製代碼
<?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)
{
//
}
}
複製代碼
這樣控制器驗證
空控制器是當找不到的方法的時候調用的方法
public function __call($name, $arguments)
{
// TODO: Implement __call() method.
return 'error request';
}
複製代碼
建立restful控制器 輸入
php think make:controller index@Blog
複製代碼
生成資源控制器 生成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)
{
//
}
}
複製代碼
註冊資源路由便可
Route::resource('blog', 'Blog');
複製代碼
編寫控制器
<?php
namespace app\index\middleware;
class Hello
{
public function handle($request, \Closure $next){
$request->hello = 'ming';
return $next($request);
}
}
複製代碼
使用路由註冊控制器
<?php
use think\facade\Route;
Route::rule('ming', 'index/index')->middleware(
[
app\index\middleware\Hello::class
]
);
複製代碼
訪問 http://localhost:8082/index/ming
出現 ming
說明中間件註冊成功