Laravel Tip: Route-Model Binding and Invokable Controller

preview_player
Показать описание
A quick video discussing a code snippet from an open-source project.

- - - - -
Support the channel by checking out my products:

- - - - -
Other places to follow:
Рекомендации по теме
Комментарии
Автор

I love invokable controllers, I feel they are clean and self descriptive.

alanimohammad
Автор

Yes I use invokable controllers majority of the time for putting a complex logic which tends to have high cyclomatic complexity and gives high CRAP index.

shahsawoodshinwari
Автор

Personally I don't because if I ever want to add more methods later, it becomes a bit of a mess

Instead I personally just use a resource route, define it with "only" and the methods I'm currently using, and go from there when I want to add more

JamesAutoDude
Автор

For me, it is clear to add a name to the method. If in the future you need to add another method to the controller, you'll have to specify a name for each one.
Also, I prefer to put "formatHours" in the model, for me is a calculated field, I name it "niceOfficeHours", some times used in blade files to show it prettier...

PabloZagni
Автор

Don't forget that you can customize the retriever of the model, after route model binding, like if you want to get it from the cache (my case)

giacomogaravaglia
Автор

Nowadays I am building an app with livewire, so no controller for now

shubhamsahuSD
Автор

Would this essentially be similar to a callback in the routes web file? Essentially a way to drag that logic into a controller and keep the routes file cleaner? I like the idea a lot if that is the case.

riley.shannon
Автор

Is there some approach in which a single object, like a DTO, will be passed to the view, so that the view knows one exact structure and does not have to guess which variables exist and which do not?

r.chitector
Автор

I use invokable controllers a lot. Not a fan of the default CRUD controllers. This is because I try to make my pages more domain-oriented rather than having a simple table or form with a billion inputs with no obvious purpose

spicynoodle
Автор

I want to understand the route model binding part

The {user:slug} part are you saying it'll check the database if that slug exists or what exactly is happening there

codesmiles_
Автор

I don't see the benefit. The typical way of doing routing and controller is easier to reason about in a glance.

genechristiansomoza