framework icon indicating copy to clipboard operation
framework copied to clipboard

can() method does not work for route groups

Open marian-r opened this issue 8 months ago • 7 comments

Laravel Version

11.44.2

PHP Version

8.3

Database Driver & Version

No response

Description

Attaching Authorization middleware does not work for route groups using can() method, only via middleware('can...').

This does not work:

Route::prefix('{post}')
    ->can('access', 'post') // Middleware not registered.
    ->group(static function (): void {
        Route::get('/', 'show')
        ...
    });

Working example:

Route::prefix('{post}')
    ->middleware('can:access,post') // Middleware registered fine.
    ->group(static function (): void {
        Route::get('/', 'show')
        ...
    });

Steps To Reproduce

Route::prefix('{post}')
    ->can('access', 'post') // Middleware not registered.
    ->group(static function (): void {
        Route::get('/', 'show')
        ...
    });

marian-r avatar Mar 21 '25 09:03 marian-r

Can you point where exactly this is available in Laravel documentation?

crynobone avatar Mar 21 '25 12:03 crynobone

Sure: https://laravel.com/docs/11.x/authorization#via-middleware. The example shows usage on a single route without using groups, however the can() method is available on the RouteRegistrar, so IDE autocompletes it.

marian-r avatar Mar 21 '25 12:03 marian-r

Thank you for reporting this issue!

As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.

If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team.

Thank you!

github-actions[bot] avatar Mar 21 '25 13:03 github-actions[bot]

route::livewire does not work messege error attribute livewire does not exist

emanohasan avatar Mar 26 '25 10:03 emanohasan

Hi, can I work on this?

rohDevellopper avatar Mar 31 '25 19:03 rohDevellopper

@rohDevellopper Feel free to open a PR to fix this! Note that there was an attempt to resolve the problem in #55135, but it was rejected. So maybe try a different approach?

AndrewMast avatar Apr 10 '25 14:04 AndrewMast

I think the middleware needs to be applied correctly. I'll put it like this: Route::prefix('{post}') ->middleware('can:access,post') // Apply the middleware correctly ->group(function (): void { Route::get('/', 'show'); // Other routes... });

rohDevellopper avatar Apr 10 '25 16:04 rohDevellopper