How can call this function
$this->get('translator')->trans()
in my class namespace Di\NotificationBundle\Manager;
class SMSManager
{
private function send(){
// call $this->get('translator')->trans()
}
}
You have to define your service and inject your parameter to that service. Then it's available in your constructor.
https://symfony.com/doc/current/service_container/parameters.html
and pass the translation service to your class as dependency as example:
class SMSManager
{
public function __construct(
TranslatorInterface $translator
) {
$this->translator = $translator;
}
defined as example as:
acme_sms_manager:
class: Di\NotificationBundle\Manager\SMSManager
arguments:
- '#translator'
then simply call as:
private function send(){
this->translator->trans()
}
Hope this help
Related
I always use doctrine from controllers or from entity repository classes, now I am trying to use it from a static class but I can't find any example on how to do id.
Basically I'd need (I think) a way to create the entity manager in a static method.
thanks
M
You may call a setter function, injecting entity manager, where you call the static method:
MyController
Class MyController extends Controller
{
public function newAction()
{
$entityManager = $this->getDoctrine()->getManager();
SomeClass::setEntityManager($entityManager);
$result = SomeClass::myStaticMethod();
}
}
SomeClass
Class SomeClass
{
private static $entityManager;
public static function setEntityManager($entityManager)
{
self::$entityManager = $entityManager;
}
public static function myStaticMethod()
{
return $entityManager->getRepository(SomeEntity::class)->findAll();
}
}
I'm not sure from your question what you mean by static class/method, some code example might help. But you could declare this class as a service, which it sounds like it might be meant to be anyway, and then inject the entity manager as a dependency.
services.yml
services:
my_service:
class: Acme\AppBundle\Services\MyService
arguments: ["#doctrine.orm.entity_manager"]
Then in your class you will have the entity manager available like this:
<?php
namespace Acme\AppBundle\Services;
use Doctrine\ORM\EntityManager;
class MyService
{
/**
* Entity Manager
*
* #var Doctrine\ORM\EntityManager
*/
protected $em;
public function __construct(EntityManager $em)
{
$this->em = $em;
}
...
}
And you can then use this service in your controllers like so:
$this->get('my_service')->doSomething();
I have a function that I need a few controllers. Can i call it in some other way than to extend the class in which the function is?
Question:
class CategoryController extends Controller
{
public function getCategoriesAction()
{
$categories = $this->getDoctrine()->getRepository('ModelBundle:Category')->findAll();
return $categories;
}
}
How call this function in PostController?
You can define a service and inject entity manager in it, for retrieving data:
dummy.manager:
class: AppBundle\Model\DummyManager
arguments:
entityManager: "#doctrine.orm.entity_manager"
In your service, call findAll() method:
class DummyManager {
/** #var EntityManager */
protected $entityManager;
public function __construct($entityManager) {
$this->entityManager = $entityManager;
}
public function getCategories(){
return $this->entityManager->getRepository('ModelBundle:Category')->findAll();
}
}
And last step, in your controller:
class CategoryController extends Controller
{
public function getCategoriesAction()
{
$categories = $this->container->get('dummy.manager')->getCategories();
//...
}
}
I hope this helps.
You can also use pure PHP with traits
trait CategoryProvider {
public function getCategoriesAction() {
$categories = $this->container->get('dummy.manager')>getCategories();
//...
}
}
After that you can use this trait in your controllers
class Controller1 {
use CategoryProvider;
public function indexAction(){
//...
$this->getCategoriesAction();
//...
}
}
class Controller2 {
use CategoryProvider;
public function anotherAction(){
//...
$this->getCategoriesAction();
//...
}
}
You should not define model-style getters on a controller. The notion of a "getCategoriesAction" method for a controller should be vacated from your brain.
What you want to do is define this fetching behavior in the model layer. This can be done a bunch of different ways. Cristian just posted one way so I'll offer another.
You can define the Entity Repository itself as a service.
app/config/services.yml
services:
model.repository.category:
class: ModelBundle\Entity\CategoryRepository
factory: ["#doctrine.orm.entity_manager", getRepository]
arguments: ['ModelBundle:Category']
Then in your controller (assuming it's a container-aware controller) you can do this
$categories = $this->get('model.repository.category')->findAll();
Another approach, how #Med mentioned, is to declare your controller as service:
services:
categoryController:
class: AppBundle\Controller\CategoryController
arguments:
entityManager: #entityManager
I followed the tutorial of Fabien Potiencier, about how to create your own Framework on top of the Symfony Components. Now i need a way. And I want to inject the Dependency Container to all my Controllers, without defining every single Controller as a Service.
In the orginal Symfony2 Framework all Controllers extends the Controller Class located in Symfony\Bundle\FrameworkBundle\Controller\Controller.php:
namespace Symfony\Bundle\FrameworkBundle\Controller;
class Controller extends ContainerAware
{
// ...
}
The Controller Class extends the ControllerAware Class, so you can do something like this in your Controller:
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class MyController extends Controller
{
public function someAction()
{
$this->container->get('dependencie_xyz);
}
}
So my question is: How can I accomplish the same in my Framework?
It took me a while, but i finally figured out how the Symfony2 Framework does it.
In the SymfonyFrameworkBundle is a custom ControllerResolver, which call the setContainer Method on the resolved controller. The controller has to be a instance of the ContainerAwareInterface.
Simplified version:
class ContainerAwareControllerResolver extends ControllerResolver
{
private $container;
public __construct(ContainerInterface $container)
{
$this->container = $container;
parent::__construct();
}
public function getController(Request $request)
{
$controller = parent::getController($request);
if($controller instanceof ContainerAware ){
$controller->setContainer($this->container);
}
}
}
Source:
https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerResolver.php
It is too simply. The next code will help you
namespace Symfony\Bundle\FrameworkBundle\Controller;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
use Symfony\Component\DependencyInjection\ContainerAware as ContainerAware;
class TestService extends ContainerAware
{
public function __construct(Container $container) {
// in your example from official doc 'dependencie_xyz' is a name of service
$this->setContainer($container); // call parent setContainer() method, for identifying container variable, from now you can access to ServiceContainer using $this->container variable
$test_param = $this->container->getParameter('test_param'); // get test_param from config.yml
}
}
in service.yml
write smthing like this
services:
test_service:
class: Symfony\Bundle\FrameworkBundle\TestService
arguments: ['#service_container']
and post service container as argument
If you are not implementing any interface on controller you can add the this way and it will work. This is a small modification to c4pone implementation.
/**
* Description of ContainerAwareControllerResolver
*
* #author sbc
*/
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Controller\ControllerResolver;
class ContainerAwareControllerResolver extends ControllerResolver {
private $container;
public function __construct(LoggerInterface $logger = null, ContainerInterface $container = null) {
parent::__construct($logger);
$this->container = $container;
}
protected function instantiateController($class) {
$new_class = new $class();
$new_class->setContainer($this->container);
return $new_class;
}
The Controller Class extends the ControllerAware Class, so you can do something like this in your Controller:
Well, this is not true. If we take a look at the signature of the ContainerAware class, we see that this added a setContainer method so we can set the container. Symfony2 has created the Controller::get method to make some live easier.
We can see how they do it in the source code:
/**
* Gets a service by id.
*
* #param string $id The service id
*
* #return object The service
*/
public function get($id)
{
return $this->container->get($id);
}
You can put this in your own Controller class and let all your controllers extend that controller class.
Can somebody show me how I would inject validator into a regular class using dependency injection.
In my controller I have :
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Form;
class FormController extends Controller {
public function indexAction()
{
$form = new Form();
$email = $request->request->get('email');
$valid = $form->isValid($email);
}
}
and I want to use this custom class - but I need it got have access to validator.
class Form {
public function isValid($value)
{
// This is where I fail
$validator = $this->get('validator');
... etc
}
}
To do this, your custom class will need to be defined as a service, and you'll access it from the controller using $form = $this->get('your.form.service'); instead of instantiating it directly.
When defining your service, make sure to inject the validator service:
your.form.service:
class: Path\To\Your\Form
arguments: [#validator]
Then you'll need to handle this in the construct method of your Form service:
/**
* #var \Symfony\Component\Validator\Validator
*/
protected $validator;
function __construct(\Symfony\Component\Validator\Validator $validator)
{
$this->validator = $validator;
}
From Symfony2.5 on
Validator is called RecursiveValidator so, for injection
use Symfony\Component\Validator\Validator\RecursiveValidator;
function __construct(RecursiveValidator $validator)
{
$this->validator = $validator;
}
In Symfony 4+, if you use the default configuration (with auto wiring enabled), the easiest way is to inject a ValidatorInterface.
For instance:
<?php
use Symfony\Component\Validator\Validator\ValidatorInterface;
class MySuperClass
{
private $validator;
public function __construct(
ValidatorInterface $validator
) {
$this->validator = $validator;
}
Your Form class can inherit from ContainerAware (Symfony\Component\DependencyInjection\ContainerAware). Then you will have access to the container and you will be able to get the validator service like this:
$validator = $this->container->get('validator');
I have one class which extends \Twig_Extension like below :
class MYTwigExtension extends \Twig_Extension
{
protected $doctrine;
protected $router;
public function __construct(RegistryInterface $doctrine , $router)
{
$this->doctrine = $doctrine;
$this->router = $router;
}
public function auth_links($user , $request)
{
// Some other codes here ...
// HOW TO GENERATE $iconlink which is like '/path/to/an/image'
$html .= "<img src=\"$iconlink\" alt=\"\" /> ";
echo $html;
}
}
My question is How to generate Asset links in a Twig Extension ? I would like a replacement for ASSET helper in my class. Bassically I have no idea what I have to inject or use here ! Thanks in advance.
<img src="{{ asset('img/icons/modules/timesheet.png') }}" alt="" />
You can use the templating.helper.assets service directly.
use Symfony\Component\DependencyInjection\ContainerInterface;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
and use it like so:
$this->container->get('templating.helper.assets')->getUrl($iconlink);
Injecting just the templating.helper.assets directly does not work in this case because the twig extension cannot be in the request scope. See the documentation here: https://symfony.com/doc/2.3/cookbook/service_container/scopes.html#using-a-service-from-a-narrower-scope
I didn't want to deal with the Dependency Injection Container. This is what I did:
use Twig_Environment as Environment;
class MyTwigExtension extends \Twig_Extension
{
protected $twig;
protected $assetFunction;
public function initRuntime(Environment $twig)
{
$this->twig = $twig;
}
protected function asset($asset)
{
if (empty($this->assetFunction)) {
$this->assetFunction = $this->twig->getFunction('asset')->getCallable();
}
return call_user_func($this->assetFunction, $asset);
}
I've looked at Twig_Extension class code, and found this initRuntime method there, to be overriden in our custom Extension class. It receives the Twig_Environment as an argument! This object has a getFunction method, which returns a Twig_Function instance. We only need to pass the function name (asset, in our case).
The Twig_Function object has a getCallable method, so we finally can have a callable asset function.
I've gone a bit further creating an asset method for my own extension class. Anywhere else on it, I can simply call $this->asset() and obtain the same result as {{ asset() }} in the templates.
EDIT: The getFunction call at initRuntime throws a scope exception when clearing the cache. So I moved it to the custom asset method. It works fine.
Here's a simple and clean way for Symfony 2.8:
services.yml:
app.twig_extension:
class: Path\To\AcmeExtension
arguments:
assets: "#templating.helper.assets"
In the TWIG extension:
use Symfony\Bundle\FrameworkBundle\Templating\Helper\AssetsHelper;
class AcmeExtension
{
protected $assets;
public function __construct(AssetsHelper $assets)
{
$this->assets = $assets;
}
}
Then you can use it in any function of the extension like this:
$this->assets->getUrl('myurl');
In Symfony 5.3 that worked for me:
(just do what the assets extension does and inject Packages)
use Symfony\Component\Asset\Packages;
use Twig\Extension\AbstractExtension;
use Twig\Extension\ExtensionInterface;
class AppExtension extends AbstractExtension implements ExtensionInterface
{
public function __construct(Packages $packages)
{
$this->packages = $packages;
}
// ... your other methods
private function asset($path, $packageName = null)
{
return $this->packages->getUrl($path, $packageName);
}
}
In Symfony 2.8 that works for me:
# services.yml
services:
app.twig_extension:
class: AppBundle\Twig\AppTwigExtension
public: false
arguments:
- #templating.helper.assets
tags:
- { name: twig.extension }
AppTwigExtension class:
namespace AppBundle\Twig;
use Symfony\Bundle\FrameworkBundle\Templating\Helper\AssetsHelper;
/**
* Class AppTwigExtension
* #package AppBundle\Twig
*/
class AppTwigExtension extends \Twig_Extension
{
const IMG_PATH = 'bundles/app/images/';
private $assetsHelper;
public function __construct(AssetsHelper $assetsHelper)
{
$this->assetsHelper = $assetsHelper;
}
public function getFilters()
{
return array(
new \Twig_SimpleFilter('img', array($this, 'imagePathFilter'))
);
}
/**
* Get image path relatively to host
* Usage in Twig template: {{ 'my_image.png'|img }} - equal to
* {{ asset('bundles/app/images/my_image.png') }} in Twig template:
*
* #param string $imageName (e.g. my_image.png)
* #return string
*/
public function imagePathFilter($imageName)
{
return $this->assetsHelper->getUrl(self::IMG_PATH . $imageName);
}
public function getName()
{
return 'app_twig_extension';
}
}