How to get Tempating or Container in custom Service? - php

I have custom Service and I would like to use it in Twig templates.
In Symfony < 3 I can do:
use Symfony\Component\DependencyInjection\Container;
//...
public function __construct(Container $container)
{
$this->container = $container;
}
public function getView()
{
$this->container->get('templating')->render('default/view.html.twig');
}
But in Symfony 3.3 I have error:
Cannot autowire service "AppBundle\Service\ViewService": argument
"$container" of method "__construct()" references class
"Symfony\Component\DependencyInjection\Container" but no such service
exists. Try changing the type-hint to one of its parents: interface
"Psr\Container\ContainerInterface", or interface
"Symfony\Component\DependencyInjection\ContainerInterface".

It's not good idea to inject whole container. Better is to inject single dependencies:
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
class MyService
{
private $templating;
public function __construct(EngineInterface $templating)
{
$this->templating = $templating;
}
public function getView()
{
$this->templating->render('default/view.html.twig');
}
}

Related

Symfony 4 access parameters inside of repository

I have a repository class called EmailRepository
class EmailRepository extends EntityRepository implements ContainerAwareInterface { ... }
I need to get a parameter injected into this repository class but I dont know how...
This is what I currently have inside of the repository, which is being called from my controller:
Controller:
$em->getRepository(Email::class)->getEmailApi();
Repository
class EmailRepository extends EntityRepository implements ContainerAwareInterface {
protected $container;
public function setContainer(ContainerInterface $container = null) {
$this->container = $container;
}
/**
* #param $array
*/
public function getEmailApi($array)
{
echo $this->container->getParameter('email_api');
}
}
I always get this error:
Call to a member function getParameter() on null
The parameter is not null, it does have a value. I know it's telling me that $this->container is null. How do I fix this?
If I run this inside of my controller, it works fine and returns Google
echo $this->getParameter('email_api');
Inject container not a good idea. Try this
services.yaml
App\Repository\EmailRepository:
arguments:
$emailApi: '%env(EMAIL_API)%'
Repository
class EmailRepository
{
protected $emailApi;
public function __construct(string $emailApi)
{
$this->emailApi = $emailApi;
}
/**
* #param $array
*/
public function getEmailApi($array)
{
return $this->emailApi;
}
}
Or via setter injection
services.yaml
App\Repository\EmailRepository:
calls:
- method: setEmailApi
arguments:
$emailApi: '%env(EMAIL_API)%'
Repository
class EmailRepository extends EntityRepository implements ContainerAwareInterface
{
protected $emailApi;
public function setEmailApi(string $emailApi)
{
$this->emailApi = $emailApi;
}
/**
* #param $array
*/
public function getEmailApi($array)
{
return $this->emailApi;
}
}
Your original code is not going to work because there is nothing calling EmailRepository::setContainer. Furthermore, using ContainerAware and injecting the full container is discouraged.
Fortunately, the Doctrine bundle has a new base repository class that the entity manager can use to pull the repository from container and allow you to inject additional dependencies as needed. Something like:
namespace App\Repository;
use App\Entity\Email;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Symfony\Bridge\Doctrine\RegistryInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
class EmailRepository extends ServiceEntityRepository // Different class to extend from
{
private $emailApi;
public function __construct(RegistryInterface $registry, ParameterBagInterface $parameterBag)
{
parent::__construct($registry, Email::class);
$this->emailApi = $parameterBag->get('email_api');
}
So in this case we inject all the parameters and then store the ones we need.
Even injecting the parameter bag is a bit frowned upon. Better to inject individual parameters though this takes just a bit more configuration as we need to use services.yaml to explicitly inject the needed parameters:
public function __construct(RegistryInterface $registry, string $emailApi)
{
parent::__construct($registry, Email::class);
$this->emailApi = $emailApi;
}
#services.yaml
App\Repository\EmailRepository:
$emailApi: 'email_api_value'

Symfony inject service container into doctrine connection wrapper

I try to inject the symfony service container into a dcotrine dynamic connection wrapper_class
use Doctrine\DBAL\Connection;
class DynamicConnection extends Connection
{
public $container;
/**
* #required
* #param $container
*/
public function setContainer(ContainerInterface $container)
{
$this->container = $container;
}
}
I also tried to inject it with the service.yaml
App\Service\Database\DynamicConnection:
calls:
- [setContainer, ['#service_container']]
But this is also not working. How can i inject the service container here?
My goal here is to get a variable of the service container:
$this->container->get('my.string.variable')
You can do this by adding a CompilerPass. For simple CompilerPass, you can add it directly in your application Kernel class by implementing CompilerPassInterface:
class Kernel extends BaseKernel implements CompilerPassInterface
{
use MicroKernelTrait;
...
public function process(ContainerBuilder $container)
{
$container
->getDefinition('doctrine.dbal.default_connection')
->addMethodCall('setContainer', [
new Reference('service_container')
]);
}
}
Note however that as mentioned by other users, this is not a very good practice. You should inject what you need precisely instead of Container service.

How do I use the serviceLocator outside of the controller

How do I use the Zend Framework Service Locator in a Model? I have a class that I would like to use the a Table Gateway Model in. I have followed the Album example and would like to access the table outside of the controller. However if I copy and paste the code from the controller into the class I need it I get an error (undefined method :getServiceLocator()). How do I use this 'class' outside of the controller?
In the end I would like to access the functions in the " class AlbumTable" in something other then the controller (in this case another class). Thanks.
class Calendar implements ServiceLocatorAwareInterface{
protected $serviceLocator;
public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
{
$this->serviceLocator = $serviceLocator;
}
public function getServiceLocator()
{
return $this->serviceLocator;
}
/*
* Create Calendar Sync Table
*/
public function getCalendarSyncTable()
{
if (!$this->calendarSyncTable) {
$sm = $this->getServiceLocator();
$this->calendarSyncTable = $sm->get('Pro\Model\CalendarSync\CalendarSyncTable');
}
return $this->calendarSyncTable;
}
Needed to change how I called it in the controller to
$calendar = $this->getServiceLocator()>get('Pro\Model\GoogleCalendar\Calendar');
If you want to use ServiceLocator in any class, just implement ServiceLocatorAwareInterface. For example:
class SomeClass implements ServiceLocatorAwareInterface
{
protected $serviceLocator;
public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
{
$this->serviceLocator = $serviceLocator;
}
public function getServiceLocator()
{
return $this->serviceLocator;
}
ZendFramework2 will automaticaly inject instance of ServiceLocator to your class.
Read more about ServiceManager here

Using Doctrine's EntityManager in a repository design

I'm trying to implement a repository pattern in my zend framework 2 application. I have made a service
<?php
class UserService {
private $userRepository;
public function __construct(IUserRepository $repo) {
$this -> userRepository = $repo;
}
public function createUser($params) {
$this -> userRepository -> create($params);
}
public function findAllUsers() {
return $this -> userRepository -> getAllUsers();
}
}
which has a repository that implements an interface
class UserRepository implements IUserRepository {
public function getAllUsers() {
//return all users
}
public function getUserById($id) {
}
public function getOneUser($params){
}
public function getUsers($params){
}
public function create($params){
}
public function update($params){
}
public function delete($params){
}
}
<?php
interface IUserRepository {
public function getAllUsers();
public function getUserById($id);
public function getOneUser($params);
public function getUsers($params);
public function create($params);
public function update($params);
public function delete($params);
}
In my module.php I make use of dependency injection to determine which repository I inject into a controller
public function getControllerConfig() {
return array('factories' => array(
'My\Controller\Accounts' => function(){
return new AccountsController(new UserRepository());
},
),
);
}
In my controller I pass the repository to my service
class AccountsController extends AbstractActionController {
private $service;
public function __construct(IUserRepository $repo) {
$this->service = new UserService($repo);
}
public function indexAction() {
$all_users = $this->service->findAllUsers();
return new ViewModel(array('users' => $all_users));
}
}
My problem is that I'm using Doctrine as Orm and I want to use the entitymanager in my repositories but I don't know how to do that, any ideas and feedback are appreciated
There are several ways to do this, of course. The typical way you'd do this kind of thing in a ZF2/D2 project would be to start with DoctrineORMModule.
That module exposes Doctrine's EntityManager via the ZF2 Service Manager in a variety of handy ways (you can $sm->get('doctrine.entitymanager.orm_default') to explicitly get the EM instance).
Once you can get your entitymanager from the SM, you write a factory for your repository, and inject the EM.
That said, there's a cleaner way. Doctrine has built-in support for repositories, and you can extend the default implementation.
Your repository would then look like this:
<?php
use Doctrine\ORM\EntityRepository;
class UserRepository extends EntityRepository implements IUserRepository {
public function getAllUsers() {
return $this->findAll();
}
// ...
}
Just remember to add the repository class to the User Entity's metadata. For example, with an annotation:
/**
* #ORM\Entity(repositoryClass="MyDomain\Model\UserRepository")
*/
class User
{
}

PHP/Laravel - Can't initiate extend of abstract class

I'm quite new to using abstract classes and interfaces in PHP.
I'm trying to initiate a extend of an abstract class, but it won't work. It might be a Laravel specific issue i'm having.
This is the case:
I have an interface
I have an abstract class that implements the interface
I have 'regular' class that extends the abstract class
I try to implement the class
This is the interface:
<?php namespace Collection\Services\Validation;
interface SomeInterface {
public function with(array $input);
public function passes();
public function errors();
}
This is the abstract class:
<?php namespace Collection\Services\Validation;
use Illuminate\Validation\Factory;
abstract class SomeClass implements SomeInterface {
protected $validator;
protected $data = array();
protected $errors = array();
protected $rules = array();
public function __construct(Factory $validator)
{
$this->validator = $validator;
}
public function with(array $data)
{
$this->data = $data;
return $this;
}
public function passes()
{
$validator = $this->validator->make($this->data, $this->rules);
if( $validator->fails() )
{
$this->errors = $validator->messages();
return false;
}
return true;
}
public function errors()
{
return $this->errors;
}
}
This is the "regular" class:
<?php namespace Collection\Services\Validation;
class SomeClassExtender extends SomeClass {
public function sayBye()
{
return 'bye';
}
}
This is the implementation:
<?php
use Collection\Services\Validation\PageFormValidator;
use Collection\Services\Validation\SomeClassExtender;
class PagesController extends BaseController {
protected $someClass;
public function __construct(SomeClassExtender $class)
{
$this->someClass = $class;
}
And then i get this error:
Illuminate \ Container \ BindingResolutionException
Target [Symfony\Component\Translation\TranslatorInterface] is not instantiable.
If i remove the initiation of the Factory class, the error is gone. The Factory class is also just a regular class.
What am i doing wrong here?
I see that you're following Chris Fidao's book. Got the same error as you are.
This is my solution, put this inside global.php
App::bind('Symfony\Component\Translation\TranslatorInterface', function($app) {
return $app['translator'];
});
EDIT:
I think the problem with Factory is that you need to bind the translator interface to $app['translator']. Here's what I found...
If you look at the Factory class, it requires the translator interface -- A quick look into its public __construct in the API:
public function __construct(TranslatorInterface $translator, Container $container = null)
{
$this->container = $container;
$this->translator = $translator;
}
Then if you look at the public function register() in ValidationServiceProvider, you'll find that Laravel binds the TranslatorInterface to $app['translator']:
$validator = new Factory($app['translator'], $app);
Then seems like a service provider to bind $app['translator'] is needed, or we can just bind it in global.php.
I think this is the best working solution, found the same exact problem . Solved it by,
injecting the already bound "validator" object in the Validator facade.
<?php namespace Illuminate\Support\Facades;
/**
* #see \Illuminate\Validation\Factory
*/
class Validator extends Facade {
/**
* Get the registered name of the component.
*
* #return string
*/
protected static function getFacadeAccessor() { return 'validator'; }
}
Instantiate the Factory class with App::make('validator')
Do it this way,when instantiating your SomeClassExtender class.
$someClassExtender = new SomeClassExtender( App::make('validator') );
This article by #PhilipBrown Advanced Validation as a Service for Laravel 4 - http://culttt.com/2014/01/13/advanced-validation-service-laravel-4/

Categories