有什么方法可以在路由中执行此配置.php:
Route::get('/', function(){
if ( Auth::check() === FALSE )
{
return homeController@guest<--- Specific Controller's method
}
else
{
return homeController@logged <--- Specific Controller's method
}
});
我不想使用重定向,因为我想保留mysite.com/
作为主地址。
当然,
您可以像@Ferticidios答案一样直接在路由中执行所有操作,或者像@maytham建议的那样只有一个控制器方法。但您也可以完全按照您的要求进行操作:
Route::get('/', function(){
if ( Auth::check() === FALSE )
{
return App::make('homeController')->callAction('guest', array());
}
else
{
return App::make('homeController')->callAction('logged', array());
}
});
你可以做这样的事情:
Route::get('/', function(){
if ( Auth::check() === FALSE )
{
//Do stuff... get data
return Response::view('guest')->with($data);
}
else
{
//Do stuff... get data
return Response::view('logged')->with($data);
}
});
这就是过滤器在 Laravel 中设计的目的。内置身份验证已经打开,或者您可以创建自己的身份验证
Route::get('user', array('before' => 'auth', function()
{
return App::make('homeController')->callAction('logged');
}));
可以在应用程序/过滤器中调整默认过滤器.php
http://laravel.com/docs/4.2/routing#route-filters