5

我想为登录用户提供一般主页和不同的主页
我在谷歌上搜索了很多但我找不到在我的 if 语句中放入什么

我试过这样的事情:

Route::get('/', array('as'=>'home', function(){
    if (!Auth::check()) {
        Route::get('/', array('uses'=>'homecontroller@index'));
    }
    else{
        Route::get('/', array('uses'=>'usercontroller@home'));
    }
}));

我也尝试使用类似的东西:

return Controller::call('homecontroller@index');

但似乎不适用于 laravel 4

我尝试了很多其他的东西,所以我认为这更像是一个误解问题

如果你有任何线索

感谢您的帮助

4

4 回答 4

9

好的,在这个平台和其他论坛上讨论后,我回来了一个紧凑的解决方案

Route::get('/', array('as'=>'home', 'uses'=> (Auth::check()) ? "usercontroller@home" : "homecontroller@index" ));
于 2013-09-19T13:34:22.613 回答
6

我能想到的最简单的解决方案是:

<?php

$uses = 'HomeController@index';
if( ! Auth::check())
{
    $uses = 'HomeController@home';
}

Route::get('/', array(
     'as'=>'home'
    ,'uses'=> $uses
));

或者您可以将 url / 路由到方法 index() 并在那里执行 Auth::check() 。

于 2013-09-19T12:05:19.487 回答
3
// 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
    }

}
于 2013-09-19T12:08:09.277 回答
0

您应该尝试以下方法:

Route::get('/', array('as'=>'home', function(){
    if (!Auth::check()) {
        Redirect::to('home/index'));
    }
    else{
        Redirect::to('user/index'));
    }
}));

因此,您基本上是根据 Auth 检查重定向用户,而不是定义额外的路由。

或者使用路由过滤器

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

于 2013-09-19T12:30:54.823 回答