Symfony 3 YAML路由不起作用


Symfony 3 YAML routing not working

这是我的前控制器的代码:

<?php
namespace SystemBundle'Controller;
use Symfony'Bundle'FrameworkBundle'Controller'Controller;
class SystemController extends Controller
{
    public function indexAction()
    {
        return $this->render('SystemBundle:Pages:index.html.twig');
    }
    public function pkgsAction(){
        return $this->render('SystemBundle:Pages:pkg.html.twig');
    }
    public function aboutAction(){
        return $this->render('SystemBundle:Pages:about.html.twig');
    }
    public function contactAction(){
        return $this->render('SystemBundle:Pages:contact.html.twig');
    }
}

下面是我的SystemBundle>资源>配置中的routing.yml文件:

system_homepage:
    path:     /
    defaults: { _controller: SystemBundle:System:index }
system_about:
    path: /about
    defaults: { _controller: SystemBundle:System:about}
system_contact:
    path: /contact
    defaults: { _controller: SystemBundle:System:contact}
system_pkg:
    path: /pkgs
    defaults: { _controller: SystemBundle:System:pkgs}

一切似乎都很好,我可以访问localhost:8000,但不能访问localhost:8000/contact等其他页面。

他们给出一个404错误和一个异常:此处为错误图像

请帮忙。。。

routing.yml文件中,将type设置为annotation。有关详细信息,请参阅Symfony路由文档。

// app/config/routing.yml
app:
    resource: "@SystemBundle/Controller/"
    type:     annotation

然后你可以在你的控制器中添加注释路线,我可以很容易地添加。。像这样:

<?php
namespace SystemBundle'Controller;
use Symfony'Bundle'FrameworkBundle'Controller'Controller;
class SystemController extends Controller
{
    /**
     * @Route("/")
     */
    public function indexAction()
    {
        return $this->render('SystemBundle:Pages:index.html.twig');
    }
    /**
     * @Route("/pkg")
     */
    public function pkgsAction()
    {
        return $this->render('SystemBundle:Pages:pkg.html.twig');
    }
    /**
     * @Route("/about")
     */
    public function aboutAction()
    {
        return $this->render('SystemBundle:Pages:about.html.twig');
    }
    /**
     * @Route("/contact")
     */
    public function contactAction()
    {
        return $this->render('SystemBundle:Pages:contact.html.twig');
    }
}

现在,您可以在类/方法级别控制路由。就我个人而言(我觉得很多人都会同意),这为你的路线提供了很好的控制和清晰度,因为它们与所讨论的方法一起包含在那里。

洞穴有些人会争辩说(出于一些好的原因)注释是的坏做法,因为它们扰乱了方法注释,实际上删除了所述注释中的清晰度。我不同意这个观点。Symfony注释与大多数标准的方法注释方法明显不同,因为它们需要具有特定的语法,即@route不会编译;例如,@Route必须具有大写字母"R"。

更不用说,当您在基于Symfony的项目中工作时,您应该意识到使用注释的可能性。这都是开发团队内部沟通的一部分。例如,如果你受雇于一个基于Symfony的项目,你应该真正注意注释,它们不应该让你感到困惑,也不应该混淆你正在编写的代码。

我赞成路线注释。你会发现它们易于使用,易于控制。