How to call a service

We have called a service in our project before. To know the answer, open mava/src/AppBundle/Controller/DashboardController.php and note two things:

<?php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class DashboardController extends Controller
{
    public function indexAction()
    {
        $uId = $this->getUser()->getId();
        $util = $this->get('mava_util');
        //...
    }
}

First, this class extends Symfony's Controller class. Secondly, spot the get(mava_util) method at line 9. This method is in charge of calling services and is defined in the Controller.php class as follows:

 /** * Gets a container service by its id. * @param string $id The service id * @return object The service */ public function get($id) ...

Get Mastering Symfony now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.