路由配置
文件位置:\route\admin.php
详细路由文档说明
浏览器输入’http://域名/admin/user/lst‘ 访问
use think\facade\Route;
Route::get('user/lst','admin.user.User/lst');
控制器后缀
如果你希望避免引入同名模型类的时候冲突,可以在route.php
配置文件中设置
// 使用控制器后缀
'controller_suffix' => true,
这样类名就要命名为UserController
控制器一般不需要任何输出,直接
return
即可。并且控制器在json
请求会自动转换为json
格式输出。
不要在控制器中使用包括die
、exit
在内的中断代码。如果你需要调试并中止执行,可以使用系统提供的halt
助手函数
控制器中使用依赖注入直接实例化类
namespace app\controller;
use app\Request;
use app\common\repositories\user\UserRepository;
class User
{
protected $repository;
public function __construct(App $app, repository $repository)
{
parent::__construct($app);
$this->repository = $repository;
}
public function lst($cid)
{
[$page, $limit] = $this->getPage();
$where = $this->request->params(['id']);
$data = $this->repository->search($where, $page, $limit);
return app('json')->success($data);
}
}
控制器数据验证使用如下:
namespace app\controller;
class Index
{
public function index()
{
//调用validate助手函数验证数据,需要在异常处理文件中捕获到验证数据的异常,做对应的响应处理
validate('app\index\validate\User')->check([
'name' => 'thinkphp',
'email' => 'thinkphp@qq.com',
]);
}
}