【问题标题】:Create custom router service创建自定义路由器服务
【发布时间】:2016-02-08 12:03:15
【问题描述】:

我正在基于 Sf2 组件创建自己的框架,并尝试创建路由器服务。

我需要 generateUrl() 方法的服务

protected function generateUrl($route, $parameters = array(), $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
{
    return $this->get('router')->generate($route, $parameters, $referenceType);
}

我试试这个

$container = new ContainerBuilder();

$container->setDefinition('router_loader', new Definition('Symfony\Component\Config\Loader\LoaderInterface'));
$container->setDefinition('router', new Definition('Symfony\Component\Routing\Router', array()));

当我在我的 methodAction 中执行时

$this->generateUrl('home');

他还给我:

可捕获的致命错误:参数 1 传递给 Symfony\Component\Routing\Router::__construct() 必须是 Symfony\Component\Config\Loader\LoaderInterface,没有给出 D:\xampp\htdocs\my_fw\vendor\symfony\routing\Router.php 在第 95 行

查看路由器构造函数,我看到了。我需要那个界面

   public function __construct(LoaderInterface $loader, $resource, array $options = array(), RequestContext $context = null, LoggerInterface $logger = null)

如何避免服务中的实现?

**New update:** routing.php

use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;

// Routing
$routes = new RouteCollection();

// Home

$routes->add('home', new Route('/', array(
    '_controller' => 'MyCompany\\Controller\\HomeController::indexAction',
)));

【问题讨论】:

标签: symfony


【解决方案1】:

您遇到此错误是因为您正在配置没有服务定义的路由器服务,服务定义应该有您的服务参数,这是您的错误的根源,因为容器尝试创建没有参数的路由器check this

为了更好的使用可以在service.yml/.xml文件中配置路由服务

【讨论】:

【解决方案2】:

编辑:documentation page for the Routing component 有详细的设置说明

尝试将router_loader 服务作为参数注入router 服务。对于这种情况,您必须使用 Symfony\Component\DependencyInjection\Reference 类。

在使用Router 类中的所有内容时,必须使用配置文件配置路由。你必须使用FileLocator和一个真正的Loader类,比如YamlFileLoader,你不能只使用接口(服务在Symfony中一般不能是接口)。

router 服务的服务容器设置应如下所示:

use Symfony\Component\DependencyInjection\Reference;

// Loads config files from the current directory, change this to 
// your liking, or add more than one path
$container->setDefinition('router_config_locator', new Definition(
    'Symfony\Component\Config\FileLocator', [[__DIR__]]
));

$container->setDefinition('router_loader', new Definition(
    'Symfony\Component\DependencyInjection\Loader\YamlFileLoader', [
        new Reference('router_config_locator'),
    ]
));

$container->setDefinition('router', 
     new Definition('Symfony\Component\Routing\Router', array(
         'loader' => new Reference('router_loader'),
         // Definition of routes in Yaml form
         'resource' => 'routes.yml',
     ))
);

routes.yml 文件包含您的路由定义:

home:
  path: /home
  defaults: {_controller: "MyController:index"}

还可以查看有关setting up the routing component 的文档页面,该页面还讨论了在没有一体化类和配置文件的情况下设置路由。

【讨论】:

  • hm 这听起来不错,但我不使用配置组件。我正在使用一个文件routing.php 我将收藏放在其中。检查我的更新!我可以在没有.yml 的情况下优化它并使用默认的.php
  • @Zend 如果你想在一个路由器类中使用全部,你必须使用配置组件。如果您只想使用 .php 文件,请查看 Routing Component Documentation。那么你必须使用UrlGenerator 类。
  • 所以我需要使用配置组件。谢谢,我会在文档中查看更多内容
  • @Zend 你介意接受答案吗?我认为我的回答解决了您最初的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-15
  • 2020-10-02
  • 2021-09-28
  • 2017-02-18
  • 2018-08-10
  • 2019-09-03
  • 1970-01-01
相关资源
最近更新 更多