Symfony doesn't load my custom validator defined as service - php

I'm trying to inject EntityManagerInterface in my custom validator, but I have this error:
Attempted to load class "validator_check_client_id" from the global namespace.
Did you forget a "use" statement?
My code is:
CheckClientId.php
namespace App\Validator;
use Symfony\Component\Validator\Constraint;
/**
* #Annotation
*/
class CheckClientId extends Constraint{
public $message = 'Test';
public function validatedBy(){
return 'validator_check_client_id';
}
}
CheckClientIdValidator.php
namespace App\Validator;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class CheckClientIdValidator extends ConstraintValidator{
private $entityManager;
public function __construct(EntityManagerInterface $entityManager) {
$this->entityManager = $entityManager;
}
public function validate($value, Constraint $constraint){
//Todo
return;
}
}
services.yaml
validator.check_client_id:
class: App\Validator\CheckClientIdValidator
autowire: false
arguments:
$entityManager: '#doctrine.orm.entity_manager'
tags:
- {
name: validator.constraint_validator,
alias: validator_check_client_id,
}
Should I do an extra step to register my service? I think that symfony cannot find it because it is not properly registered.
I also tried to do what this answer says symfony validator as service not working, but I get a similar error:
Attempted to load class "app.validator.blog.slug_unique" from the global namespace.
Did you forget a "use" statement?
Do you have any idea what is happening?
Thank you for your help!

Thanks to autowire and autoconfig, all you really have to do is to get your class names right. No changes to services.yaml. Here is a working example using a fresh 5.2 project.
# src/Validator/CheckClientId.php
namespace App\Validator;
use Symfony\Component\Validator\Constraint;
/** #Annotation */
class CheckClientId extends Constraint
{
public $message = 'CheckClientId';
}
# =============================
# src/Validator/CheckClientIdValidator
namespace App\Validator;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedValueException;
class CheckClientIdValidator extends ConstraintValidator
{
public function __construct(private EntityManagerInterface $entityManager)
{
}
public function validate($value, Constraint $constraint)
{
dump('validate ' . $value);
throw new UnexpectedValueException($value, 'string');
}
}
# ===============================
# Test Entity
# src/Entity/Client.php
namespace App\Entity;
use App\Validator as AcmeAssert;
class Client
{
/** #AcmeAssert\CheckClientId */
public $id = 42;
}
# ====================================
# Test Command
# src/Command/ValidateCommand
namespace App\Command;
use App\Entity\Client;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
class ValidateCommand extends Command
{
protected static $defaultName = 'test:validator';
public function __construct(private ValidatorInterface $validator)
{
parent::__construct();
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$client = new Client();
$errors = $this->validator->validate($client);
echo $errors;
return Command::SUCCESS;
}
}
Get the test command working and move on from there.

Related

Injecting Services into a Service

I am trying to inject public services like entityManager in the constructor of a service I created but I keep having this error :
Too few arguments to function App\Services\BillingInterface::__construct(), 0 passed in /var/www/.../src/Controller/TestController.php on line 144 and exactly 1 expected.
In my controllers, services are correctly injected in different methods but in the service I created it's not injected in the constructor.
I didn't change anything in the services.yaml as the documentation says autowire is automatic in Symfony 4.2
PS : I recently updated from Symfony 4.1 to 4.2 and I'm not sure but I think it worked before.
Maybe a library didn't updated correctly but I don't find any errors.
Here are the informations for the service
Service code :
#/src/Services/BillingInterface
namespace App\Services;
use Doctrine\ORM\EntityManagerInterface;
class BillingInterface {
private $em;
public function __construct(EntityManagerInterface $entityManager)
{
$this->em = $entityManager;
}
}
Controller code :
namespace App\Controller;
use App\Services\BillingInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class TestController extends AbstractController {
public function teest(EntityManagerInterface $entityManager)
{
$billing = new BillingInterface();
}
}
And If I instantiate BillingInterface with $entityManager parameter of Controller, it works but I would like it injected directly in the BillingInterface class constructor.
And finally, here is what is written in Symfony's documentation :
// src/Service/MessageGenerator.php
// ...
use Psr\Log\LoggerInterface;
class MessageGenerator
{
private $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
public function getHappyMessage()
{
$this->logger->info('About to find a happy message!');
// ...
}
}
Link : https://symfony.com/doc/current/service_container.html
Chapter : Injecting Services/Config into a Service
So, I don't know what's wrong with my Service.
Thank you for your answers.
Since your BillingInterface is a service - you need to use its instance that is provided by Symfony container instead of attempting to instantiate it by yourself. Your controller needs to inject this service in order to be able to use it:
namespace App\Controller;
use App\Services\BillingInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class TestController extends AbstractController
{
/**
* #var BillingInterface
*/
private $billing;
/**
* #param BillingInterface $billing
*/
public function __construct(BillingInterface $billing)
{
$this->billing = $billing;
}
public function teest(EntityManagerInterface $entityManager)
{
// Use $this->billing ...
}
}

Symfony 4 kernel controller event listener - implement interface

I want to make some operations before controller load and I have problem with include interfaces or classes into function.
My question is how should I do it to start working?
There is a code:
~/src/Controller/ControllerListener.php
<?php
namespace App\EventListener;
use App\Controller\DailyWinController;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
class ControllerListener implements DailyWinController
{
public function onKernelController(FilterControllerEvent $event, LoggerInterface $logger) {
$logger->alert('Working');
}
}
~/src/Controller/DailyWinController.php
<?php
namespace App\Controller;
interface DailyWinController {
// maybe there something?
}
~/src/Controller/UserController.php
<?php
namespace App\Controller;
use App\Entity\User;
use App\Entity\DailyWin;
use Psr\Log\LoggerInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
class UserController extends Controller implements DailyWinController
{
/**
* #Route("/user", name="user")
* #param AuthorizationCheckerInterface $authChecker
* #param UserInterface $user
* #return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*/
public function user(AuthorizationCheckerInterface $authChecker, UserInterface $user = null, LoggerInterface $logger) {
if ($authChecker->isGranted('ROLE_USER') === false) {
return $this->redirectToRoute('logowanie');
}
$logger->warning('Logger is working');
$em = $this->getDoctrine()->getManager();
$DWrep = $em->getRepository(DailyWin::class);
$userId = $user->getId();
$dailyWin = $DWrep->findOneBy(['userId' => $userId]);
return $this->render('andprize/user/index.html.twig', array(
'dailyWin' => $dailyWin,
'userId' => $userId
));
}
}
I have the following problem:
FatalThrowableError Type error: Argument 2 passed to
App\EventListener\ControllerListener::onKernelController() must
implement interface Psr\Log\LoggerInterface, string given
You have to inject the logger to the listener.
<?php
namespace App\EventListener;
use App\Controller\DailyWinController;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
class ControllerListener implements DailyWinController
{
protected $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger=$logger;
}
public function onKernelController(FilterControllerEvent $event) {
$this->logger->alert('Working');
}
}

Symfony 3.1 / Doctrine, Repository or include path wrong

I got this error:
Warning: Missing argument 1 for Doctrine\ORM\EntityRepository::__construct()
I used phpstorm for coding and the line in TestController.php
new ProductRepository();
is underlined with a message:
Required parameter $em missing less.
Invocation parameter types are not compatible with declared.
But I don't use the $em parameter yet.
I use 3 files:
AppBundle
|__Controller
| |__ TestController.php
|__Entity
|_______ Product.php
|_______ ProductRepository.php
TestController.php:
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Doctrine\ORM\EntityRepository;
use AppBundle\Entity\ProductRepository;
use Symfony\Component\HttpFoundation\Response;
class TestController extends Controller
{
/**
* #Route("/test", name="test")
*/
public function indexAction()
{
$pr = new ProductRepository();
return new Response('OK '.$pr->test());
}
}
Product.php:
<?php
// src/AppBundle/Entity/Product.php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="product")
* #ORM\Entity(repositoryClass="AppBundle\Entity\ProductRepository")
*/
class Product
{ /* ......CODE ...*/}
ProductRepository.php:
namespace AppBundle\Entity;
use Doctrine\ORM\EntityRepository;
class ProductRepository extends EntityRepository
{
public function test()
{
return 'hello';
}
}
Get repository through doctrine service, controller returns doctrine service by getDoctrine method
public function indexAction()
{
$pr = $this->getDoctrine()->getRepository('AppBundle:Product');
return new Response('OK '.$pr->test());
}
You shouldn't create repositories directly. Use EntityManager for this purpose. You can try the following code:
class TestController extends Controller
{
private $entityManager;
public function __construct(\Doctrine\ORM\EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}
/**
* #Route("/test", name="test")
*/
public function indexAction()
{
$pr = $this->entityManager->getRepository('AppBundle\Entity\ProductRepository');
return new Response('OK '.$pr->test());
}
}

Can't load class by dependency injection in Symfony2

I'm trying to use dependenc injection in Symfony, but it doesnt work for mee :-(
I have this controller:
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
/**
* Class UsersController
*/
class UsersController extends Controller
{
....
public function indexAction(){
....
$userManager = $this->get('user_manager');
$userManager->createUser();
...
}
....
}
This want this class to inject:
<?php
namespace AppBundle\DI;
use AppBundle\Entity\User;
use Doctrine\ORM\EntityManager;
class UserManager
{
protected $em;
public function __construct(EntityManager $entityManager){
$this->em = $entityManager;
}
public function createUser(){
$user = new User();
....
$this->em->persist($user);
$this->em->flush();
}
}
My services.yml
services:
user_manager:
class: AppBundle\DI\UserManager
arguments: ["#doctrine.orm.entity_manager"]
But i still have this error:
Attempted to load class "UserManager" from namespace "AppBundle\DI".
Did you forget a "use" statement for another namespace?
Could someone advise me, where is a mistake? I'm starting with symfony...
Thanks.

Laravel 5 repository injection

I'm quite new in Laravel 5, what I am trying to do is a simple repository with dependency injection. But I'm stuck with this error:
Argument 1 passed to
App\Http\Controllers\Api\UserController::__construct() must implement
interface App\Repositories\UserInterface, instance of
App\Repositories\UserRepository given
Here is my code:
UserController:
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Response;
use App;
use Auth;
use Crypt;
use Lang;
use Image;
use Storage;
use Config;
use Validator;
use App\User;
use App\Repositories\UserInterface;
class UserController extends Controller
{
protected $config;
protected $users;
public function __construct(UserInterface $users)
{
$this->middleware('api');
$this->middleware('auth', ['except' => 'getInfo']);
$this->users = $users;
$this->config = Config::get('images.avatar');
}
UserInterface:
namespace App\Repositories;
use App\Repositories\BaseInterface;
interface UserInterface extends BaseInterface
{
};
BaseInterface:
namespace App\Repositories;
interface BaseInterface
{
public function all();
public function paginate($count);
public function find($id);
}
BaseRepository
namespace App\Repositories;
use App\Repositories\BaseInterface;
class BaseRepository implements BaseInterface
{
protected $model;
public function __call($name, $args)
{
// $this->getNewInstance()->{$name($args)};
return call_user_func_array([
$this->getNewInstance(),
$method], $args);
}
public function all($relations = [])
{
$instance = $this->getNewInstance();
return $instance->with($relations)->all();
}
public function find($id, $relations = [])
{
$instance = $this->getNewInstance();
return $instance->with($relations)->find($id);
}
public function findOrFail($id, $relations = [])
{
$instance = $this->getNewInstance();
return $instance->with($relations)->findOrFail($id);
}
public function paginate($count)
{
}
protected function getNewInstance()
{
return new $this->model;
}
}
UserRepository
namespace App\Repositories;
use App\Repositories\BaseRepository;
Class UserRepository extends BaseRepository
{
protected $model = 'App\User';
}
RepositoryServiceProvider
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App;
class RepositoryServiceProvider extends ServiceProvider
{
/**
* Register any error handlers.
*
* #return void
*/
public function boot()
{
}
/**
* Register the service provider.
*
* #return void
*/
public function register()
{
//
App::bind('App\Repositories\UserInterface', 'App\Repositories\UserRepository');
}
}
Of course RepositoryServiceProvider is added under service providers in my config/app.php
Please help, I'm almost sure that I've tried everything whatever I found in Google.
Your UserRepository has to implement UserInterface:
namespace App\Repositories;
use App\Repositories\BaseRepository;
class UserRepository extends BaseRepository implements UserInterface
// ^^^^^^^^^^^^^^^^^^^^^^^^
{
protected $model = 'App\User';
}

Categories