简体   繁体   English

Symfony 3 YAML路由不起作用

[英]Symfony 3 YAML routing not working

this is the code for my front controller: 这是我的前端控制器的代码:

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

and the following is my routing.yml file inside my SystemBundle>Resources>config : 以下是我的SystemBundle>资源> config中的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}

Everything seems okay and I can access the localhost:8000 but not the other pages like localhost:8000/contact, etc. 一切似乎都还可以,我可以访问localhost:8000,但不能访问其他页面,例如localhost:8000 / contact等。

They give out a 404 error and an exception: error image here 他们给出了404错误和异常: 这里的错误图片

Please help... 请帮忙...

In your routing.yml file, set type to annotation . 在您的routing.yml文件中,将type设置为注释 See the Symfony Routing Docs for more information. 有关更多信息,请参见Symfony路由文档

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

Then you can add annotation routes, very easily I might add, in your controller.. like so: 然后,您可以在控制器中添加批注路由(很容易添加),如下所示:

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

}

Now you can control your routing right at the class/method level. 现在,您可以直接在类/方法级别上控制路由。 Personally (and I feel many would agree) this offers a great level of control and clarity over your routes as they're included right there with the method in question. 就个人而言(我认为很多人都同意),因为所涉及的方法已包含在其中,因此可以对您的路线进行高度的控制和明确。

Caveat Some will argue (for some good reason) that annotations are bad practice as they clutter up the method comments, and actually remove clarity within said comments. 警告有些人会(出于某种充分的理由)认为注解是不好的做法,因为它们会使方法注释变得杂乱无章,并实际上消除了注释中的清晰度。 I disagree with this notion. 我不同意这个观点。 Symfony annotations are clearly different than most standard methods of method commenting as they're required to have a certain syntax, ie. Symfony注释与大多数标准的方法注释方法明显不同,因为它们需要具有某种语法,即。 @route will not compile; @route不会编译; @Route must have a capital 'R', for example. @Route必须使用大写@Route “ R”。

Not to mention, when you're working in a Symfony-based project, you should be aware of the possibility that annotations will be used. 更不用说,当您在基于Symfony的项目中工作时,您应该意识到使用批注的可能性。 This is all part of communication within a development team. 这是开发团队内部沟通的全部内容。 if you're being hired, for example, to work on a Symfony-based project, you should really be aware of annotations and they should not confuse you, nor should they confuse the code you're writing. 例如,如果您被雇用来从事基于Symfony的项目,则您应该真正意识到注释,它们不应该使您感到困惑,也不应使您正在编写的代码变得混乱。

I'm in favour of route annotations. 我赞成路线注释。 You'll find they're easy-to-use, and easy to control. 您会发现它们易于使用且易于控制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM