Skip to content

Conversation

@rizqyhi
Copy link
Member

@rizqyhi rizqyhi commented Aug 9, 2023

Background

Dicoding is already using ADR pattern for quite long time. This pattern maps every single route to one action controller. Currently we hard-coded the method name on the route definition, even the method name is static. Take a look below (notice the action method).

Route::get('/users', ['uses' => 'UsersIndexAction@action']);
Route::get('/users/{id}', ['uses' => 'UsersShowAction@action']);

The problem with that syntax is it's quite hard to navigate to the action class. Usually we'll block the class name (e.g. UsersIndexAction) and trigger file search feature. Imagine if we can click-follow the action classes when we reference them using ::class syntax, like:

Route::get('/users', ['uses' => UsersIndexAction::class]);
Route::get('/users/{id}', ['uses' => UsersShowAction::class]);

Solution

Laravel introduced single action controller when version 5.3 released. The base idea is using default method for the controller, which is magic method __invoke. So when a developer defines a route action without method name (or by referencing the class directly), the router will dispatch the request to __invoke method automatically.

This PR tries to backport that feature to Laravel 4.2.

References:

@rizqyhi rizqyhi self-assigned this Aug 9, 2023
@rizqyhi rizqyhi changed the title Implements invokable controller action Implements single action controller Aug 10, 2023
@rizqyhi rizqyhi merged commit 33c59bf into master Aug 14, 2023
@rizqyhi rizqyhi deleted the feature/invokable-controller branch August 14, 2023 10:17
@rizqyhi rizqyhi restored the feature/invokable-controller branch September 22, 2023 08:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants