Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Single Action Controller in Laravel
Aug 20, 2018 @ 14:10:38

On the CodeWall blog they've posted a tutorial showing how to create a single action controller in a Laravel-based application to isolate functionality that can be called directly with no method required.

There are some magical functions in PHP, and __invoke is one of them.

Through this __invoke method, we can create classes with just one function __invoke in them, and whenever their object is called it will directly call the __invoke method, it means you don’t have to manually say $obj->someFunction() .

The post starts by talking about the Single Responsibility Principle (SRP) and how having an isolated controller with just an __invoke method. It then walks you through the implementation and how to use a controller like this. In their example they show how to use it to create "post" records (like for a blog). The post also includes some of the drawbacks of using this kind of controller including the fact that you cannot use resource routing with it.

tagged: controller laravel singleaction invoke tutorial

Link: https://www.codewall.co.uk/single-action-controller-in-laravel/

Trending Topics: