Skip to main content
INFYNNO
INFYNNO
Infinite InnovationsInfinite Innovations
  1. Home
  2. Articles
  3. What Is The Use Of Invokable Controller In Laravel

What Is The Use Of Invokable Controller In Laravel

One controller for handling all the CRUD requests. Now this doesn’t look bad until you start defining those methods and very soon your controller becomes very fat and hard to manage and it becomes much worse if you are validating requests in the same controller instead of using separate FormRequest classes.

B

Bhavdip

Author · Infynno Solutions

Jul 28, 20201 sections
Controllers

At a glance

CategoryLaravel
Sections1 topics
AuthorBhavdip
PublishedJul 28, 2020
Start reading
What Is The Use Of Invokable Controller In Laravel

What Is The Use Of Invokable Controller In Laravel

In Laravel we are all used to using the default resource controllers which currently consist of these methods.

invokable_controller_1.png

One controller for handling all the CRUD requests. Now this doesn’t look bad until you start defining those methods and very soon your controller becomes very fat and hard to manage and it becomes much worse if you are validating requests in the same controller instead of using separate FormRequest classes.

So instead of bloating your controllers with everything we can use a different approach where we will handle each request using its very own separate invokable controller.

So let’s say that we are handling the create and store requests for creating a post in our application. Create request will serve a form to create a Post and store request add that Post in our database.

So let’s create two controllers one for handling create request and another one for handling store request. We can create our invokable controllers using the command:-

invokable_controller_2.png

Notice that I have stored both the controllers inside a PostManager directory. So all the controllers for managing the Posts will be stored in my PostManager directory.

Our controllers will look something like this:-

invokable_controller_3.png

and…

invokable_controller_4.png

Now what about this invoke function. invoke function is a type of magic method which will be called when you try to call an object as a function. So in simple words this function makes our class to be called as a function. And this is not just a Laravel specific thing it is also available in core PHP. You will understand this more clearly when you reach the end of this article. Let’s have a look at our routes which would look like this:-

invokable_controller_5.png

You can also write them like them.

invokable_controller_6.png

Notice that we haven’t specified any method like we used to before in our routes like ‘CreateController@create’. So this is what invokable will do for us it will automatically get called.

So now we can handle these request in our controllers.

CreateController :-

invokable_controller_7.png

In our CreateController we are just returning a view which contains a form for creating a Post.

StoreController :-

invokable_controller_8.png

And this is how our StoreController will look like In. It is just calling a method createPost which takes in the form data and create the post. As you can see it looks much cleaner than using a single controller for all the requests and easy to manage too. And you might have noticed that we haven’t validated our request. So for validating request you can still use the FormRequest classes or if you don’t like using them then you can also handle them in the same controller like this:-

invokable_controller_9.png

Now you shouldn’t be adding too much methods in your invokable controllers and keep it as light as possible so if your validation logic gets quite big then I would still advice you to use the <strong>FormRequest</strong> classes. Anyways give this approach a try in your next project and see if it helps you. I hope that it was helpful to you and Thanks for reading.

Are you looking for PHP Laravel Developers? Hire from us: https://infynno.com/hire-developer/

B

Bhavdip

Author · Infynno Solutions

Jul 28, 2020

Share

XLinkedIn

On this page

What Is The Use Of Invokable Controller In Laravel

Building something like this?

Free discovery call. NDA before we go further.

Book a free call
B

Bhavdip

Author

Engineer at Infynno Solutions building AI, EdTech & enterprise software products.

More from the blog

All articles
What Is Ioc Container In Laravel?
LaravelJul 27, 2020

What Is Ioc Container In Laravel?

The Laravel inversion of control container is a powerful tool for managing class dependencies. Dependency injection is a method of removing hard-coded class dependencies. Instead, the dependencies are injected at run-time, allowing for greater flexibility as dependency implementations may be swapped easily.

Bhavdip
What Is Service Container In Laravel?
LaravelJul 24, 2020

What Is Service Container In Laravel?

The Laravel service container is a powerful tool for managing class dependencies and performing dependency injection. Dependency injection is a fancy phrase that essentially means this: class dependencies are “injected” into the class via the constructor or, in some cases, “setter” methods.

Bhavdip
What’s Exciting In Laravel-7
LaravelMar 26, 2020

What’s Exciting In Laravel-7

Laravel 7 is now released and includes many new features including Laravel Airlock, better routing speed, custom Eloquent casts, Blade component tags, fluent string operations, a new HTTP client, CORS support, and many more features.

Bhavdip
Infynno

Infynno is an AI-native product engineering company helping businesses build, modernize, and scale software products through AI, automation, and experienced engineering teams.

Trusted worldwide

Google
Google
4.9
22 reviews
Clutch
Clutch
4.9
8+ reviews
Upwork
Upwork
Top Rated
8K+ Hours, 20+ Jobs
Glassdoor
Glassdoor
5
18 reviews

Trusted worldwide

Google
Google
4.9
22 reviews
Clutch
Clutch
4.9
8+ reviews
Upwork
Upwork
Top Rated
8K+ Hours, 20+ Jobs
Glassdoor
Glassdoor
5
18 reviews

Engineering Services

  • AI Strategy & Consulting
  • AI Integration
  • AI Automation & AI Agents
  • IT Consulting
  • Web Development
  • Product Development
  • AI Search Visibility (AEO + GEO)
  • Social Media and Branding

Edtech Industry Expertise

  • Learning Management Software
  • Exam Preparation Platform
  • Tutor Management Software
  • Medical Entrance Exam Platform
  • Scholarship Exam Platform
  • AI For Education
  • EdTech Mobile App Development

Company

  • About Infynno
  • Our Work
  • Client Success Stories
  • Life @ Infynno
  • Contact Us

Resources

  • Case Studies
  • Guides
  • Blogs
  • FAQs
  • Open Source & Community
  • Careers

Contact

  • Project Inquiry

    sales@infynno.com
  • HR & Careers

    hr@infynno.com
  • Phone

    +91-84888-38308

Office address

India

Ahmedabad, India

E-720 Ganesh Glory 11,

Nr. BSNL Office, Jagatpur Road,

Gota, S.G. Highway,

Ahmedabad - 382481, Gujarat

IST · UTC+5:30

Trusted worldwide

Google
Google
4.9
22 reviews
Clutch
Clutch
4.9
8+ reviews
Upwork
Upwork
Top Rated
8K+ Hours, 20+ Jobs
Glassdoor
Glassdoor
5
18 reviews
ISO 27001:2022 certified company

An ISO 27001:2022 Certified Company

  • LLP Identification Number - AAZ-7794

© 2026 Infynno Solutions LLP. All rights reserved.

Privacy PolicyTerms of UseCookie PolicySitemap
Infinite Innovations