dsf5989 2018-06-20 13:03
浏览 121
已采纳

如何在现有的Symfony项目中正确设置新Bundle的路由?

I am working on a Symfony project, where the other developers are not really giving project-specific information, so this work is one of the worst project experience for me in the last five years. I am to create two Symfony bundles, an OrderBundle and an ErpBundle, to create some entities in the OrderBundle along with a service and a controller into ErpBundle.

The entities are normal Doctrine entities, I do not think they are important from this question's point of view. The HomeController is in the folder of

mainfolder/vendor/Myproject/myfolder/src/Myproject/App/ErpBundle/Controller/Admin/HomeController.php and it is like this:

<?php

namespace Myproject\App\ErpBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class HomeController extends AdminController
{
    /**
     * @Route("/home", defaults={
     *     "_format": "html"
     * })
     */
    public function indexAction()
    {
        //some code
    }

    /**
     * @Route(  "/home/menu",
     *          defaults={"_format": "html"})
     */
    public function indexMenuAction()
    {
        //some code
    }
}

Its routing.yml contains this

myproject_app_erp.myproject_admin.controller:
    resource: erp
    type: myproject_admin

and its sevice.yml contains this

services:
#    erp.example:
#        class: MyprojectAppErpBundle\Example
#        arguments: ["@service_id", "plain_value", "%parameter%"]
parameters:
    myproject_app_erp.routing_controllers:
        home: 'MyProject\App\ErpBundle\Controller\Admin\HomeController'

And, inside mainfolder/app/config/routing.yml I have this section:

myproject_app_erp:
    resource: '@MyprojectAppErpBundle/Resources/config/routing.yml'

In theory this should run the HomeController I need, which, in turn would work with the service I described, which will do something with the entities, but instead of that I get the following error:

The parameter "myproject_app_erp.routing_controllers" must be defined in erp (which is being imported from "/var/www/mainfolder/vendor/myproject/sing/src/Myproject/App/ErpBundle/Resources/config/routing.yml").

I have tried to copy the parameters section from the service.yml into the routing.yml to have that section there, which was missed by the error message, but the error message remained. I have been trying to solve this since yesterday with a lot of approaches and I have the impression that I have more difficulties in setting up the working environment than with writing the code. I have read about service creation at the Symfony docs, this is why I have been able to reach as far as I reached, but I do not know how to solve this problem. I think I need to check how autoload works, since it seems that /home routes are controlled there, but I do not know whether I am right and how to reach that. My question is: what should I check, where and how to be able to solve this problem.

EDIT:

I have managed to avoid the error described above by implementing DependencyInjection, but creating a folder with the name of DependencyInjection into the ErpBundle and implementing the following two classes:

Configuration.php

<?php

namespace Myproject\App\ErpBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
 * This is the class that validates and merges configuration from your app/config files
 *
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
 */
class Configuration implements ConfigurationInterface
{
    /**
     * {@inheritdoc}
     */
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $rootNode = $treeBuilder->root('myproject_app_erp');
        return $treeBuilder;
    }
}

MyprojectAppErpExtension.php

<?php

namespace Myproject\App\ErpBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;

/**
 * This is the class that loads and manages your bundle configuration
 *
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
 */

class MyprojectAppErpExtension extends Extension
{
    /**
     * {@inheritdoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.yml');
    }
}

While the page loads now, it still uses another bundle's HomeController than the one inside ErpBundle.

  • 写回答

1条回答 默认 最新

  • dongxia19772008 2018-06-21 12:46
    关注

    It turns out that there is a custom routing-specific logic in the project and I had to do some database changes, following a project-specific convention previously unkown to me. The details are so specific, that I cannot share them here without violating the business secrets related to the project, so I know what the answer is, but cannot share this with the community.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用三极管设计一个单管共射放大电路
  • ¥20 fluent无法启动
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架