Laravel 相同的路由,不同的控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18892830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:27:04  来源:igfitidea点击:

Laravel same route, different controller

phpcontrollerroutinglaravellaravel-4

提问by jeremy castelli

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

感谢您的帮助

采纳答案by Rob Gordijn

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() 。

回答by jeremy castelli

ok after discussions on this platform and other forums, I come back with a compact solution

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

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

回答by Andreyco

// 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
    }

}

回答by Glad To Help

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