当我启用 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 AppBundleGroupCommunity;
use DoctrineORMMappingClassMetadata;
use DoctrineORMQueryFilterSQLFilter;
/**
* 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路由器以在路由中自动添加子域占位符。
你知道是什么会导致这种情况吗?
更新
<?php
namespace AppBundleRouting;
use AppBundleGroupCommunityCommunityResolver;
use SymfonyComponentHttpFoundationRequestStack;
use SymfonyComponentRoutingExceptionMethodNotAllowedException;
use SymfonyComponentRoutingExceptionResourceNotFoundException;
use SymfonyComponentRoutingGeneratorUrlGeneratorInterface;
use SymfonyComponentRoutingRequestContext;
use SymfonyComponentRoutingRouteCollection;
use SymfonyComponentRoutingRouterInterface;
use SymfonyBundleFrameworkBundleRoutingRouter 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);
}
}
我找到了解决方案,实际上我在会话中传递了我的"租户"(这里是我的"社区"(对象,如下所示(在订阅者中 onKernelRequest(
if (null === ($session = $request->getSession())) {
$session = new Session();
$session->start();
$request->setSession($session);
}
$session->set('community', $community);
我更改为将此对象存储在服务中,它可以工作。也许使用会话来存储数据是一种不好的做法。
我认为您的Symmfony路由器覆盖可能会导致问题。你能把代码贴给我们吗?