laravel-actions icon indicating copy to clipboard operation
laravel-actions copied to clipboard

laravel/framework 9.35+ action middleware not being resolved

Open jalt007 opened this issue 2 years ago • 12 comments

Hello!

From laravel/framework 9.35+ actions run asController have a regression in behaviour.

Controller middleware is not being resolved properly, leading to things like route model binding not working.

Due to: https://github.com/laravel/framework/commit/6cdf03e661a2a8b9b4ea2cc7d503e1d6a09bf5dc#diff-efe2a07687b9b821bbd99ae38a9329253f032e6df04bdea02bba9303a2b2beadL1084-R1103

Current workaround: make actions extend Illuminate\Routing\Controller.

Not sure how best to proceed, I'm happy using the workaround mentioned above at the moment.

jalt007 avatar Oct 13 '22 13:10 jalt007

I can confirm the same on one of my projects where I'm using a few actions asController. Thanks for the suggested workaround in the meantime, it saved me some time hunting down the root cause.

jaspertey avatar Oct 13 '22 23:10 jaspertey

😢 Thanks for raising this! I don't think there's anything Laravel Actions can do aside from making a PR to Laravel to relax that last if statement.

lorisleiva avatar Oct 14 '22 11:10 lorisleiva

Was issue created at laravel/framework ?

pm-consultancy avatar Oct 14 '22 13:10 pm-consultancy

Yes, in this PR: https://github.com/laravel/framework/pull/44516

lorisleiva avatar Oct 14 '22 13:10 lorisleiva

No, what I meant to say was. Was an issue logged with Laravel about this issue. Something like 'Can use invokable classes without extending it with Controller' or how to use HasMiddleware

pm-consultancy avatar Oct 14 '22 13:10 pm-consultancy

Oh sorry. No, not yet, I've been trying to fix it on Laravel Actions first.

I've been trying to hijack the middleware logic but I sadly end up running route middleware twice. See #200.

lorisleiva avatar Oct 14 '22 13:10 lorisleiva

I'm writing a PR for laravel/framework right now.

lorisleiva avatar Oct 14 '22 13:10 lorisleiva

I've done some debugging on my own.

My conclusions

  1. Middleware will be detected with/without the extends Controller or implements hasMiddleware
  2. using the current __invoke method will fail the SubstituteBindings Middleware, it will see ...$arguments which it can't bind
  3. using the Route::get('route', [CLASS, 'asController]` will make everything work nicely

In short the __invoke is the problem

pm-consultancy avatar Oct 14 '22 13:10 pm-consultancy

That's not quite true. The ControllerDecorator updates the action property of the route so that SubstituteBindings can work. The issue introduced by Laravel 9.35 has nothing to do with this.

lorisleiva avatar Oct 14 '22 14:10 lorisleiva

And I was so proud for finding this. Anyway, for some reason with the laravel 9.35 the route paramters in asController don't work anymore

pm-consultancy avatar Oct 14 '22 14:10 pm-consultancy

I've created a PR in laravel/framework that would fix this if it is merged. 🤞

https://github.com/laravel/framework/pull/44590

lorisleiva avatar Oct 14 '22 14:10 lorisleiva

PR merged on laravel/framework! 🎉 I still need to update the AsController trait so it includes an empty getMiddleware method for that second if statement to trigger. I'll do that as soon as I have a moment. Bear with me. 🌺

lorisleiva avatar Oct 17 '22 18:10 lorisleiva

Fix merged and released as v2.4.1. 🚀

Let me know if you have any more issues. 🙏

lorisleiva avatar Oct 20 '22 17:10 lorisleiva