Commit f0378061 authored by Fabien Potencier's avatar Fabien Potencier

minor #1382 Included extend() explanation in services documentation chapter (hvt)

This PR was squashed before being merged into the 2.2.x-dev branch (closes #1382).

Discussion
----------

Included extend() explanation in services documentation chapter

Together with the Twig add global and filter example.

Commits
-------

65d6d344 One change too many
124654fc Some typo fixes
7d1e96c5 Included extend() explanation in services documentation chapter
parents 9e388b22 65d6d344
......@@ -166,6 +166,31 @@ However, you can inject it via `use($app)`::
// ...
});
Modify services after definition
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sometimes you want to alter a service after its definition. Pimple facilitates
this by extending the already defined service.
First argument of the ``extend`` method is the name of the service you want to
modify. Second argument is a callable. This callable is executed with the service
you want to alter as its first argument, the service container itself is provided
in the second argument.
.. note::
Be sure to return the modified service in the callable.
You can use this pattern to add functionality to :doc:Twig <providers/twig> for
example::
$app->extend('twig', function($twig, $app) {
$twig->addGlobal('pi', 3.14);
$twig->addFilter('levenshtein', new \Twig_Filter_Function('levenshtein'));
return $twig;
});
Core services
-------------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment