symfony2 Unable to find controller

2019-09-10 22:53发布

I am trying to start on Symfony2 but ran into a problem right away following the Symfony 2 "the book" part "Creating pages in Symfony 2":

I did this:

Created the bundle

php app/console init:bundle "Acme\StudyBundle" src

*Added the namespace in app/autoload.php *

$loader->registerNamespaces(array(
    'Acme'                         => __DIR__.'/../src',
));

Initialized the bundle

// app/AppKernel.php
public function registerBundles()
{
    $bundles = array(
        // ...
        new Acme\StudyBundle\AcmeStudyBundle(),
    );

    // ...

    return $bundles;
}

Created the routes in app/config.routing.yml and src/Acme/StudyBundle/Resources/config/routing.yml

# app/config/routing.yml
homepage:
    pattern:  /
    defaults: { _controller: FrameworkBundle:Default:index }

hello:
    resource: "@AcmeStudyBundle/Resources/config/routing.yml"


# src/Acme/StudyBundle/Resources/config/routing.yml
hello:
    pattern:  /hello/{name}
    defaults: { _controller: AcmeStudyBundle:Hello:index }

Created the controller

// src/Acme/StudyBundle/Controller/HelloController.php

namespace Acme\StudyBundle\Controller;
use Symfony\Component\HttpFoundation\Response;

class HelloController
{
    public function indexAction($name)
    {
        return new Response('<html><body>Hello '.$name.'!</body></html>');
    }
}

When I load the page: http://localhost/app_dev.php/hello/Ryan Symfony gives me an exception:

Unable to find controller "AcmeStudyBundle:Hello" - class "Acme\StudyBundle\Controller\HelloController" does not exist.

I got over the code several times but cannot find anything wrong.

3条回答
够拽才男人
2楼-- · 2019-09-10 22:58

just add

<?php

in the beginning of your controller file : src/Acme/StudyBundle/Controller/HelloController.php

it's solved the problem to me.

查看更多
Ridiculous、
3楼-- · 2019-09-10 23:17

Afaik there is a discussion going on within the Symfony 2.0 dev guys in what places they should keep the "Bundles" extension.

I've just grabbed the latest version of Symfony via Git and followed your code 1:1.

I got various error messages too but when I changed...

  1. in src/Acme/StudyBundle/Resources/config/routing.yml

    defaults: { _controller: AcmeStudyBundle:Hello:index } to defaults: { _controller: AcmeStudy:Hello:index }

  2. app/config/routing.xml

    resource: "@AcmeStudyBundle/Resources/config/routing.yml" to resource: "@AcmeStudy/Resources/config/routing.yml"

...i got a pretty "Hello Ryan" in the browser.

Hope this helps!

查看更多
聊天终结者
4楼-- · 2019-09-10 23:23

You are probably running PR9. Update to PR11(latest), and I would bet this gets resolved. Symfony devs removed the 'Bundle' suffix in PR9, but added it back again shortly there after.

Also, Symfony devs keep an Update log that I find extremely helpful.

查看更多
登录 后发表回答