takeoo/laravel-service-layer

Lightweight service layer for laravel application!
316 15
Install
composer require takeoo/laravel-service-layer
Latest Version:v0.2.2
PHP:>=7.0.0
Last Updated:Nov 27, 2020
Links: GitHub  ·  Packagist
Maintainer: TakeoO

Service Layer for laravel framework

Extra simple, lightweight service manager module for laravel app

Installation

  • Require repository
composer require takeoo/laravel-service-layer
  • Add \Takeoo\Service\TakeooServiceServiceProvider::class to config/app.php "providers" array

  • Run:

  php artisan vendor:publish      

Usage

  • Create new service class (Example.php) anywhere in your project:

//Example.php


namespace  My\Service\Namespace;


use Takeoo\Service;

class MyService extends Service
{
// your code 
}

or if you do not want to extend Service.php just use Service trait;


//Example.php


namespace  My\Service\Namespace;


use Takeoo\Service\Traits;

class MyService 
{
 use Service;
// your code 
}

  • when you created new service class, you have to register it:
  • go to config/service.php
  • add your service to "services" array:

'services' => [
  'Example' => \My\Service\Namespace\Example::class,
]
  • Add Service trait to your Controller.php class (if you extend it with all your controllers) or to every controller class in which you want to use Service layer

By default all services are created as singletons, if you want to create non singleton class, provided its alias in "service.non-singleton" array

Code

In code you can call your service as:


$service = $this->getService("Example");
             

if you want to use autocomplete (tested in JetBrains IDE) add PHPDoc above variable


/**
 * @var \My\Service\Namespace\Example $serivce
 */
$service = $this->getService("Example");
             

or you can always create helper functions for your commonly used services e.g:


/**
 * return \My\Service\Namespace\Example
 */
 public function getExampleService()
 {
  return $this->getService("Example");
 }             

Related Packages

sdclub/laravel-image

laravel-image 是一个辅助服务,处理上传的图像和存储图像没有重复.

26 1
enzyme/laravel-binder

A service container contextual binding helper for Laravel 5.

25 1
okaybueno/validation

Validation as Service for Laravel applications.

4,797 0
sands/asasi-service

Asasi model service

128 0