Problem using the API Mercure with Symfony - php

Here is the error message :
Argument 1 passed to App\Controller\PublisherController::index() must be an instance of Symfony\Component\Mercure\Publisher, instance of Symfony\Component\Mercure\Debug\TraceablePublisher given, called in /home/gw01/Etna/PLI/vendor/symfony/http-kernel/HttpKernel.php on line 145 (500 Internal Server Error)
PublisherController :
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mercure\Publisher;
use Symfony\Component\Mercure\Update;
use Symfony\Component\Routing\Annotation\Route;
/**
* Class PublisherController
* #package App\Controller
*/
class PublisherController extends AbstractController
{
/**
* #Route("/publish/{topic}", name="publisher", methods={"POST"})
*/
public function index(Publisher $publisher, $topic, Request $request)
{
$publisher(new Update($topic, $request->getContent()));
return new Response('success');
}
}
How can i fix this ?

Related

Symfony 5: controller requires that you provide a value for an argument

I have recently bought a new computer. Both old and new have xampp over Windows 10. After I have copied all my apache, mysql and php configuration files to the new one and once checked everything can run. I find an error on one of my symfony sites homepage using exactly the same files I was using previously.
Controller "App\Controller\FrontendController::index()" requires that
you provide a value for the "$locale" argument. Either the argument is
nullable and no null value has been provided, no default value has
been provided or because there is a non optional argument after this
one.
In order to check for the possible error I have created a new FrontendController class with the minimal code I needed in order to reproduce my error.
Here is the code that does not generate the error:
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class FrontendController extends AbstractController
{
/**
* #Route("/", name="index")
*/
public function index(
Request $request,
string $locale = 'es'
) {
dd($locale);
}
}
Here is the code that DOES generate the error. The only difference is that I add WhiteOctober/BreadcrumbsBundle that works like a charm in my installation on the previous computer.
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use WhiteOctober\BreadcrumbsBundle\Model\Breadcrumbs;
class FrontendController extends AbstractController
{
/**
* #Route("/", name="index")
*/
public function index(
Request $request,
string $locale = 'es',
Breadcrumbs $breadcrumbs
) {
dd($locale);
}
}
Again, when I remove $locale from the parameters list, things also work:
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use WhiteOctober\BreadcrumbsBundle\Model\Breadcrumbs;
class FrontendController extends AbstractController
{
/**
* #Route("/", name="index")
*/
public function index(
Request $request,
Breadcrumbs $breadcrumbs
) {
dd("Without locale");
}
}

Symfony 4 Invalid service "my.myform.service"

I am new to Symfony. I try to make a service from one of my classes.
When I run bin/console cache:clear I get this error:
In ResolveNamedArgumentsPass.php line 66:
Invalid service "my.myform.service": did you forget to add the "$" prefix to argument "container"?
Here is my code:
config/services.yaml:
my.myform.service:
class: App\Controller\MyformController
arguments:
container: "#service_container"
src/Controller/MessageController.php:
namespace App\Controller;
use App\Entity\Product;
use Doctrine\ORM\EntityManagerInterface;
use App\Controller\MyformController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class MessageController extends AbstractController
{
/**
* #Route("/message", name="message", methods="GET")
*/
public function index(Request $request): Response
{
//$myform = new MyformController();
//$myform->createMyform();
$this->get("my.myform.service")->createMyform();
...
src/Controller/MyformController.php:
namespace App\Controller;
use App\Entity\Myform;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class MyformController extends AbstractController
{
public function __construct($container )
{
$this->container = $container;
}
/* protected function get($service)
{
return $this->container->get($service);
}
*/
public function createMyform(): Response
{
// you can fetch the EntityManager via $this->getDoctrine()
What's wrong?
PS I commented function get() in MyformController because I had an error:
PHP Fatal error: Declaration of App\Controller\MyformController::get($service) must be compatible with Symfony\Bundle\FrameworkBundle\Controller\AbstractController::get(string $id): object in /home/admin/web/alpin52.ru/public_html/miriada/myform/src/Controller/MyformController.php on line 51
Thanks.
why do you use dedicated method for getting the service? Use the dependency injection, framework let you define the service as argument of your action method
public function index(Request $request, YourServiceClass $serviceClass): Response
{
//$myform = new MyformController();
//$myform->createMyform();
$serviceClass->doSomething();
}

Why is Symfony's autowire giving me the debug-version instance of a class? Can I bypass this to get the regular version in dev mode?

I have a problem with the autowiring of Symfony\Component\Mercure\Publisher. Instead I am being autowired Symfony\Component\Mercure\Debug\TraceablePublisher by error. How do I fix this?
Error message is:
Argument 1 passed to App\Controller\PublisherController::index() must be an instance of Symfony\Component\Mercure\Publisher, instance of Symfony\Component\Mercure\Debug\TraceablePublisher given, called in C:\xampp\htdocs\freely\vendor\symfony\http-kernel\HttpKernel.php on line 145
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mercure\Publisher;
use Symfony\Component\Mercure\Update;
use Symfony\Component\Routing\Annotation\Route;
/**
* Class PublisherController
* #package App\Controller
*/
class PublisherController extends AbstractController
{
/**
* #Route("/publish/{topic}", name="publisher", methods={"POST"})
*/
public function index(Publisher $publisher, $topic, Request $request)
{
$publisher(new Update($topic, $request->getContent()));
return new Response('success');
}
}

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());
}
}

Categories