在特定 Bundle 中查找路由

发布于 2024-12-13 19:50:59 字数 789 浏览 0 评论 0原文

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

路线是:

@Route("{app_name}/{garbage}", name="_core_app_garbage", requirement={"garbage"=".*"})

这很好用。它捕获 $app_name 设置为应用程序名称(第一个子目录)的所有路由,以及 $garbage 内的其余 uri。

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

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

  1. 使用路由器服务查找路由,但尝试删除捕获所有路由:

    $router->getRouteCollection()->remove("_core_app_garbage");
    

    这似乎没有改变任何东西(我返回的路由仍然是包罗万象的)。

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

有没有一种快速且简单的方法可以做到这一点?

I am implementing a web app using symfony 2. It has a main page that is served through a "CoreBundle" which loads up the contents of other app bundles within a div. To handle this, I want to have the CoreBundle route catch all routes and then forward the request to the app bundles in order to get the content within the div.

the route is:

@Route("{app_name}/{garbage}", name="_core_app_garbage", requirement={"garbage"=".*"})

This works great. It captures all routes with $app_name set to the name of the app (the first subdirectory), and the rest of the uri inside $garbage.

My problem is trying to find the route for $garbage within the specific app. The core bundle has knowledge of the bundle name for the app.

So far I have tried the following two things:

  1. Use the router service to find the route but trying to remove the catch all route:

    $router->getRouteCollection()->remove("_core_app_garbage");
    

    That doesn't seem to change anything (the route I get back is still the catch all).

  2. Creating my own router. I tried various configurations but it seems to take a lot of class creations and specific class creations ( AnnotationDirectoryLoader in my case ). That seems overly difficult to what I am used to with Symfony and it is not portable if I decide to change the routing format (to YAML for example).

Is there a quick and easy way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

◇流星雨 2024-12-20 19:50:59

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

基本上我使用“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'] );

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

I found a pretty good solution, although it isn't perfect:

Basically I load the route collection for a specific bundle using the 'routing.loader' service. Then I create my own UrlMatcher to match against those routes.

// 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'] );

The problem here is that I have hard coded to only look for controllers in the "Controller" folder within the bundle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文