Laravel routes controller action declaration

Panjeh
2 min readMar 5, 2021

I’ve just installed Laravel 8 besides Laravel Breeze and in auth.php file in the routes directory, I see how we should import the namespace the of the controllers.

As you see you for declaration the store action, we have to put a bracket around the controller and the action like below

It means in Laravel 8 and above we are passing an array as the second argument for the post() method.

This is for the case that in PasswordResetLinkController controller we have more than one method.

If we have one and only one method and it is __invoke(), it is obvious that we can use the same format like above as you can see in auth.php file :

But the following format also works fine:

As you see we don’t mention the name of the method (here __invoke()) in the Rout Facade. and also removed the brackets around the controller declaration.

This is because of we are using __invoke() that is a magic method in php and is called when a script tries to call an object as a function. In our case, the object is an instance of the VerifyEmailController::class.

I would like to introduce two packages for Laravel that I have recently developed: Laravel Pay Pocket, a modern multi-wallet package, and Laravel Failed Jobs, a UI for the Laravel Failed Jobs Table. I hope they may be of help to you.

https://github.com/HPWebdeveloper/laravel-pay-pocket
https://github.com/HPWebdeveloper/laravel-failed-jobs

--

--