今天我从 sf2.8 升级到 sf3.4...首先我遇到了一些错误,因为我没有重新编写 sf 标准文件,现在这些错误消失了,我在应用程序级别遇到了错误。在 sf2.8 下这不是问题,我不明白为什么现在。
错误触发点击“/”:
[Tue Oct 02 11:37:31.885382 2018] [proxy_fcgi:error] [pid 24:tid 140202295097088] [client 172.29.0.1:37964] AH01071: Got error 'PHP message: PHP Fatal error: Uncaught Symfony\\Component\\Debug\\Exception\\FatalThrowableError: Type error: Argument 2 passed to TheApp\\FrontendBundle\\Services\\UrlAliasService::__construct() must be an instance of TheApp\\FrontendBundle\\Routing\\Router, instance of Symfony\\Bundle\\FrameworkBundle\\Routing\\Router given, called in /var/www/var/cache/dev/ContainerV5bxhc0/appDevDebugProjectContainer.php on line 3678 in /var/www/src/TheApp/FrontendBundle/Services/UrlAliasService.php:25\nStack trace:\n#0 /var/www/var/cache/dev/ContainerV5bxhc0/appDevDebugProjectContainer.php(3678): TheApp\\FrontendBundle\\Services\\UrlAliasService->__construct(Object(Doctrine\\ORM\\EntityManager), Object(Symfony\\Bundle\\FrameworkBundle\\Routing\\Router), Object(Cocur\\Slugify\\Slugify))\n#1 /var/www/var/cache/dev/ContainerV5bxhc0/appDevDebugProjectContainer.php(3698): ContainerV5bxhc0\\appDevDebugProjectContainer->getTheApp_UrlaliasserviceService()\n#2 /var/www/var/cache/dev/ContainerV5bxhc0/appDevDebugProjectContainer.php(3872): Contain...\n'
所以服务“UrlAliasService”需要 3 个参数:
...
use TheApp\FrontendBundle\Routing\Router;
...
public function __construct(EntityManager $entityManager, Router $router, Slugify $slugify)
{
$this->em = $entityManager;
$this->urlRepository = $this->em->getRepository('TheAppFrontendBundle:UrlAlias');
$this->router = $router;
$this->slugify = $slugify;
$this->urlAliasInProcess = new ArrayCollection();
}
这 3 个参数在 services.yml 中连接:
theapp.urlaliasservice:
class: TheApp\FrontendBundle\Services\UrlAliasService
arguments: ["@doctrine.orm.entity_manager", "@router", "@cocur_slugify"]
这是路由器类,扩展了 BaseRouter:
namespace TheApp\FrontendBundle\Routing;
use Symfony\Bundle\FrameworkBundle\Routing\Router as BaseRouter;
...
use Symfony\Component\Routing\RequestContext;
class Router extends BaseRouter implements ContainerAwareInterface
{
private $container;
public function __construct(ContainerInterface $container, $resource, array $options = array(), RequestContext $context = null)
{
parent::__construct($container, $resource, $options, $context);
$this->setContainer($container);
}
public function getGenerator()
{
$generator = parent::getGenerator();
$generator->setContainer($this->container);
return $generator;
}
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
}
所以错误告诉我传入的参数来自类型 TheApp\FrontendBundle\Routing\Router .... 但它实际上来自类型 Symfony\Bundle\FrameworkBundle\Routing\Router .... 阅读代码这个错误没有感觉对吗?TheApp\FrontendBundle\Routing\Router 类扩展了 Symfony\Bundle\FrameworkBundle\Routing\Router 那么它怎么可能不是它的实例呢?非常感谢帮助和解释。
很明显,这@router
是一个由Symfony\Bundle\FrameworkBundle\Routing\Router
. arguments: ["@doctrine.orm.entity_manager", "@router", "@cocur_slugify"]
例如,您应该注入您的服务而不是它theapp.router
。您在这里有几个选择:
theapp.route
router
您的服务,但我不会这样做。本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句