dongxueji2838
2015-04-11 04:10 浏览 204
已采纳

Laravel Undefined offset:1

I want to add a authentication when guest try to access the home page. I get the error when I try to access the home page.

Laravel Version : Laravel 5

The routes.php error version:

Route::get('auth/login', 'Auth\AuthController@getLogin');  
Route::post('auth/login', 'Auth\AuthController@postLogin');  
Route::get('auth/logout', 'Auth\AuthController@getLogout');
Route::get('auth/register', 'Auth\AuthController@getRegister'); 
Route::post('auth/register', 'Auth\AuthController@postRegister'); 
Route::get('/', ['middleware' => 'auth', function()
{
    Route::get('/', 'HomeController@index'); 
    Route::get('pages/{id}', 'PagesController@show');  
    Route::post('comment/store', 'CommentsController@store'); 
}]);

Route::group(['prefix' => 'admin', 'namespace' => 'Admin', 'middleware' =>     'auth'], function()  
{
  Route::get('/', 'AdminHomeController@index');
  Route::resource('pages', 'PagesController');
  Route::resource('comments', 'CommentsController');
});

Error Log

at HandleExceptions->handleError('8', 'Undefined offset: 1',   'G:\wamp\www\Laravel5\vendor\compiled.php', '7377', array('request' => object(Request), 'this' => object(Route))) in compiled.php line 7377

The correct origional version

Route::get('/', 'HomeController@index');  
Route::get('pages/{id}', 'PagesController@show');  
Route::post('comment/store', 'CommentsController@store'); 
Route::get('auth/login', 'Auth\AuthController@getLogin');  
Route::post('auth/login', 'Auth\AuthController@postLogin');  
Route::get('auth/logout', 'Auth\AuthController@getLogout');
Route::get('auth/register', 'Auth\AuthController@getRegister'); 
Route::post('auth/register', 'Auth\AuthController@postRegister'); 
Route::group(['prefix' => 'admin', 'namespace' => 'Admin', 'middleware' =>  'auth'], function()  
{
  Route::get('/', 'AdminHomeController@index');
  Route::resource('pages', 'PagesController');
  Route::resource('comments', 'CommentsController');
});

Update: I know solve the problem but don't know why? You can see the last four lines I declared a route group with authentication. And when I test that part, I had logged in. Now I log out the /admin prefix and I can see the login page when I try to access the root url.

So my idea is write a guestAuth middleware for users and a adminAuth middleware to administrators. Do you have better ideas?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答 默认 最新

  • 已采纳
    doudong1117 doudong1117 2015-04-11 04:32

    Try with -

    Route::group(['middleware' => 'auth'], function()
    {
        Route::get('/', 'HomeController@index'); 
        Route::get('pages/{id}', 'PagesController@show');  
        Route::post('comment/store', 'CommentsController@store'); 
    });
    

    Check here

    点赞 1 评论 复制链接分享
  • duannuo4620 duannuo4620 2015-04-11 04:30

    Is it this line of code?

    Route::get('/', ['middleware' => 'auth', function()
    {
        Route::get('/', 'HomeController@index'); 
        Route::get('pages/{id}', 'PagesController@show');  
        Route::post('comment/store', 'CommentsController@store'); 
    }]);
    

    Should it be?

    Route::get('/', ['middleware' => 'auth'], function()
        {
            Route::get('/', 'HomeController@index'); 
            Route::get('pages/{id}', 'PagesController@show');  
            Route::post('comment/store', 'CommentsController@store'); 
        });
    
    点赞 10 评论 复制链接分享

相关推荐