Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Routing 在特定包中查找路由_Routing_Symfony - Fatal编程技术网

Routing 在特定包中查找路由

Routing 在特定包中查找路由,routing,symfony,Routing,Symfony,我正在使用symfony 2实现一个web应用程序。它有一个主页,该主页通过一个“CoreBundle”提供服务,该“CoreBundle”加载div中其他应用程序包的内容。为了处理这个问题,我想让CoreBundle路由捕获所有路由,然后将请求转发给应用程序包,以便获取div中的内容 路线是: @Route("{app_name}/{garbage}", name="_core_app_garbage", requirement={"garbage"=".*"}) 这很有效。它捕获将$app

我正在使用symfony 2实现一个web应用程序。它有一个主页,该主页通过一个“CoreBundle”提供服务,该“CoreBundle”加载div中其他应用程序包的内容。为了处理这个问题,我想让CoreBundle路由捕获所有路由,然后将请求转发给应用程序包,以便获取div中的内容

路线是:

@Route("{app_name}/{garbage}", name="_core_app_garbage", requirement={"garbage"=".*"})
这很有效。它捕获将$app_name设置为应用程序名称(第一个子目录)的所有路由,以及$garbage中的剩余uri

我的问题是试图在特定应用程序中找到$garbage的路径。核心捆绑包知道应用程序的捆绑包名称

到目前为止,我已经尝试了以下两件事:

  • 使用路由器服务查找路由,但尝试删除全部捕获路由:

    $router->getRouteCollection()->remove("_core_app_garbage");
    
    这似乎没有改变任何事情(我返回的路线仍然是包罗万象的)

  • 创建我自己的路由器。我尝试了各种配置,但似乎需要大量的类创建和特定的类创建(在我的例子中是AnnotationDirectoryLoader)。这对于我使用Symfony的习惯来说似乎太难了,而且如果我决定更改路由格式(例如改为YAML),它是不可移植的


  • 有没有快速简便的方法可以做到这一点?

    我找到了一个很好的解决方案,尽管它并不完美:

    基本上,我使用“routing.loader”服务加载特定捆绑包的路由集合。然后我创建自己的UrlMatcher来匹配这些路由

    // Get the route collection for the app's bundle
    $kernel = $this->container->get('kernel');
    $routingFilesLocation = $kernel->locateResource( '@'. $app->getBundle() . '/Controller/' );
    $routeLoader = $this->container->get('routing.loader');
    $collection = $routeLoader->load( $routingFilesLocation );
    
    // Ensure that we don't get in a redirect loop when
    // loading the dashboard of core
    $collection->remove( '_core_redirect_home' );
    
    // Try to match the rest of the url to one of the routes
    $router = $this->container->get('router');
    $routeString = $routingFilesLocation . "<br/>";
    $url =  "/" . $garbage;
    
    $matcher = new UrlMatcher( $collection, $router->getContext() );
    try
    {
        $routes = $matcher->match( $url );
    }
    catch( ResourceNotFoundException $e )
    {
        throw $this->createNotFoundException( 'Could not find "' . $url . '" route within ' . $app_name );
    }
    $response = $this->forward( $routes['_controller'] );
    
    //获取应用程序包的路由集合
    $kernel=$this->container->get('kernel');
    $RoutingFileLocation=$kernel->locateResource('@'.$app->getBundle()./Controller/');
    $routeLoader=$this->container->get('routing.loader');
    $collection=$routeLoader->load($RoutingFileLocation);
    //确保我们在运行时不会进入重定向循环
    //加载core的仪表板
    $collection->remove('u core\u redirect\u home');
    //尝试将url的其余部分与其中一个路由匹配
    $router=$this->container->get('router');
    $routeString=$RoutingFileLocation。“
    ”; $url=“/”$垃圾 $matcher=newurlmatcher($collection,$router->getContext()); 尝试 { $routes=$matcher->match($url); } 捕获(ResourceNotFoundException$e) { 抛出$this->createNotFoundException('找不到'.$url'.'路由在'.$app\u name'中); } $response=$this->forward($routes[''u controller']);

    这里的问题是,我已经硬编码,只在捆绑包中的“Controller”文件夹中查找控制器。

    我找到了一个非常好的解决方案,尽管它并不完美:

    基本上,我使用“routing.loader”服务加载特定捆绑包的路由集合。然后我创建自己的UrlMatcher来匹配这些路由

    // Get the route collection for the app's bundle
    $kernel = $this->container->get('kernel');
    $routingFilesLocation = $kernel->locateResource( '@'. $app->getBundle() . '/Controller/' );
    $routeLoader = $this->container->get('routing.loader');
    $collection = $routeLoader->load( $routingFilesLocation );
    
    // Ensure that we don't get in a redirect loop when
    // loading the dashboard of core
    $collection->remove( '_core_redirect_home' );
    
    // Try to match the rest of the url to one of the routes
    $router = $this->container->get('router');
    $routeString = $routingFilesLocation . "<br/>";
    $url =  "/" . $garbage;
    
    $matcher = new UrlMatcher( $collection, $router->getContext() );
    try
    {
        $routes = $matcher->match( $url );
    }
    catch( ResourceNotFoundException $e )
    {
        throw $this->createNotFoundException( 'Could not find "' . $url . '" route within ' . $app_name );
    }
    $response = $this->forward( $routes['_controller'] );
    
    //获取应用程序包的路由集合
    $kernel=$this->container->get('kernel');
    $RoutingFileLocation=$kernel->locateResource('@'.$app->getBundle()./Controller/');
    $routeLoader=$this->container->get('routing.loader');
    $collection=$routeLoader->load($RoutingFileLocation);
    //确保我们在运行时不会进入重定向循环
    //加载core的仪表板
    $collection->remove('u core\u redirect\u home');
    //尝试将url的其余部分与其中一个路由匹配
    $router=$this->container->get('router');
    $routeString=$RoutingFileLocation。“
    ”; $url=“/”$垃圾 $matcher=newurlmatcher($collection,$router->getContext()); 尝试 { $routes=$matcher->match($url); } 捕获(ResourceNotFoundException$e) { 抛出$this->createNotFoundException('找不到'.$url'.'路由在'.$app\u name'中); } $response=$this->forward($routes[''u controller']);

    这里的问题是,我已经硬编码,只在捆绑包中的“Controller”文件夹中查找控制器。

    您是否尝试过将catch-all路由移动到路由配置的底部?是否尝试过将catch-all路由移动到路由配置的底部?