Download the PHP package hivokas/laravel-handlers without Composer
On this page you can find all versions of the php package hivokas/laravel-handlers. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-handlers
Laravel Handlers
Goodbye controllers, hello request handlers!
This package adds a handy way of creating the request handlers.
Basically, the request handler is a single action controller which leads to a more clear request to the response flow.
You should try to use request handlers instead of with controllers and you probably won't feel the need to go back to controllers anytime soon.
Some advantages in comparison with controllers
- single responsibility (seems like controllers with many actions break this principle);
- testability (you won't need to resolve dependencies that are not related to the action you are testing, since there is only single action in each request handler);
- registering of routes (
Route::get('/posts/{post}', ShowPost::class)
is much more convenient and pretty thanRoute::get('/posts/{post}', 'PostController@show')
).
Installation
You can install this package via composer using this command:
The package will automatically register itself.
You can publish the config file with:
This is the contents of the published config file:
Creation of request handlers
- Create a handler
ShowPost
handler will be created
- Create handlers for all resource actions (
index
,show
,create
,store
,edit
,update
,destroy
)
IndexPost
,ShowPost
,CreatePost
,StorePost
,EditPost
,UpdatePost
,DestroyPost
handlers will be created
- Exclude unnecessary for an API actions (
create
,edit
)
IndexPost
,ShowPost
,StorePost
,UpdatePost
,DestroyPost
handlers will be created
- Create handlers by the specified actions
ShowPost
,DestroyPost
,ApprovePost
handlers will be created
- Exclude specified actions
CreatePost
,StorePost
,UpdatePost
,DestroyPost
handlers will be created
- Specify namespace for handlers creating (relative path)
IndexPost
,ShowPost
,CreatePost
,StorePost
,EditPost
,UpdatePost
,DestroyPost
handlers will be created underApp\Http\Handlers\Post
namespace inapp/Http/Handlers/Post
directory
- Specify namespace for handlers creating (absolute path)
ActivateUser
handler will be created underApp\Foo\Bar
namespace inapp/Foo/Bar
directory
- Force create
If
EditPost
handler already exists, it will be overwritten by the new one
Writing logic in request handler
Request handlers are invokable classes that use PHP's __invoke
magic function, turning them into a Callable, which allows them to be called as a function. So you need to return response in __invoke
method.
Eventually your handlers will look something like this:
Registering of routes
Here are several ways to register routes where request handlers are used as an actions.
In separate handlers.php
route file
- Create
routes/handlers.php
file (you can choose any name, it's just an example) - Define the "handlers" route group in
app/Providers/RouteServiceProvider.php
With namespace auto prefixing
Without namespace auto prefixing
In web.php
route file
-
Change the namespace for "web" group in
RouteServiceProvider.php
- Put request handlers and controllers in different route groups in
routes/web.php
file and prepend an appropriate namespace for each of them
Testing
You can run the tests with:
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
License
The MIT License (MIT). Please see License File for more information.