在自己的 Symfony3 供应商bundle中注释路由



我想建立一个具有多个可重用捆绑包的私有生态系统,类似于Sonata项目。这是我第一次,所以我遵循Symfony2 - 创建自己的供应商捆绑包 - 项目和git策略,并设置了一个名为PUIEconomyBundle的简单捆绑包与DefaultController。我使用 composer.json 将捆绑包从我的 Git 存储库导入到一个示例项目中。

现在我遇到了 404 No route found for "GET /test".请务必添加带注释的路线以保持概览。如何将工作注释路由引入控制器?debug:router没有提到此捆绑包中的路由,尽管探查器表示已启用 PUIEconomyBundle。

默认控制器:

class DefaultController extends Controller
{
    /**
     * @Route("/test", name="homepage")
     * @param Request $request
     *
     * @return SymfonyComponentHttpFoundationResponse
     */
    public function indexAction(Request $request)
    {
        dump('Hello!');die;
    }
}

外延:

public function load(array $configs, ContainerBuilder $container)
{
    $configuration = new Configuration();
    //$config = $this->processConfiguration($configuration, $configs);
    $fileLocator = new FileLocator(__DIR__.'/../Resources/config');
    $loader = new LoaderYamlFileLoader($container, $fileLocator);
    $loader->load('services.yml');
}

服务.yml:

services:
    pui_economy.routing_loader:
        class: CompanyPUIEconomyBundleServiceRoutingLoader
        tags:
            - { name: routing.loader }

路由加载器:

class RoutingLoader extends Loader
{
    public function load($resource, $type = null)
    {
        $collection = new RouteCollection();
        $resource = '@PUIEconomyBundle/Resources/config/routing.yml';
        $type = 'yaml';
        $importedRoutes = $this->import($resource, $type);
        $collection->addCollection($importedRoutes);
        return $collection;
    }
    public function supports($resource, $type = null)
    {
        return 'advanced_extra' === $type; // ??
    }
}

Routing.yml:

pui_economy:
    resource: "@PUIEconomyBundle/Controller"
        type: annotation

谢谢

您似乎忘记添加以下内容:

app_extra:
    resource: .
    type: extra

app/config/routing.yml.

请参阅使用自定义加载程序。

为什么要使用自定义路由加载程序?这是一个非常先进的主题,没有必要简单地通过注释在路由上绑定控制器。

您可以在此处找到@Route注释的工作示例:https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing.html

您还应该删除 die(( 语句。如果你以这种方式杀死请求,Symfony mabey不会给你回应。

您不需要自定义路由加载程序来加载捆绑带注释的路由。

我面临着类似的问题。我们所需要的只是将此配置放在我们要加载捆绑包的应用程序中

config/routes.yaml

my_cool_bundle_routes:
  # loads routes from the given routing file stored in some bundle
  resource: '@XyzAuthBundle/Controller/'
  type:     annotation

仅此而已:)

my_cool_bundle_routes命名并不重要。它必须是独一无二的。

最新更新