【问题标题】:symfony which bundle I am runningsymfony 我正在运行哪个包
【发布时间】:2014-07-14 20:40:02
【问题描述】:

我正在尝试在Symfony 中创建我的第一个捆绑包。本教程将引导您完成创建名为“Acme”的包的过程。因为我下载了 symfony 2.4,它与 Acme Demo 捆绑包一起提供。我将所有“AcmeBundle”更改为“myAcmeBundle”。我创建了控制器、路由和包,与指令完全一样,只是包的名称除外。

tut 说如果我去

http://localhost/app_dev.php/hello/Ryan

它应该调用我的 HelloController.php 我的问题是 symfony 如何知道我要运行哪个包。我的“src”文件夹中现在有 2 个捆绑包。每当我输入 localhost/app_dev.php/hello/Ryan 时,它都会在 AcmeBundle (默认演示包)中显示 HelloController.php 的内容。我想在 myAcmeBundle(我新创建的包)中显示 HelloController.php 的内容。

附加信息: 可能与 AppKernel 类有关,但不太确定

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
            new Symfony\Bundle\SecurityBundle\SecurityBundle(),
            new Symfony\Bundle\TwigBundle\TwigBundle(),
            new Symfony\Bundle\MonologBundle\MonologBundle(),
            new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
            new Symfony\Bundle\AsseticBundle\AsseticBundle(),
            new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
            new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
            new myAcme\HelloBundle\myAcmeHelloBundle(),
        );

        if (in_array($this->getEnvironment(), array('dev', 'test'))) {
            $bundles[] = new Acme\DemoBundle\AcmeDemoBundle();
            //$bundles[] = new myAcme\HelloBundle\myAcmeHelloBundle();
            $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
            $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
            $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
        }

        return $bundles;
    }

    public function registerContainerConfiguration(LoaderInterface $loader)
    {
        $loader->load(__DIR__.'/config/config_'.$this->getEnvironment().'.yml');
    }
}

app\config\routing.yml

my_acme_hello:
    resource: "@myAcmeHelloBundle/Resources/config/routing.yml"
    prefix:   /

src\myAcme\HelloBundle\Resources\config\routing.yml

my_acme_hello_homepage:
    path:     /hello/{name}
    defaults: { _controller: myAcmeHelloBundle:Default:index }

【问题讨论】:

  • 您需要调整路由设置。你读过this 页面吗?
  • 您是否删除了src/Acme/HelloBundle/Resources/config/routing.yml 的路由条目?首先找到的匹配项将用于选择控制器。

标签: php symfony routing bundle


【解决方案1】:

Symfony2 路由以与解析相同的顺序提供(匹配):当您使用 prefix: / 而不指定任何其他内容(特别是测试 hello ruote)时,您试图覆盖基本 AcmeBundle 路由,我想在没有足够信息的情况下,在您的新捆绑路由之前进行解析。

解决方案:尝试注销AcmeBundle 看看会发生什么。 如果现在该框架为您的 newbundle ruote 提供服务,并且您想保持 AcmeBundle 活着,您应该在 AcmeDemo 之前编写您的 bundle 的路由

更新

要取消注册 AcmeDemoBundle,请修改您的 AppKernel,如下所示:

if (in_array($this->getEnvironment(), array('dev', 'test'))) {
  //$bundles[] = new Acme\DemoBundle\AcmeDemoBundle();
  $bundles[] = new myAcme\HelloBundle\myAcmeHelloBundle();
  $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
  $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
  $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
}

【讨论】:

  • 嗨,对不起,我是 symfony 的新手。如何注销捆绑包?
【解决方案2】:

调用“Hello”控制器而不是“Default”控制器

my_acme_hello_homepage:
    path:     /hello/{name}
    defaults: { _controller: myAcmeHelloBundle:Hello:index }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-13
    • 1970-01-01
    相关资源
    最近更新 更多