使用Doctrine过滤器启用WebProfiler上的Twig错误

时间:2017-08-08 07:49:30

标签: php symfony doctrine-orm twig symfony-3.3

当我启用Doctrine过滤器时,我和Twig以及WebProfiler有一个奇怪的错误。

request.CRITICAL: Uncaught PHP Exception Twig_Error_Runtime: "An exception has been thrown 
during the rendering of a template ("Error when rendering "http://community.localhost:8000/
_profiler/e94abf?community_subdomain=community&panel=request" (Status code is 404).")." at 
/../vendor/symfony/symfony/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/
layout.html.twig line 103

{{ render(path('_profiler_search_bar', request.query.all)) }}会导致错误。

我的学说过滤器允许在某些类上添加过滤器约束(带有动态子域的多租户应用程序)

<?php

namespace AppBundle\Group\Community;

use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Query\Filter\SQLFilter;

/**
 * Class CommunityAwareFilter
 */
class CommunityAwareFilter extends SQLFilter
{
    /**
     * Gets the SQL query part to add to a query.
     *
     * @param ClassMetadata $targetEntity
     * @param string        $targetTableAlias
     *
     * @return string The constraint SQL if there is available, empty string otherwise.
     */
    public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias)
    {
        if (!$targetEntity->reflClass->implementsInterface(CommunityAwareInterface::class)) {
            return '';
        }

        return sprintf('%s.community_id = %s', $targetTableAlias, $this->getParameter('communityId')); // <-- error
        // return ''; <-- no error
    }
}

我还扩展了Symfony Router,以便在路由中自动添加子域占位符。

你知道是什么原因引起的吗?

更新

<?php

namespace AppBundle\Routing;

use AppBundle\Group\Community\CommunityResolver;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Bundle\FrameworkBundle\Routing\Router as BaseRouter;

class Router implements RouterInterface
{
    /**
     * @var BaseRouter
     */
    private $router;

    /**
     * @var RequestStack
     */
    private $request;

    /**
     * @var CommunityResolver
     */
    private $communityResolver;

    /**
     * Router constructor.
     *
     * @param BaseRouter        $router
     * @param RequestStack      $request
     * @param CommunityResolver $communityResolver
     */
    public function __construct(BaseRouter $router, RequestStack $request, CommunityResolver $communityResolver)
    {
        $this->router = $router;
        $this->request = $request;
        $this->communityResolver = $communityResolver;
    }

    /**
     * Sets the request context.
     *
     * @param RequestContext $context The context
     */
    public function setContext(RequestContext $context)
    {
        $this->router->setContext($context);
    }

    /**
     * Gets the request context.
     *
     * @return RequestContext The context
     */
    public function getContext()
    {
        return $this->router->getContext();
    }

    /**
     * Gets the RouteCollection instance associated with this Router.
     *
     * @return RouteCollection A RouteCollection instance
     */
    public function getRouteCollection()
    {
        return $this->router->getRouteCollection();
    }

    /**
     * Tries to match a URL path with a set of routes.
     *
     * If the matcher can not find information, it must throw one of the exceptions documented
     * below.
     *
     * @param string $pathinfo The path info to be parsed (raw format, i.e. not urldecoded)
     *
     * @return array An array of parameters
     *
     * @throws ResourceNotFoundException If the resource could not be found
     * @throws MethodNotAllowedException If the resource was found but the request method is not allowed
     */
    public function match($pathinfo)
    {
        return $this->router->match($pathinfo);
    }

    public function generate($name, $parameters = array(), $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
    {
        if (null !== ($community = $this->communityResolver->getCommunity())) {
            $parameters['community_subdomain'] = $community->getSubDomain();
        }

        return $this->router->generate($name, $parameters, $referenceType);
    }
}

2 个答案:

答案 0 :(得分:0)

我找到了解决方案,事实上我通过了我的&#34;租户&#34; (这里是我的&#34;社区&#34;)会话就像这样(在订阅者onKernelRequest中)

if (null === ($session = $request->getSession())) {
    $session = new Session();
    $session->start();
    $request->setSession($session);
}
$session->set('community', $community);

我更改为将此对象存储在服务中并且它可以正常工作。使用Session存储数据可能是一种不好的做法。

答案 1 :(得分:-1)

我认为您的Symmfony路由器覆盖可能会导致问题。你能把代码粘贴给我们吗?