首页 文章

symfony 2找不到“GET /”的路线

提问于
浏览
21

当我尝试运行http://localhost/app_dev.php时,Symfony2返回找不到"GET /"的路由,但此URL工作正常:http://localhost/app_dev.php/hello/Symfony . 我删除了AcmeDemoBundle,我正在尝试从symfony2教程运行一个示例包 . 怎么了 ?

app / config / routing.yml:

ShopMyShopBundle:
resource: "@ShopMyShopBundle/Resources/config/routing.yml"
prefix:   /

app / config / routing_dev.yml:

_assetic:
resource: .
type:     assetic

_wdt:
resource: "@WebProfilerBundle/Resources/config/routing/wdt.xml"
prefix:   /_wdt

_profiler:
resource: "@WebProfilerBundle/Resources/config/routing/profiler.xml"
prefix:   /_profiler

_configurator:
resource: "@SensioDistributionBundle/Resources/config/routing/webconfigurator.xml"
prefix:   /_configurator

_main:
resource: routing.yml

src / Shop / MyShopBundle / Resources / config / routing.yml:

ShopMyShopBundle_homepage:
pattern:  /hello/{name}
defaults: { _controller: ShopMyShopBundle:Main:index }
requirements:
    _method:  GET

7 回答

  • 20

    我也尝试过这个错误,我只是添加了/ hello /任意名称,因为它是必须有一个hello / name的路径

    示例:而不是仅仅放http://localhost/app_dev.php

    这样说吧http://localhost/name_of_your_project/web/app_dev.php/hello/ai

    它会显示Hello Ai . 我希望我回答你的问题 .

  • 10

    问题是你没有 / 的路线 . 将您的定义更改为:

    ShopMyShopBundle_homepage:
        pattern:  /
        defaults: { _controller: ShopMyShopBundle:Main:index }
        requirements:
            _method:  GET
    
  • 5

    上面的答案是错误的,分别没有回答为什么你在查看demo-content prod-mode时遇到麻烦 .

    这是正确的答案:清除你的“prod”-cache:

    php app/console cache:clear --env prod
    
  • 1

    这项工作对我来说:

    cache:clear --env=prod
    
  • 3

    使用symfony 2.3与PHP 5.5和使用内置服务器

    app/console server:run
    

    应输出如下内容:

    Server running on http://127.0.0.1:8000
    Quit the server with CONTROL-C.
    

    然后去http://127.0.0.1:8000/app_dev.php/app/example

    这应该给你默认,你也可以通过查看src / AppBundle / Controller / DefaultController.php找到默认路由

  • 1

    前缀是url路由的前缀 . 如果它等于'/',则表示它没有前缀 . 然后你定义了一个模式“它应该以/ hello开头”的路由 .

    要为'/'创建路由,您需要在src / Shop / MyShopBundle / Resources / config / routing.yml中添加以下行:

    ShopMyShopBundle_homepage:
        pattern:  /
        defaults: { _controller: ShopMyShopBundle:Main:index }
    
  • 0

    我可能只是一个犯了这个错误的人,但也许不是这样我会发布的 .

    路由中的format for annotations必须以斜杠和 two 星号开头 . 我犯了一个斜线的错误,只有一个星号,PHPStorm自动完成 .

    我的路线看起来像这样:

    /*
     * @Route("/",name="homepage")
     */
    public function indexAction(Request $request) {
        return $this->render('default/index.html.twig');
    }
    

    什么时候应该是这样的

    /**
     * @Route("/",name="homepage")
     */
    public function indexAction(Request $request) {
        return $this->render('default/base.html.twig');
    }
    

相关问题