【问题标题】:Laravel same route, different controllerLaravel 相同的路由,不同的控制器
【发布时间】:2013-09-24 10:17:21
【问题描述】:

我想要一般的主页 和登录用户的不同主页
我在谷歌上搜索了很多,但我找不到在我的 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

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

如果你有任何线索

感谢您的帮助

【问题讨论】:

    标签: php controller routing laravel laravel-4


    【解决方案1】:

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

    Route::get('/', array('as'=>'home', 'uses'=> (Auth::check()) ? "usercontroller@home" : "homecontroller@index" ));
    

    【讨论】:

      【解决方案2】:

      您应该尝试以下方法:

      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

      【讨论】:

        【解决方案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
            }
        
        }
        

        【讨论】:

        • 感谢 andreyco,但这不是我想要的。正如您在我的示例中看到的那样,我想路由到另一个控制器,所以我更喜欢在路由文件中进行测试
        【解决方案4】:

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

        <?php
        
        $uses = 'HomeController@index';
        if( ! Auth::check())
        {
            $uses = 'HomeController@home';
        }
        
        Route::get('/', array(
             'as'=>'home'
            ,'uses'=> $uses
        ));
        

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

        【讨论】:

        • 我被困在路线封闭中,你改变了之前做测试的视角......你太棒了!它完美地工作
        • 这行得通,但是这样的编程逻辑会乱扔您的 routes.php 文件,使其更难阅读。在我看来,使用过滤器是一个更好的选择。
        • 我同意,但我现在找不到使用过滤器的解决方案并留在根目录“/”
        • @GladToHelp 那么您将如何使用过滤器来做到这一点?
        猜你喜欢
        • 2016-03-29
        • 2017-04-05
        • 2016-10-03
        • 2018-12-12
        • 2016-03-03
        • 2021-05-26
        • 2018-02-18
        • 2016-06-04
        • 2014-11-01
        相关资源
        最近更新 更多