logo
Tags down

shadow

Symfony EventListener


By : bdxpro
Date : October 18 2020, 01:08 AM
will help you Thank you to all. I found the solution, in ContainerAwareCommand you have to use the service of event_dispatcher.
ClientCommand.php
code :
namespace AppBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
use AppBundle\Event\ClientEnterEvent;

class ClientCommand extends ContainerAwareCommand {
  protected function configure() { ... }
  protected function execute(InputInterface $input, OutputInterface $output) {
    $dispatcher = $this->getContainer->get('event_dispatcher');
    $dispatcher->dispatch('app.client_enter', new ClientEnterEvent("Maxi"));
}


Share : facebook icon twitter icon

Write UnitTest for Symfony EventListener


By : haidaansko
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further You can unit test your listener by mocking up all the necessary stuff it needs to work, for example, from a project of mine:
code :
class UploadListenerTest extends \PHPUnit_Framework_TestCase
{
    public function testOnUpload()
    {
        $session = new Session(new MockArraySessionStorage());
        $file = new File(__FILE__);

        $event = new PostPersistEvent($file, new EmptyResponse, new Request(), "", []);

        $listener = new UploadListener($session);
        $listener->onUpload($event);

        $tempFiles = $session->get('_temp_files');

        $this->assertCount(1, $tempFiles);
        $this->assertEquals($tempFiles[0], $file->getFilename());

        $otherFile = new File(__FILE__);

        $event = new PostPersistEvent($otherFile, new EmptyResponse, new Request(), "", []);

        $listener->onUpload($event);

        $tempFiles = $session->get('_temp_files');

        $this->assertCount(2, $tempFiles);
        $this->assertEquals($tempFiles[0], $file->getFilename());
        $this->assertEquals($tempFiles[0], $otherFile->getFilename());
    }
} 
public function testUploadNoFilesNoAjaxLoggedUser()
{
    $this->loginUser($this->getDummyUser());

    $response = $this->requestRoute(self::UPLOAD_ROUTE, "POST");

    $this->assertResponseRedirect("panel_index", $response);
}

Symfony 2 EventListener Template


By : Bogdan Lungu
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , You could create customized error pages in Symfony without the need of any event listeners
How to customize Error Pages

Symfony 2.5 / FOSUserBundle Get user id in an EventListener


By : user2198948
Date : March 29 2020, 07:55 AM
will help you In a controller you should be able since symfony 2.1 to simple use
code :
 $user = $this->getUser();
 $userId = $user->getId();
$user = $this->container->get('security.context')->getToken()->getUser();
$userId = $user->getId();
$userManager = $this->container->get('fos_user.user_manager');
$user = $userManager->findUserByUsername($this->container->get('security.context')->getToken()->getUser());
$userId = $user->getId();

Symfony EventListener How does it exactly work


By : ryz01
Date : March 29 2020, 07:55 AM
should help you out This is a huge topic and I found it hard to understand when I was learning it via Symfony documentation and other online blogs. I will recommend you to view this tutorial from KNP University and believe me you will have a very clear understanding of it.

Use PRE_SUBMIT EventListener in AbstractType Symfony 3


By : tooba noorzai
Date : March 29 2020, 07:55 AM
may help you . That's because PRE_SUBMIT is called before the data is submitted to the form. This is why $form->getData() is empty when PRE_SUBMIT is triggered. The doc says:
shadow
Privacy Policy - Terms - Contact Us © soohba.com