Laravel 未定义偏移量:1
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29574023/
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
Laravel Undefined offset: 1
提问by Peter Zhu
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
Laravel 版本:Laravel 5
The routes.php error version:
route.php 错误版本:
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.
更新:我知道可以解决问题,但不知道为什么?你可以看到最后四行我声明了一个带有身份验证的路由组。当我测试那部分时,我已经登录了。现在我注销了/admin
前缀,当我尝试访问根 url 时,我可以看到登录页面。
So my idea is write a guestAuth middleware for users and a adminAuth middleware to administrators. Do you have better ideas?
所以我的想法是为用户编写一个 guestAuth 中间件,为管理员编写一个 adminAuth 中间件。你有更好的想法吗?
采纳答案by Sougata Bose
回答by TalkingHeads
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');
});