简体   繁体   English

Laravel同样的路线,不同的控制器

[英]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.

相关问题 Laravel不同路线相同控制器 - Laravel Different Route Same Controller Laravel 为相同的资源路由调用不同的 controller - Laravel call different controller for same resource route 2个按钮使用Laravel在同一控制器中以相同的路径访问不同的方法 - 2 button accessing different method in the same controller with the same route using Laravel 如何在Laravel中使用具有不同控制器功能和相同路径的不同形式 - How to use different form with different controller functions and the same route in Laravel 相同的路由但在Laravel 5.1路由中调用不同的控制器 - Same route but call different controller in Laravel 5.1 routing 根据Laravel中的用户角色,对同一路径使用不同的控制器 - Use different controller for same route based on user role in Laravel Laravel 5.2根据条件将同一路径分配给不同的控制器动作 - Laravel 5.2 assigning same route to different controller action by conditions laravel如何路由到同一控制器中的另一个功能 - laravel how to route to another function in the same controller Laravel同一控制器只能在一个路径上工作,而不能与另一路径一起工作 - Laravel same controller work with one route and not with another Laravel路由传递变量到控制器不一样 - Laravel route pass variable to controller not same
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM