[英]Laravel same route, different controller
I would like to have general home page and a different homepage for logged-in users 我希望为登录用户提供常规主页和不同的主页
I search a lot on google but I can't find what to put in my if statement 我在谷歌搜索了很多,但我无法找到我的if语句中的内容
I tried something like this: 我试过这样的事情:
Route::get('/', array('as'=>'home', function(){
if (!Auth::check()) {
Route::get('/', array('uses'=>'homecontroller@index'));
}
else{
Route::get('/', array('uses'=>'usercontroller@home'));
}
}));
I also try with something like: 我也尝试过类似的东西:
return Controller::call('homecontroller@index');
but it seems it's not for laravel 4 但似乎不适合laravel 4
I tried a lot of other things so I think it's more a misconception problem 我尝试了很多其他的东西,所以我认为这更像是一个误解问题
If you have any clue 如果你有任何线索
thanks for your help 谢谢你的帮助
在这个平台和其他论坛的讨论之后,我回来了一个紧凑的解决方案
Route::get('/', array('as'=>'home', 'uses'=> (Auth::check()) ? "usercontroller@home" : "homecontroller@index" ));
The most simple solution I can think of is: 我能想到的最简单的解决方案是:
<?php
$uses = 'HomeController@index';
if( ! Auth::check())
{
$uses = 'HomeController@home';
}
Route::get('/', array(
'as'=>'home'
,'uses'=> $uses
));
Or you can just route the url / to method index() and do the Auth::check() in there. 或者您可以将url /路由到方法index()并在那里执行Auth :: check()。
// routes.php
Route::get('/', 'homecontroller@index');
// homecontroller.php
class homecontroller extends BaseController
{
public function index()
{
if (!Auth:: check()) {
return $this->indexForGuestUser();
} else {
return $this->indexForLoggedUser();
}
}
private function indexForLoggedUser()
{
// do whatever you want
}
private function indexForGuestUser()
{
// do whatever you want
}
}
You should try something like: 你应该尝试类似的东西:
Route::get('/', array('as'=>'home', function(){
if (!Auth::check()) {
Redirect::to('home/index'));
}
else{
Redirect::to('user/index'));
}
}));
So you are basically redirecting the user based on the Auth check instead of defining an additional route. 因此,您基本上是基于Auth检查重定向用户,而不是定义其他路由。
Or use route filters 或使用路由过滤器
Route::filter('authenticate', function()
{
if (!Auth::check())
{
return Redirect::to('home/index');
}
});
Route::get('home', array('before' => 'authenticate', function()
{
Redirect::to('user/index');
}));
http://laravel.com/docs/routing#route-filters http://laravel.com/docs/routing#route-filters
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.