symfony 注释_Symfony2路线注释入门

symfony 注释

When you download the Standard Symfony 2 Distribution, it includes an interesting bundle named SensioFrameworkExtraBundle which implements a lot of great stuff, especially the opportunity to use annotations directly within your controllers.

当您下载Standard Symfony 2发行版时,它包含一个名为SensioFrameworkExtraBundle的有趣包,该包实现了很多很棒的东西,尤其是有机会在控制器中直接使用注释。

The idea behind this article is not to convice developers to embrace this way of doing, but to point the finger at an alternative method to easily configure controllers. Keep in mind that there is no magic recipe, it depends on what you need in each specific scenario.

本文的想法不是要劝告开发人员采用这种方式,而是要用另一种方法轻松配置控制器。 请记住,没有神奇的配方,这取决于您在每种特定情况下的需求。

Symfony 2 implements a strong built-in component to manage all the routes of an application: the Routing Component. Basically, a route maps a URL to a controller action. Because Symfony is intended to be modular, a file is dedicated to this: routing.yml. You will find it in app > config > routing.yml.

Symfony 2实现了一个强大的内置组件来管理应用程序的所有路由:Routing组件。 基本上,路由将URL映射到控制器动作。 因为Symfony旨在模块化,所以专用于此文件: routing.yml 。 您可以在app > config > routing.yml找到它。

In order to understand how to define our routes with annotations, I’ll take as example a simple blog application.

为了理解如何使用注释定义路线,我将以一个简单的博客应用程序为例。

步骤1:建立首页路线 (Step 1: creating the homepage route)

We just want to link the path / to an action of our HomeController.

我们只想将路径/链接到HomeController的动作。

没有注释 (Without annotations)

In app/config/routing.yml:

app/config/routing.yml

blog_front_homepage:
  path : /
  defaults:  { _controller: BlogFrontBundle:Home:index }

In src/Blog/FrontBundle/Controller/HomeController.php:

src/Blog/FrontBundle/Controller/HomeController.php

<?php
namespace Blog\FrontBundle\Controller;

class HomeController
{
    public function indexAction()
    {
        //... create and return a Response object
    } 
}

Let’s take a look at those files. In routing.yml, we declared a simple configuration for the route named blog_front_homepage with 2 parameters: the path and the action of the controller we want to target. As far as the controller goes, it doesn’t need anything special.

让我们看一下那些文件。 在routing.yml ,我们为名为blog_front_homepage的路由声明了一个简单配置,其中包含两个参数:我们要定位的控制器的路径和操作。 就控制器而言,它不需要任何特殊的东西。

带注释 (With annotations)

In app/config/routing.yml:

app/config/routing.yml

blog_front:
    resource: "@BlogFrontBundle/Controller/"
    type:     annotation
    prefix:   /

In src/Blog/FrontBundle/Controller/HomeController.php:

src/Blog/FrontBundle/Controller/HomeController.php

<?php

namespace Blog\FrontBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

class HomeController
{
    /**
     * @Route("/", name="blog_home_index")
     */
    public function indexAction() { /* ... */ }
}

First, let’s see what happened to routing.yml:

首先,让我们看看routing.yml发生了什么:

  • resource targets the controller to impact

    resource将控制对象作为目标

  • type obviously defines the way we declare routes

    type显然定义了我们声明路由的方式

  • prefix defines a prefix for all actions of a controller class (optional)

    prefix定义控制器类的所有操作的前缀(可选)

What’s more interesting is how our controller is now built. Before everything else, we have to call the relevant class of the SensioFrameworkExtraBundle: use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;. We are now ready to implement the route and the parameters we want to assign: in this case only the path and the name (we will later see all that we can do): @Route("/", name="blog_homepage").

更有趣的是我们现在如何构建控制器。 在进行其他所有操作之前,我们必须调用SensioFrameworkExtraBundle的相关类: use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 。 现在,我们准备实现要分配的路由和参数:在这种情况下,仅路径和名称(我们稍后将看到我们可以做的一切): @Route("/", name="blog_homepage")

Some of you might be thinking: “Well, we know how to override the controller with the routing layer, so what? At the end of the day, more code is needed for basically the same result”. And you would be right… At least for now.

你们中有些人可能会想:“好吧,我们知道如何用路由层覆盖控制器,那又如何呢? 归根结底,基本相同的结果需要更多的代码。” 而且您会是正确的……至少现在是这样。

步骤2:添加文章页面路线 (Step 2: adding an article page route)

没有注释 (Without annotations)

In app/config/routing.yml:

app/config/routing.yml

blog_front_homepage:
  path : /
  defaults:  { _controller: BlogFrontBundle:Home:index }

blog_front_article:
  path : /article/{slug}
  defaults:  { _controller: BlogFrontBundle:Home:showArticle }

In src/Blog/FrontBundle/Controller/HomeController.php:

src/Blog/FrontBundle/Controller/HomeController.php

<?php
// namespace & uses...

class HomeController
{
    public function indexAction() { /* ... */ }
    
    public function showArticleAction($slug) { /* ... */ }
}

带注释 (With annotations)

In app/config/routing.yml:

app/config/routing.yml

blog_front:
    resource: "@BlogFrontBundle/Controller/"
    type:     annotation
    prefix:   /

In src/Blog/FrontBundle/Controller/HomeController.php:

src/Blog/FrontBundle/Controller/HomeController.php

<?php
// namespace & uses...

class HomeController
{
    /**
     * @Route("/", name="blog_home_index")
     */
    public function indexAction() { /* ... */ }
    
    /**
     * @Route("/article/{slug}", name="blog_home_show_article")
     */
    public function showArticleAction($slug) { /* ... */ }
}

Have you noticed? routing.yml doesn’t need any changes. Now, you can check from a single glance which action is being called from a route pattern.

你注意到了吗? routing.yml不需要任何更改。 现在,您可以一目了然地检查从路线模式中正在调用哪个动作。

If you want all your actions from the controller to have a prefix, for instance /admin, you can remove prefix key from routing.yml and add an extra @Route annotation at the top of your class:

如果希望控制器的所有动作都带有前缀,例如/admin ,则可以从routing.yml删除prefix键,并在类顶部添加一个额外的@Route注释:

In app/config/routing.yml:

app/config/routing.yml

blog_front: ...
 
blog_admin:
    resource: "@BlogAdminBundle/Controller/"
    type:     annotation

In src/Blog/AdminBundle/Controller/AdminController.php:

src/Blog/AdminBundle/Controller/AdminController.php

<?php
// namespace & uses...

/**
 * @Route("/admin")
 */
class AdminController
{
    /**
     * @Route("/", name="blog_admin_index")
     */
    public function indexAction() { /* ... */ }
    
    /**
     * @Route("/create", name="blog_admin_create_article")
     */
    public function createArticleAction() { /* ... */ }
}

步骤3:额外的路由配置 (Step 3: extra routing configuration)

设置URL默认参数 (Setting URL default parameters)

Syntax: defaults = { "key" = "value" }.

语法: defaults = { "key" = "value" }

/**
 * @Route(
 *       path     = "/article/{slug}",
 *       name     = "blog_home_show_article",
 *       defaults = { "slug" = "hello" }
 * )
 */

By adding slug to the defaults key, the {slug} placeholder is no longer required. The URL /article will match this route and the value of the slug parameter will be set to hello. The URL /blog/world will also match, giving the page parameter a value of world.

通过将slug添加到defaults键,不再需要{slug}占位符。 URL /article将匹配此路由,并且slug参数的值将设置为hello 。 URL /blog/world也将匹配,使page参数的值为world

增加要求 (Adding requirements)

Syntax: requirements = { "key" = "value" }.

语法: requirements = { "key" = "value" }

/**
 * @Route(
 *       path         = "/article/{slug}",
 *       name         = "blog_home_show_article",
 *       defaults     = { "slug" = "hello" },
 *       requirements = { "slug" = "[a-z]+" }
 * )
 */

We can define requirements for the slug key with a regular expression, to explicitly define that the value of {slug} has to be exclusively made of alpha characters. In the following example, we do the exact same thing with digits:

我们可以使用正则表达式定义对slug键的要求,以明确定义{slug}的值必须仅由字母字符组成。 在以下示例中,我们用数字做完全相同的事情:

/**
 * @Route(
 *       path         = "article/{id}",
 *       name         = "blog_home_show_article",
 *       defaults     = { "id" = 1 },
 *       requirements = { "id" = "\d+" } 
 * )
 */

If you need more information about regular expressions follow this link.

如果您需要有关正则表达式的更多信息,请单击此链接

实施HTTP方法 (Enforcing the HTTP method)

Syntax: methods = { "request method" }.

语法: methods = { "request method" }

/**
 * @Route(
 *       path         = "/article/{slug}",
 *       name         = "blog_home_show_article",
 *       defaults     = { "slug" = "hello" },
 *       requirements = { "slug" = "[a-z]+" },
 *       methods      = { "GET", "POST" }
 * )
 */

We can also match on the method of the incoming request (i.e. GET, POST, PUT, DELETE). Remember that if no method is specified, the route will match any of them.

我们还可以匹配传入请求的方法(即GETPOSTPUTDELETE )。 请记住,如果未指定任何方法,则路由将匹配其中任何一个。

实施HTTP方案 (Enforcing the HTTP scheme)

Syntax: schemes = { "protocol" }.

语法: schemes = { "protocol" }

/**
 * @Route(
 *       path         = "/article/{slug}",
 *       name         = "blog_home_show_article",
 *       defaults     = { "slug" = "hello" },
 *       requirements = { "slug" = "[a-z]+" },
 *       methods      = { "GET", "POST" },
 *       schemes      = { "https" }  
 * )
 */

In this case, we want to secure the route to be sure that it is accessed via the HTTPS protocol.

在这种情况下,我们要确保路由安全,以确保可以通过HTTPS协议访问该路由。

强制执行主机名 (Enforcing the host name)

Syntax: host = "myhost.com".

语法: host = "myhost.com"

/**
 * @Route(
 *       path         = "/article/{slug}",
 *       name         = "blog_home_show_article",
 *       defaults     = { "slug" = "hello" },
 *       requirements = { "slug" = "[a-z]+" },
 *       methods      = { "GET", "POST" },
 *       schemes      = { "https" },
 *       host         = "myblog.com"
 * )
 */

We can also match on the HTTP host. This one will match only if the host is myblog.com.

我们还可以在HTTP主机上进行匹配。 仅当主机为myblog.com时,此主机才会匹配。

步骤4:运动 (Step 4: exercise)

As we are now able to build a solid routing structure, imagine that we have to create the action to delete an article with the right route in our AdminController.php. We have to:

现在我们已经能够构建一个可靠的路由结构,可以想象我们必须创建操作以删除AdminController.php具有正确路由的文章。 我们必须:

  • Define the path as /admin/delete/article/{id};

    将路径定义为/admin/delete/article/{id}

  • Define the name as blog_admin_delete_article;

    将名称定义为blog_admin_delete_article

  • Define the requirement for the key id as only digits;

    将密钥id的要求定义为仅数字;

  • Define the GET request method.

    定义GET请求方法。

Stuck? Have a look at the solution below:

卡住? 看看下面的解决方案:

<?php
// src/Blog/AdminBundle/Controller/AdminController.php
// namespace & uses...

/**
 * @Route("/admin")
 */
class AdminController
{
    /* ... */

    /**
     * @Route("
     *    path          = "/delete/article/{id}", 
     *    name          = "blog_admin_delete_article"
     *    requirements  = { "id" = "\d+" },
     *    methods       = { "GET" }
     * )
     */
    public function deleteArticleAction($id) { /* ... */ }
}

最后的想法 (Final thoughts)

As you can see, managing routes with annotations is both easy to write, and easy to read and maintain. It also has the nice pro of gathering both the code and the configuration in a unique file: the controller class.

如您所见,使用注释管理路由既易于编写,也易于阅读和维护。 它还具有将代码和配置都收集在一个唯一的文件中的好功能:控制器类。

Do you use annotations, or the standard configuration? Which do you prefer, and why?

您使用注释还是标准配置? 你更偏向于哪个,为什么?

翻译自: https://www.sitepoint.com/getting-started-symfony2-route-annotations/

symfony 注释

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值