-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Controller
liaofei edited this page Jan 20, 2021
·
1 revision
多应用模式下,控制器类定义仅仅是命名空间有所区别,例如:
<?php
namespace app\shop\controller;
class User
{
public function login()
{
return 'login';
}
}
文件位置:app\shop\route\route.php
详细路由文档说明
浏览器输入'http://域名/shop/login' 访问
use think\facade\Route;
Route::get('login','user/login');
如果你希望避免引入同名模型类的时候冲突,可以在route.php
配置文件中设置
// 使用控制器后缀
'controller_suffix' => true,
这样类名就要命名为UserController
控制器一般不需要任何输出,直接
return
即可。并且控制器在json
请求会自动转换为json
格式输出。
不要在控制器中使用包括die
、exit
在内的中断代码。如果你需要调试并中止执行,可以使用系统提供的halt
助手函数
namespace app\controller;
use app\Request;
use app\services\user\UserServices;
class Index
{
public function index(Request $request,UserServices $services)
{
$action = $request->action();
$userInfo = $services->getUserInfo();
return app('json')->success(['userInfo' => $userInfo, 'action' => $action]);
}
}
namespace app\controller;
class Index
{
public function index()
{
//调用validate助手函数验证数据,需要在异常处理文件中捕获到验证数据的异常,做对应的响应处理
validate('app\index\validate\User')->check([
'name' => 'thinkphp',
'email' => '[email protected]',
]);
}
}