Php laravel 4按1d显示查询用户
我正在使用Laravel4进行一个web项目,我正在创建一个管理面板 在admin中,我使用admin/profile/{id}来显示用户配置文件,如firstname、lastname等 在我的AdminController中,我有:Php laravel 4按1d显示查询用户,php,laravel-4,Php,Laravel 4,我正在使用Laravel4进行一个web项目,我正在创建一个管理面板 在admin中,我使用admin/profile/{id}来显示用户配置文件,如firstname、lastname等 在我的AdminController中,我有: // get the Admin Profile page public function getProfile($id) { // get the user from the database $user = User::f
// get the Admin Profile page
public function getProfile($id) {
// get the user from the database
$user = User::find($id);
$this->layout->content = View::make('admin.profile', array('user' => $user));
}
但是,如果我只是在没有任何用户id的情况下转到admin/profile,会发生什么情况,我如何让它工作
基本上,如果页面不存在,如何转到仪表板或类似的东西?例如,如果他们尝试了admin/test,而test不是其中的一种方法,如果他们登录,它将转到仪表板,如果不是,它将转到登录页面?您问了两个问题:
admin/profile
,会出现404错误。如何重定向到登录页面Route::get('{any_url}', function(){ return Redirect::route("login"); });
此必须是定义的最后一个路由,因为它将匹配任何URL
另一种方法是在start/global.php
文件中捕获NotFoundHttpException
。添加此代码:
App::error(function(\Symfony\Component\HttpKernel\Exception\NotFoundHttpException $exception, $code)
{
return Redirect::route("login");
});
这两个示例都重定向到名为login
的命名路由
至于第二个问题,正确的处理方法是使用auth
过滤器。在filters.php
文件中,可以添加如下内容:
Route::filter('auth', function($route)
{
// is the user authorized? if not, redirect to the login page
if (!user_is_authorized())
{
// redirect to the login page
return Redirect::route('login');
}
});
App::missing(function($e){
// Log the missing url
Log::error($e);
// You may redirect to home
return Redirect::to('/');
// Or redirect to a 404 route that is declared
// to show a custom 404 page from that route
return Redirect::to('missing');
});
Route::get('missing', function(){
// show the view: errors.missing
$this->layout->content = View::make('errors.missing');
});
当
user\u被授权时
功能只是您在代码中执行的任何检查的简写。有关使用auth
过滤器的信息,请参阅。您只需添加一个缺失的处理程序(句柄404
),如下所示:
Route::filter('auth', function($route)
{
// is the user authorized? if not, redirect to the login page
if (!user_is_authorized())
{
// redirect to the login page
return Redirect::route('login');
}
});
App::missing(function($e){
// Log the missing url
Log::error($e);
// You may redirect to home
return Redirect::to('/');
// Or redirect to a 404 route that is declared
// to show a custom 404 page from that route
return Redirect::to('missing');
});
Route::get('missing', function(){
// show the view: errors.missing
$this->layout->content = View::make('errors.missing');
});
将代码(上面给出的)放入app/start/global.php
文件中。对于缺失的url/route
您需要在routes.php
文件中添加一条路径,如下所示:
Route::filter('auth', function($route)
{
// is the user authorized? if not, redirect to the login page
if (!user_is_authorized())
{
// redirect to the login page
return Redirect::route('login');
}
});
App::missing(function($e){
// Log the missing url
Log::error($e);
// You may redirect to home
return Redirect::to('/');
// Or redirect to a 404 route that is declared
// to show a custom 404 page from that route
return Redirect::to('missing');
});
Route::get('missing', function(){
// show the view: errors.missing
$this->layout->content = View::make('errors.missing');
});
创建一个视图
作为视图/错误/missing.blade.php
并在missing.blade.php
视图中,显示一条奇特的消息,通知访问者,请求的页面/url
不可用,并在该404
页面中添加到主页的链接
请阅读Laravel网站上关于的更多信息,检查。对于第一个问题,如果我添加NotFoundHttpException并不是针对我的整个应用程序,因此每当出现错误时,它将被重定向到登录页面??我是否可以将代码添加到我的getProfile($id)中,其中id未设置,例如重定向到仪表板?这可能吗?是的,NotFoundHttpException
将应用于整个应用程序。如果你不想那样,那就做点别的吧。对于您的第二条评论,是的,如果您在路由中设置{id}
参数为可选的话,这也会起作用;如何使其成为可选的?我在AdminController中尝试过这一点:公共函数getProfile($id){if(!isset($id))Redirect::to('admin/dashboard');$user=user::find($id);$this->layout->content=View::make('admin.profile',array('user'=>$user));}但仍然出现错误:AdminController::getProfile()缺少参数1