dongtang5057
2017-12-24 14:36
浏览 60
已采纳

Symfony 4内核控制器事件监听器 - 实现接口

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

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • drflkphi675447 2017-12-24 14:46
    已采纳

    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');
        }
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报