PHP code example of hivokas / laravel-handlers

1. Go to this page and download the library: Download hivokas/laravel-handlers library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hivokas / laravel-handlers example snippets


return [

    /*
    |--------------------------------------------------------------------------
    | Base Handler Class
    |--------------------------------------------------------------------------
    |
    | Here you may specify which class will be used as the base class
    | for all generated handlers.
    |
    */

    'base' => Hivokas\LaravelHandlers\Handler::class,

];



namespace App\Http\Handlers\Foo;

use App\Models\Foo;
use App\Services\FooService;
use Hivokas\LaravelHandlers\Handler;
use App\Http\Requests\Foo\UpdateFooRequest;

class UpdateFoo extends Handler
{
    protected $service;
    
    public function __construct(FooService $service)
    {
        $this->service = $service;
    }
    
    public function __invoke(UpdateFooRequest $request, Foo $foo)
    {
        $this->bar($foo, $request->validated());
        
        return redirect('foo.show', $foo);
    }

    protected function bar(Foo $foo, array $validated)
    {
        return $this->service->baz($foo, $validated);
    }
}

// app/Providers/RouteServiceProvider.php

protected function mapHandlersRoutes()
{
    Route::middleware('web')
         ->namespace('App\Http\Handlers')
         ->group(base_path('routes/handlers.php'));
}

// app/Providers/RouteServiceProvider.php

public function map()
{
    $this->mapApiRoutes();

    $this->mapWebRoutes();
    
    $this->mapHandlersRoutes();

    //
}

// routes/handlers.php

Route::get('/post/{post}', 'ShowPost');

// app/Providers/RouteServiceProvider.php

protected function mapHandlersRoutes()
{
    Route::middleware('web')
         ->group(base_path('routes/handlers.php'));
}

// app/Providers/RouteServiceProvider.php

public function map()
{
    $this->mapApiRoutes();

    $this->mapWebRoutes();
    
    $this->mapHandlersRoutes();

    //
}

// routes/handlers.php

use App\Handlers\ShowPost;

Route::get('/post/{post}', ShowPost::class); // pretty sweet, isn't it? 😍

// app/Providers/RouteServiceProvider.php

protected function mapWebRoutes()
{
    Route::middleware('web')
         ->namespace('App\Http') // pay attention here
         ->group(base_path('routes/web.php'));
}

// routes/web.php

Route::group(['namespace' => 'Handlers'], function () {
    Route::get('/posts/{post}', 'ShowPost');
    Route::delete('/posts/{post}', 'DestroyPost');
});

Route::group(['namespace' => 'Controllers'], function () {
    Route::get('/users', 'UserController@index');
    Route::get('/users/{user}', 'UserController@show');
});
bash
php artisan vendor:publish --provider="Hivokas\LaravelHandlers\Providers\HandlersServiceProvider" --tag="config"
bash
php artisan make:handler ShowPost
bash
php artisan make:handler Post --resource
bash
php artisan make:handler Post --resource --api
bash
php artisan make:handler Post --actions=show,destroy,approve
bash
php artisan make:handler Post --resource --namespace=Post
bash
php artisan make:handler ActivateUser --namespace=\\App\\Foo\\Bar
bash
php artisan make:handler EditPost --force