[5.2] Fix missing middleware parameters when using authorizeResource() #14592
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This fixes an issue with the
authorizeResource
method in theAuthorizesResources
trait. Previously, the middleware would not be properly applied to some of the controller's methods.As an example, say you have a
PostsController
class that usesauthorizeResource
as follows:Let's say this controller is registered as a resource:
Running
php artisan route:list
will reveal that the controller'screate
andedit
methods do not have the proper middleware applied:This happens because
authorizeResource
repeatedly calls$this->middleware()
(here), which replaces the entry in the middleware each time it is called (here).This was missed by the unit tests for this method because the tests only verified the presence of middleware by name -- the tests didn't make sure the middleware was actually being applied to the correct methods.
After applying this fix, the routing table will appear as follows:
The same issue appears in Laravel 5.3, but I am guessing the middleware names applied in this method might be changing since the default Policy stub for resources includes methods like
viewAny
in addition toview
.