Skip to Content
Mastering Symfony
book

Mastering Symfony

by Sohail Salehi
April 2016
Intermediate to advanced content levelIntermediate to advanced
290 pages
5h 51m
English
Packt Publishing
Content preview from Mastering Symfony

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) ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Persistence in PHP with Doctrine ORM

Persistence in PHP with Doctrine ORM

Kevin Dunglas
PHP Web Services

PHP Web Services

Lorna Jane Mitchell
Mastering PHP 7

Mastering PHP 7

Branko Ajzele
PHP 7: Real World Application Development

PHP 7: Real World Application Development

Doug Bierer, Altaf Hussain, Branko Ajzele

Publisher Resources

ISBN: 9781784390310Supplemental Content