简体   繁体   English

Laravel 5路由相同的URL是否登录

[英]Laravel 5 route same urls login or not

I am updating a site from Laravel 4 to 5. In L4 I had this set up: 我将一个站点从Laravel 4更新到5。在L4中,我进行了以下设置:

if(Sentry::check()){ 
  Route::get('/', array('as' => 'school.home.index', 'uses' => 'school\AuthSchoolController@index'));
else{
 Route::get('/', 'school\SchoolController@index');
}

Note the same url but different controllers depending on login or not. 请注意相同的URL,但取决于登录或不登录的控制器不同。

With L5 I cannot use the middleware tried this: 对于L5,我无法使用中间件对此进行尝试:

Route::get('/', 'SchoolController@index');

Route::group(['middleware' => 'auth'], function()
{   
    Route::get('/', array('as' => 'school.home.index', 'uses' => 'AuthSchoolController@index'));
});

But this just passes over the first and goes to the group, where it gets redirected to the login page and to the admin if logged in. 但这只是越过第一个,然后转到该组,在该组中,它被重定向到登录页面和登录后的管理员。

So I think I need an if/else equivalent in the route based on login but Auth::user() for doesn't seem to work: 所以我认为我需要基于登录的路由中的if / else等效项,但是Auth :: user()似乎不起作用:

if(Auth::check()){
  Route::get('/', array('as' => 'school.home.index', 'uses' => 'AuthSchoolController@index'));
}
else{
 Route::get('/', 'SchoolController@index');
}
Route::get('/', function()
{
    if( Auth::check() ) {
        return app()->make('App\Http\Controllers\SchoolController')->callAction('index', []);
    } else {
        return app()->make('App\Http\Controllers\AuthSchoolController')->callAction('index', []);
    }    
});

Try reordering the routes like so: 尝试像这样重新排列路线:

Route::group(['middleware' => 'auth'], function()
{   
    Route::get('/', array('as' => 'school.home.index', 'uses' =>     'AuthSchoolController@index'));
});

Route::get('/', 'SchoolController@index');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM