symfony2 无法找到控制器

发布于 2024-11-01 02:51:34 字数 1722 浏览 1 评论 0原文

我正在尝试从 Symfony2 开始,但在 Symfony 2“书”部分“在 Symfony 2 中创建页面”之后立即遇到了问题:

我这样做了:

创建了捆绑包

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

*已添加app/autoload.php 中的命名空间 *

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

初始化包

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

    // ...

    return $bundles;
}

在 app/config.routing.yml 和 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 }

创建控制器

// 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>');
    }
}

当我加载页面时: http:// localhost/app_dev.php/hello/Ryan Symfony 给了我一个例外:

无法找到控制器“AcmeStudyBundle:Hello” - 类“Acme\StudyBundle\Controller\HelloController”不存在。

我把代码翻了好几遍,但找不到任何问题。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

静待花开 2024-11-08 02:51:34

只需添加

<代码>

在控制器文件的开头:src/Acme/StudyBundle/Controller/HelloController.php

它解决了我的问题。

just add

<?php

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

it's solved the problem to me.

葬心 2024-11-08 02:51:34

据我所知,Symfony 2.0 开发人员正在讨论应该在哪些地方保留“Bundles”扩展。

我刚刚通过 Git 获取了最新版本的 Symfony 并按照您的代码 1:1 进行操作。

我也收到了各种错误消息,但是当我更改时...

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

    默认值:{ _controller: AcmeStudyBundle:Hello:index }

    默认值:{ _controller: AcmeStudy:Hello:index }

  2. app/config/routing.xml

    资源:“@AcmeStudyBundle/Resources/config/routing.yml”

    资源:“@AcmeStudy/Resources/config/routing.yml”

...我在浏览器中看到了一个漂亮的“Hello Ryan”。

希望这有帮助!

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!

指尖上得阳光 2024-11-08 02:51:34

您可能正在运行 PR9。更新到 PR11(最新),我敢打赌这个问题会得到解决。 Symfony 开发人员在 PR9 中删除了“Bundle”后缀,但 不久后又将其添加回来

另外,Symfony 开发人员保留了我认为非常有帮助的更新日志

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文