2

please can u tell me what how to use AbstractController in symfony4.2 and how to inject services in contruct Controller?

nameSpace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;

class homeController extends AbstractController
{    
    /**
     * @return Response
     * @Route("/home", name="home")
     */


    public function index():Response
    {
        return $this->render('pages/home.html.twig');
    }

}

how to inject services in controller's constructor ?

ehymel
  • 1,360
  • 2
  • 15
  • 24
Mi Ba
  • 21
  • 3

1 Answers1

0

You should specify required service as Controller constructor argument. And then assign service to your controller parameter. For example:

public function construct(StringValidator $stringValidator) 
{
    $this->stringValidator = $stringValidator;
} 

Dont forget to declare $stringValidator in your parameters and also specify required Service namespace! In case of enabled autowiring in your services.yaml you dont even need to pass Service argument manually. That's all. You can use your service now.

Igor Shumichenko
  • 394
  • 3
  • 12