首页 文章

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

提问于
浏览
1

我想构建一个具有多个可重用捆绑包的私有生态系统,类似于Sonata项目 . 这是我的第一次,所以我跟着Symfony2 - creating own vendor bundle - project and git strategy并使用DefaultController设置了一个名为PUIEconomyBundle的简单包 . 我使用composer.json从我的Git repo导入了一个示例项目 .

现在我遇到了404 No route found for "GET /test" . 注释路线以保持概览非常重要 . How do I introduce working annotated routing into my controllers? debug:router 没有提及此捆绑的路线,尽管探查者说PUIEconomyBundle已启用 .

DefaultController:

class DefaultController extends Controller
{
    /**
     * @Route("/test", name="homepage")
     * @param Request $request
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */
    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 Loader\YamlFileLoader($container, $fileLocator);
    $loader->load('services.yml');
}

Services.yml:

services:
    pui_economy.routing_loader:
        class: Company\PUI\EconomyBundle\Service\RoutingLoader
        tags:
            - { name: routing.loader }

RoutingLoader:

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

谢谢

2 回答

  • 0

    你似乎忘了添加这个:

    app_extra:
        resource: .
        type: extra
    

    app/config/routing.yml .

    Using the custom Loader .

  • 1

    你为什么使用自定义路由加载器?这是一个非常高级的主题,通过注释简单地绑定路由上的控制器是不必要的 .

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

    您还应该删除die()语句 . 如果您以这种方式终止请求,Symfony mabey不会给您回复 .

相关问题