W
W
WebDev2016-08-02 11:20:59
Laravel
WebDev, 2016-08-02 11:20:59

Laravel how to register your service?

I read this article https://laravel-news.ru/blog/tutorials/design-patt...
It describes how to use the Service Layer pattern.
It all boils down to creating the app/Services directory and in it the Service class and accessing it in the controller.
How to connect it so that it is available in the controller is not said. I tried to repeat the example from the article, I get the error Undefined property: App\Http\Controllers\HomeController::$createCouponService
As I understand it, the new service needs to be connected somehow?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mikhail Osher, 2016-08-02
@kirill-93

Read The Following Manual .

// app/providers/AppServiceProvider.php

use App\Services\Coupon\CreateCoupon;
use App\Services\Notification;

class AppServiceProvider
{
  public function register()
  {
    // for regular services
    $this->app->bind(CreateCoupon::class);

    // for singletons
    $this->app->singleton(Notification::class);
  }
}

// app/http/controllers/CouponController

use App\Services\Coupon\CreateCoupon;

class CouponController
{
  public function create(Request $request, CreateCoupon $service)
  {
    $service->create($request->input());
  }
}

// EDIT
Additional answer for Stanislav Pochepko
Here I am currently writing a service. And there is such a question. What if my service depends on an object? I pass it in construct to the service. How to connect it in the controller in this case?
And how bad is it if another service is used inside the service? Is this a gross violation of the layers?
Complete freedom of action, but do not overdo it.
1) using an auxiliary (read - private) service, which is needed only by this class
namespace App\Services\Insurance\Ally;

use App\Contracts\InsuranceProvider;

class Provider implements InsuranceProvider
{
  public function __construct()
  {
    // Api === App\Services\Insurance\Ally\Api, совпадает namespace
    // Сервис Api нужен исключительно этому провайдеру
    $this->api = new Api();
  }

  public function getTerms($invoice)
  {
    // получаем данные с api и отдаем их
    return $this->api->getTerms($invoice);
  }
}

2) use and
namespace App\Services\FormBuilder;

// AppServiceProvider::register()
// $this->app->bind(HtmlRenderer::class);
use App\Services\Renderer\HtmlRenderer;

class Builder
{
  public function __construct(HtmlRenderer $renderer)
  {
    $this->inputFactory = new InputFactory();
    $this->renderer = $renderer;
    $this->inputs = [];
  }

  public function addInput($type, $className, $placeholder)
  {
    $this->inputs[] = $this->inputFactory->create($type, $className, $placeholder);
  }

  public function render()
  {
    return $this->renderer->render($this->inputs);
  }
}

3) service factory
// appserviceprovider
use App\Contracts\Renderer;

public function register()
{
  $this->app->register(Renderer::class, function ($params) {
    switch ($params['type']) {
      case 'html':
        return new HtmlRenderer();
      case 'xml':
        return new XmlRenderer();
    }
  });
}

// some service
use App\Contracts\Renderer;

class FormBuilder
{
   public function __construct($rendererType)
  {
    $this->renderer = app()->make(Renderer::class, ['type' => $rendererType]);
  }
}

S
Stanislav Pochepko, 2016-08-02
@DJZT

Have you entered your namespace correctly?
You should treat like this
new App\Services\MyService()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question