0
votes

I am trying to create my first bundle in Symfony. The tutorial walks you through the process of creating a bundle called "Acme". Since I downloaded symfony 2.4 that went with the Acme Demo bundle. I changed all "AcmeBundle" to "myAcmeBundle". I created the controller, the route and the bundle exactly like the instruction except the name of the bundle.

The tut says that if I go to

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

it should invoke my HelloController.php My question is that how symfony knows which bundle I want to run. I have 2 bundles in my "src" folder now. and whenever I enter localhost/app_dev.php/hello/Ryan , it displays the content of HelloController.php in AcmeBundle (the default demo bundle). I want to display the content of HelloController.php in myAcmeBundle (my new created bundle).

Additional info: maybe something that has to do with the class AppKernel, but not very sure

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 }
2
You need to tweak the routing settings. Did you read this page?bzeaman
Did you remove the routing entry at src/Acme/HelloBundle/Resources/config/routing.yml? Whichever match is found first will be used to select a controller.bzeaman

2 Answers

1
votes

Symfony2 routes are served (matched) in the same order that are parsed: when you use prefix: / without specify anything else (specially the test hello ruote) you're trying to override base AcmeBundle route that, I suppose without having enaugh information, is parsed BEFORE your new bundle route.

Solution: try for a moment to unregister AcmeBundle and see what happens. If now the framework serves your newbundle ruote and you want to keep AcmeBundle alive, you should write the routes of your bundle BEFORE AcmeDemo ones

Update

To unregister AcmeDemoBundle, Modify your AppKernel as follows:

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();
}
0
votes

Call the "Hello" controller instead of "Default" controller

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