Using single action controllers in Laravel

  Рет қаралды 9,020

Andrew Schmelyun

Andrew Schmelyun

Күн бұрын

In this 6 minute video, I'll tell you all about Single Action Controllers in Laravel: What they are, how to use them, and why you might find a use for them in your own applications.
You'll learn:
- What single action controllers are in Laravel
- How to create an invokable controller with Artisan
- How to call a single-use controller in a route
- 0:00 Intro
- 1:18 Adding A Single Action Controller
- 3:14 Refactoring Existing Routes
- 4:56 Why Use This?
- 5:28 That's It
Send me new video ideas and vote on what's coming next: suggest.gg/aschmelyun
Follow me on Twitter! / aschmelyun
Join my newsletter, where I send out new information about twice each month in the PHP, JavaScript, and Docker realms: aschmelyun.substack.com

Пікірлер: 28
@wayz9
@wayz9 Жыл бұрын
I am doing just this (what you said at the end) - heavy business logic into the action classes, everything else not that heavy goes to the regular controller. Few months later I came back to revisit a project and instantly got familiar with (the one using this approach). The application was medium size project for assets & packages with a unique feature of uploading csv to populate records. I have found that colleagues in my company like to go even on small apps with 3 different patterns mixed together and they would copy paste someone's mess and re use it over and over... So guys please think before you act, just ask yourself is this even gonna be used ever? Matt Stauffer had a great talk on Laracon 2022 about abstracting too early, highly recommended! Anyway excellent video Andrew, keep up the good work!
@SXsoft99
@SXsoft99 9 ай бұрын
abstracting too early comes, for some, with experience there are people 10 years+ that abstract it because they read something somewhere without understanding why and there are others that recognize certain things from the start of the project and extrat those
@rahulxcr
@rahulxcr Жыл бұрын
Thanks for the video. That's very helpful.
@MedyZeus
@MedyZeus Жыл бұрын
Amazing video.. thanks 🙏🏻
@zxcvfandie
@zxcvfandie Жыл бұрын
Good use for Action classes.
@NoahNobody
@NoahNobody Жыл бұрын
I have recently started using htmx with laravel. I use these single action controllers to process the rerendering of some component, like a search results filter.
@haallefs
@haallefs 5 ай бұрын
Nice explanation! 🙂
@NathanBudd
@NathanBudd Жыл бұрын
I like the way you named your controllers here. I've recently started using Single Action Controllers, but I've been trying to use the RESTFUL naming convention, eg PostsIndexController, PostDeleteController etc, but yours is much more readable and clear as to what it does.
@Denakino
@Denakino Жыл бұрын
I have to disagree here, I think Posts/CreateNewPostsController has no valuable information over Posts/CreateController or even Posts/CreateNewController. Why namespace controllers if you repeat the namespace in the controller name? Sure, in the routes file, CreateNewPostsController::class looks nice, but so does Posts/CreateController::class
@kenamreemas3295
@kenamreemas3295 Жыл бұрын
hi, can you create a tutorial for implementing a microservice architecture with laravel?
@connorabbas6449
@connorabbas6449 Жыл бұрын
Big fan of your channel and Laracast contributions. Keep up the great work, Andrew. Also, what VS Code theme are you using?
@aschmelyun
@aschmelyun Жыл бұрын
Thanks! I'm using Ace Palenight now: marketplace.visualstudio.com/items?itemName=acestojanoski.ace-palenight
@SXsoft99
@SXsoft99 9 ай бұрын
single use controllers are nice as long as you don't want to have certain things shared, like a project specific way of logging data fro trasability purposes, queries, caching I personally prefer to: - move my validation to request classes - move my business logic either to a repository (either just quesries or multiple part execution)/action/service class - move common things like: http calls, data remaping, caching, etc to a lib/herper class - don't add the model to parameters in controllers and move the logic to somewhere i can query it (with eager loading or joins etc) with the ID as a parameter, mostly because in certain cases i want to add conditions directly in the sql query and early break the code execution if they aren't meet, instead of doing a query/multiple and check it in the code
@JohnnyBigodes
@JohnnyBigodes Жыл бұрын
I usually only use invokable controllers on routes that will only have one single method. Yes it is a design choice, but I personally dont like it.
@stojankukrika7242
@stojankukrika7242 Жыл бұрын
totally agree :)
@33sprog5min2
@33sprog5min2 Жыл бұрын
To make it more readable you should name the method on the controller
@JohnnyBigodes
@JohnnyBigodes Жыл бұрын
@@33sprog5min2 Why should you do that if you are using an invokable method? Dont get the point...
@mikulcek
@mikulcek Жыл бұрын
So, if you don't like it, why keep using it?
@nihongo_
@nihongo_ 11 ай бұрын
thank u👍
@aschmelyun
@aschmelyun 11 ай бұрын
Of course, glad to help!
@bowiemtl
@bowiemtl Жыл бұрын
I wish I saw this video 6 months ago haha
@ernestofavio6735
@ernestofavio6735 Жыл бұрын
Nice video but I dont find the advantage og this single actions.
@bulent2435
@bulent2435 Жыл бұрын
I like better multiple methods in controllers and somewhere else all bussiness logic.
@iloginu
@iloginu Жыл бұрын
Так и не понял зачем это нужно.
@lexxkrt
@lexxkrt Жыл бұрын
серьезно? в 2023 году об этом кто-то не знает еще? тема не раскрыта, зачем это нужно? это называется контроллер одного действия, зачем для CRUD использовать такой контроллер? думал что то новое покажите
@moimoi7973
@moimoi7973 Жыл бұрын
В этом нет ничего нового, но сейчас это немного модно
@phojie6868
@phojie6868 Жыл бұрын
is this recommended if your app is only having APIS ?
@phojie6868
@phojie6868 Жыл бұрын
I would rather choose APIResource + Services/Actions architecture
5 tips for supercharged Laravel Eloquent queries
16:29
Andrew Schmelyun
Рет қаралды 67 М.
Get started with Laravel events and listeners
15:37
Andrew Schmelyun
Рет қаралды 19 М.
Mama vs Son vs Daddy 😭🤣
00:13
DADDYSON SHOW
Рет қаралды 43 МЛН
Эффект Карбонаро и нестандартная коробка
01:00
История одного вокалиста
Рет қаралды 10 МЛН
Why do big Companies use Java, and NOT PHP?
12:34
Stefan Mischook
Рет қаралды 222 М.
15 Laravel Tips to Work in a Team
23:15
Laravel Daily
Рет қаралды 17 М.
When to use Traits, Interfaces, and Abstract Classes in PHP
15:08
Andrew Schmelyun
Рет қаралды 17 М.
You don't need a frontend framework
15:45
Andrew Schmelyun
Рет қаралды 116 М.
PHP is the future
34:27
Aaron Francis
Рет қаралды 171 М.
I tried 10 code editors
10:28
Fireship
Рет қаралды 2,9 МЛН
5 Laravel Relationship Tips That You Might Not Know About
8:21
Laravel Security: Top 7 Mistakes Developers Make
11:16
Laravel Daily
Рет қаралды 82 М.
Modern monoliths: SPA and API in a single codebase
11:55
Andrew Schmelyun
Рет қаралды 4,5 М.
iPhone 16 с инновационным аккумулятором
0:45
ÉЖИ АКСЁНОВ
Рет қаралды 10 МЛН
Самые крутые школьные гаджеты
0:49
8 Товаров с Алиэкспресс, о которых ты мог и не знать!
49:47
РасПаковка ДваПаковка
Рет қаралды 163 М.
Todos os modelos de smartphone
0:20
Spider Slack
Рет қаралды 63 МЛН
Xiaomi SU-7 Max 2024 - Самый быстрый мобильник
32:11
Клубный сервис
Рет қаралды 523 М.