2

我是 Laravel 的新手,我一直在学习 laracast 上的教程,并认为我理解中间件的概念是如何工作的,但是当我尝试在另一个控制器中实现 Authenticate 时它不起作用。它只适用于我创建的第一个控制器。所以我有:

<?php

namespace App\Http\Controllers;

use App\Http\Requests\ArticleRequest;
use Carbon\Carbon;
use Request;
use Auth;

use App\Article;
use App\User;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class ArticlesController extends Controller
{

  public function __construct() {
    $this->middleware('auth',['except'=>['index','show']]);
  }

  public function index() { ... }
  public function show() { ... }
  ...
}

我的另一个控制器是:

<?php

namespace App\Http\Controllers;

use Carbon\Carbon;
use Request;
use Auth;
use App\User;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class FeedsController extends Controller
{ 

  public function __construct() {
    $this->middleware('auth',['except'=>['index']);
  }
}

我的 routes.php 文件如下所示:

Route::group(['middleware' => ['web']], function () {
  Route::resource('articles', 'ArticlesController');
  Route::resource('feeds', 'FeedsController');

  Route::controllers([
    'auth' => 'Auth\AuthController',
    'password' => 'Auth\PasswordController'
  ]);

  Route::get('/', function() {

     return view('welcome');
  });

  Route::auth();

  Route::get('/home', 'HomeController@index');

});

当我在浏览器中访问 /articles 时,我只能访问索引和显示操作,但是当我访问 /feeds 时,我可以访问所有操作。谁能告诉我哪里出错了?

顺便说一句,我正在使用 Laravel 5.2 ...

编辑:php artisan route:list

+--------+--------------------------------+--------------------------------------------------------+------------------+-----------------------------------------------------------------+-------------+
| Domain | Method                         | URI                                                    | Name             | Action                                                          | Middleware  |
+--------+--------------------------------+--------------------------------------------------------+------------------+-----------------------------------------------------------------+-------------+
|        | GET|HEAD                       | /                                                      |                  | Closure                                                         | web         |
|        | GET|HEAD                       | about                                                  |                  | App\Http\Controllers\PagesController@about                      | web         |
|        | GET|HEAD                       | articles                                               | articles.index   | App\Http\Controllers\ArticlesController@index                   | web         |
|        | POST                           | articles                                               | articles.store   | App\Http\Controllers\ArticlesController@store                   | web,auth    |
|        | GET|HEAD                       | articles/create                                        | articles.create  | App\Http\Controllers\ArticlesController@create                  | web,auth    |
|        | PUT|PATCH                      | articles/{articles}                                    | articles.update  | App\Http\Controllers\ArticlesController@update                  | web,auth    |
|        | DELETE                         | articles/{articles}                                    | articles.destroy | App\Http\Controllers\ArticlesController@destroy                 | web,auth    |
|        | GET|HEAD                       | articles/{articles}                                    | articles.show    | App\Http\Controllers\ArticlesController@show                    | web         |
|        | GET|HEAD                       | articles/{articles}/edit                               | articles.edit    | App\Http\Controllers\ArticlesController@edit                    | web,auth    |
|        | POST                           | auth/login/{one?}/{two?}/{three?}/{four?}/{five?}      |                  | App\Http\Controllers\Auth\AuthController@postLogin              | web,guest   |
|        | GET|HEAD                       | auth/login/{one?}/{two?}/{three?}/{four?}/{five?}      |                  | App\Http\Controllers\Auth\AuthController@getLogin               | web,guest   |
|        | GET|HEAD                       | auth/logout/{one?}/{two?}/{three?}/{four?}/{five?}     |                  | App\Http\Controllers\Auth\AuthController@getLogout              | web,guest   |
|        | POST                           | auth/register/{one?}/{two?}/{three?}/{four?}/{five?}   |                  | App\Http\Controllers\Auth\AuthController@postRegister           | web,guest   |
|        | GET|HEAD                       | auth/register/{one?}/{two?}/{three?}/{four?}/{five?}   |                  | App\Http\Controllers\Auth\AuthController@getRegister            | web,guest   |
|        | GET|HEAD|POST|PUT|PATCH|DELETE | auth/{_missing}                                        |                  | App\Http\Controllers\Auth\AuthController@missingMethod          | web,guest   |
|        | GET|HEAD                       | contact                                                |                  | App\Http\Controllers\PagesController@contact                    | web,student |
|        | POST                           | feeds                                                  | feeds.store      | App\Http\Controllers\FeedsController@store                      | web,auth    |
|        | GET|HEAD                       | feeds                                                  | feeds.index      | App\Http\Controllers\FeedsController@index                      | web         |
|        | GET|HEAD                       | feeds/create                                           | feeds.create     | App\Http\Controllers\FeedsController@create                     | web,auth    |
|        | DELETE                         | feeds/{feeds}                                          | feeds.destroy    | App\Http\Controllers\FeedsController@destroy                    | web,auth    |
|        | PUT|PATCH                      | feeds/{feeds}                                          | feeds.update     | App\Http\Controllers\FeedsController@update                     | web,auth    |
|        | GET|HEAD                       | feeds/{feeds}                                          | feeds.show       | App\Http\Controllers\FeedsController@show                       | web,auth    |
|        | GET|HEAD                       | feeds/{feeds}/edit                                     | feeds.edit       | App\Http\Controllers\FeedsController@edit                       | web,auth    |
|        | GET|HEAD                       | foo                                                    |                  | Closure                                                         | web,manager |
|        | GET|HEAD                       | home                                                   |                  | App\Http\Controllers\HomeController@index                       | web,auth    |
|        | GET|HEAD                       | login                                                  |                  | App\Http\Controllers\Auth\AuthController@showLoginForm          | web,guest   |
|        | POST                           | login                                                  |                  | App\Http\Controllers\Auth\AuthController@login                  | web,guest   |
|        | GET|HEAD                       | logout                                                 |                  | App\Http\Controllers\Auth\AuthController@logout                 | web         |
|        | GET|HEAD                       | password/broker/{one?}/{two?}/{three?}/{four?}/{five?} |                  | App\Http\Controllers\Auth\PasswordController@getBroker          | web,guest   |
|        | POST                           | password/email                                         |                  | App\Http\Controllers\Auth\PasswordController@sendResetLinkEmail | web,guest   |
|        | GET|HEAD                       | password/email/{one?}/{two?}/{three?}/{four?}/{five?}  |                  | App\Http\Controllers\Auth\PasswordController@getEmail           | web,guest   |
|        | POST                           | password/email/{one?}/{two?}/{three?}/{four?}/{five?}  |                  | App\Http\Controllers\Auth\PasswordController@postEmail          | web,guest   |
|        | POST                           | password/reset                                         |                  | App\Http\Controllers\Auth\PasswordController@reset              | web,guest   |
|        | POST                           | password/reset/{one?}/{two?}/{three?}/{four?}/{five?}  |                  | App\Http\Controllers\Auth\PasswordController@postReset          | web,guest   |
|        | GET|HEAD                       | password/reset/{one?}/{two?}/{three?}/{four?}/{five?}  |                  | App\Http\Controllers\Auth\PasswordController@getReset           | web,guest   |
|        | GET|HEAD                       | password/reset/{token?}                                |                  | App\Http\Controllers\Auth\PasswordController@showResetForm      | web,guest   |
|        | GET|HEAD|POST|PUT|PATCH|DELETE | password/{_missing}                                    |                  | App\Http\Controllers\Auth\PasswordController@missingMethod      | web,guest   |
|        | GET|HEAD                       | register                                               |                  | App\Http\Controllers\Auth\AuthController@showRegistrationForm   | web,guest   |
|        | POST                           | register                                               |                  | App\Http\Controllers\Auth\AuthController@register               | web,guest   |
+--------+--------------------------------+--------------------------------------------------------+------------------+-----------------------------------------------------------------+-------------+
4

0 回答 0