PHP code example of zendaemon / service-layer

1. Go to this page and download the library: Download zendaemon/service-layer 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/ */

    

zendaemon / service-layer example snippets


Zendaemon\Services\ServiceLayerGeneratorServiceProvider::class,

final class SomeController extends Controller
{
    /** @var SomeService $service */
    private $service;

    public function __construct(SomeService $service)
    {
        $this->service = $service;
    }

    public function index(): ?ResourceCollection
    {
        return SomeCollection::collection($this->service->someListMethod());
    }

    public function store(StoreSomeRequest $request): ?JsonResource
    {
        return SomeResource::make($this->service->someCreationMethod($request));
    }

    public function update(UpdateSomeRequest $request, SomeModel $model): ?JsonResource
    {
        return SomeResource::make($this->service->someUpdatingMethod($request, $model));
    }

    public function destroy(SomeModel $model): JsonResponse
    {
        if (! $this->service->someDestroyMethod($model)) {
            return response()->json([
                'message' => 'Some error.',
            ], Response::HTTP_INTERNAL_SERVER_ERROR);        
        }

        return response()->json(['success' => Response::HTTP_OK]);
    }
}

namespace App\Providers;

use App\Services\LocationService;
use Illuminate\Support\ServiceProvider;

class ServiceLayerServiceProvider extends ServiceProvider
{
    /**
         * Register any application services.
         *
         * @return void
         */
        public function register()
        {
            $this->app->singleton(LocationService::class, function () {
                return new LocationService;
            });
        }
}
shell
php artisan make:service SomeService
shell
php artisan make:service SomeService --static