{{wikiTitle}}
Controller
复制链接
编辑文档
路由配置
文件位置:\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' => '[email protected]',
]);
}
}
评论({{cateWiki.comment_num}})
最新
最早
{{cateWiki.page_view_num}}人看过该文档
评论(0)
最新
最早
1324人看过
登录/注册
即可发表评论
{{item.user ? item.user.nickname : ''}}
(自评)
{{item.content}}
搜索结果
为您找到{{wikiCount}}条结果
{{item.page_view_num}}
{{item.like ? item.like.like_num : 0}}
{{item.comment ? item.comment.comment_num : 0}}
位置:
{{path.name}}
{{(i+1) == getCataloguePathData(item.catalogue).length ? '':'/'}}
