mercredi 4 décembre 2019

How to register service through DI on Laravel?

How to register any service ex: RequestStack to be used in any other services with current state in laravel?

If I inject and push request to request stack in one service, I cannot access it in other service (RequestStack builds anew)

<?php

namespace App\Services;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

class CustomClass
{
    /**
     * @var RequestStack
     */
    private $requestStack;

    public function __construct(RequestStack $requestStack)
    {
        $this->requestStack = $requestStack;
    }

    public function createRequest()
    {
        $this->requestStack->push(new Request());
    }
}
<?php

namespace App\Services;

use Symfony\Component\HttpFoundation\RequestStack;

class CustomClass2
{
    /**
     * @var RequestStack
     */
    private $requestStack;

    public function __construct(RequestStack $requestStack)
    {
        $this->requestStack = $requestStack;
    }

    public function getRequest()
    {
        // Empty
        $request = $this->requestStack->getMasterRequest();
    }
}

How to register RequestStack globally, so I can access any modifications by any other service?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire