【问题标题】:Symfony - no route foundSymfony - 找不到路线
【发布时间】:2013-02-22 18:03:49
【问题描述】:

找不到“GET /hello.html”的路由

这是我尝试访问http://localhost/project/web/app_dev.php/hello.html时遇到的错误

默认控制器:

<?php
/**
 * @Route("/hello.html")
 * @Template()
 */
public function indexAction()
{
    return array();
}
?>

Routing.yml:

MyHelloworldBundle:
    resource: "@MyHelloworldBundle/Controller/"
    type:     annotation
    prefix:   /

AppKernel.php:

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 JMS\AopBundle\JMSAopBundle(),
            new JMS\DiExtraBundle\JMSDiExtraBundle($this),
            new JMS\SecurityExtraBundle\JMSSecurityExtraBundle(),
            new My\HelloworldBundle\MyHelloworldBundle(),
        );

        if (in_array($this->getEnvironment(), array('dev', 'test'))) {
            $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');
    }
}

整个文件结构好像没问题,src/My/HelloworldBundle/...怎么了?

到目前为止,我唯一做的就是删除了 acme demo - routing_dev.php 的三个部分,以及 AppKernel.php 中的行。

将控制器中的注释更改为其他内容也不起作用。视图文件 src/My/HelloworldBundle/Resources/views/Default/index.html.twig 存在。

编辑:

当我执行 route:match 命令时,没有一个路由匹配。

这是错误日志:http://khernik.pl/asd.png 和顶部:http://khernik.pl/asdf.png

我现在从头开始执行一切,使用 symfony 的大多数默认设置(在创建新包时添加自己的代码)。我做的唯一一件事就是从 AppKernel.php 中删除 Acme 行,并从 routing_dev.yml 中删除 acme 的三个前块。一切似乎都是正确的。

还是不行。

编辑 2 - 完全默认的控制器:

<?php

namespace My\HelloworldBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class DefaultController extends Controller
{
    /**
     * @Route("/hello/{name}")
     * @Template()
     */
    public function indexAction($name)
    {
        return array('name' => $name);
    }
}

【问题讨论】:

  • 您是否将正确的命名空间放在文件顶部,因为它不会出现在您的帖子中。
  • 命名空间 My\HelloworldBundle\Controller; - 当然
  • 如果你运行php app/console router:debug会怎样?
  • 每行显示OK。
  • 你能在你的问题中发布结果吗?并运行php app/console router:match /hello.html

标签: php symfony


【解决方案1】:

尝试在DemoController 的注释中指出Template() 属性

<?php
/**
 * @Route("/hello")
 * @Template('MyHelloworldBundle:Demo:hello.html')
 */
public function helloAction()
{
    return array();
}
?>

【讨论】:

    【解决方案2】:

    请在 hello.html 之前添加您的 ControllerName

    class DemoController extends Controller
    {
        /**
         * @Route("/hello.html")
         * @Template()
         */
        public function indexAction()
        {
            return array();
        }
    

    喜欢,要访问这个:在下面调用这个网址:

    http://localhost/project/web/app_dev.php/demo/hello.html
    

    【讨论】:

    • 他为什么要放ControllerName??
    猜你喜欢
    • 2013-12-11
    • 2015-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多