Laravel登录和管理面板(成功验证后)页面均为“/”

时间:2015-01-19 13:44:42

标签: session laravel-4 laravel-routing

我希望我的应用程序登录和管理面板(成功登录后访问)都可以在“/".

上访问

我使用SessionsController的create方法作为登录部分(SessionsController.php):

类SessionsController扩展\ BaseController {

/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
    if ( Auth::check() ) return Redirect::to('homeAdmin');
    //return View::make('homeLogin');
    return View::make('homeLogin');
}
// Other methods, store, destroy...

}

我的路线设置如下(routes.php):

<?php

// Home page as guest displays a login form
Route::get('/', ['as' => 'homeLogin', 'uses' => 'SessionsController@create']);

// Authentication via the SessionsControlle
Route::resource('sessions', 'SessionsController');

// Pages for logged in users
Route::group(['before' => 'auth'], function()
{
    // Home page with admin panel for authenticated users
    Route::get('/', ['as' => 'homeAdmin']);

    // Other pages from admin panel
    Route::get('manageThis', ['as' => 'manageThis']);
    Route::get('manageThat', ['as' => 'manageThat']);
});

过滤器没有改变,即(filters.php):

<?php

Route::filter('auth', function()
{
    if (Auth::guest())
    {
        if (Request::ajax())
        {
            return Response::make('Unauthorized', 401);
        }
        else
        {
            return Redirect::guest('login');
        }
    }
});

Route::filter('auth.basic', function()
{
    return Auth::basic();
});

Route::filter('guest', function()
{
    if (Auth::check()) return Redirect::to('/');
});

问题是我经常被重定向到'/ login'而不是'homeLogin'路线。我猜客户过滤器有一些变化,但是当我编辑它时,它变得更糟,并没有解决问题。

2 个答案:

答案 0 :(得分:1)

您是否尝试过将homeLogin放入访客群组?所以... ...

Route::group(['before' => 'guest'], function()
{
    Route::get('/', ['as' => 'homeLogin', 'uses' => 'SessionsController@create']);
});

否则,您可以以编程方式在控制器/路径中手动检查:

if(!Auth::user()) { 
    return View::make('guest.page'); 
}

有关详细信息,请查看以下答案:Laravel 4: Two different view pages for a single URI based on auth status

答案 1 :(得分:0)

路由应该是一个,并以编程方式检查控制器上的登录视图和仪表板视图。此过程有两条路由。

Route::get('/', ['as' => 'homeLogin', 'uses' => 'SessionsController@create']);

Route::get('/', ['as' => 'homeAdmin']); 

删除一个并在控制器上检查它,如下所示:

if(Auth::user()) { 
    return View::make('dashboard')->with('datas', $yourDatabaseQureiesForDashboard); 
}
else
{
    return View::make('login'); 
}