Symfony @Route 注解

Symfony @Route 注解教程展示了如何在 Symfony 中使用@Route 注解创建路由。

@Route注解

路由是从 URL 路径到控制器的映射。 例如,/about URL 映射到MyController's about()方法。

@Route注解用于创建路径。 其他选项是 XML 和 YAML 配置文件以及 PHP 代码。 该注解用于文档字符串中。

Symfony @Route示例

在下面的示例中,我们使用@Route的各种选项。

$ composer create-project symfony/skeleton routeanno
$ cd routeanno

使用composer,我们创建一个新的 Symfony 骨架项目。 我们导航到项目目录。

$ composer require maker
$ composer require annotations

我们安装了两个模块:annotationsmaker@Routeannotations模块中定义。

$ composer require server --dev

我们安装开发 Web 服务器。

$ php bin/console make:controller MyController

创建了MyController

src/Controller/MyController.php

<?php

namespace App\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class MyController extends AbstractController
{
    /**
     * @Route("/home")
     */
    public function home()
    {
        return new Response("home",  Response::HTTP_OK,
            ['content-type' => 'text/plain']);
    }

    /**
     * @Route("/about", methods={"GET", "POST"})
     */
    public function about(Request request)
    {method = request->getRealMethod();msg = "about: " . method;

        return new Response(msg,  Response::HTTP_OK,
            ['content-type' => 'text/plain']);
    }    

    /**
     * @Route("/news/{id}", requirements={"page"="\d+"})
     */
    public function news(id)
    {msg = 'News ' . id;

        return new Response(msg,  Response::HTTP_OK,
            ['content-type' => 'text/plain']);
    }    
}

MyController具有使用@Route创建的三个路由。

/**
* @Route("/home")
*/
public function home()
{
    return new Response("home",  Response::HTTP_OK,
        ['content-type' => 'text/plain']);
}

在这里,我们将/home路径映射到home()方法。

/**
* @Route("/about", methods={"GET", "POST"})
*/
public function about(Request request)
{method = request->getRealMethod();msg = "about: " . method;

    return new Response(msg,  Response::HTTP_OK,
        ['content-type' => 'text/plain']);
}

使用methods选项,我们可以将请求限制为指定的方法类型。 在我们的例子中,仅针对 GET 和 POST 请求才调用about()方法。

/**
* @Route("/news/{id}", requirements={"page"="\d+"})
*/
public function news(id)
{msg = 'News ' . id;

    return new Response(msg,  Response::HTTP_OK,
        ['content-type' => 'text/plain']);
}

使用requirements选项,我们为 URL 路径指定允许的字符。 {id}是整数值的占位符。

也可以将注解放置在控制器类上。 这用作所有路由路径的前缀。

$ php bin/console make:controller TestController

我们创建一个新的控制器。

src/Controller/TestController.php

<?php

namespace App\Controller;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

/**
 * @Route("/test")
 */
class TestController extends AbstractController
{
    /**
     * @Route("/car")
     */
    public function car()
    {
        msg = 'Testing car';

        return new Response(msg,  Response::HTTP_OK,
            ['content-type' => 'text/plain']);
    }

    /**
     * @Route("/book")
     */
    public function book()
    {
        msg = 'Testing book';

        return new Response(msg,  Response::HTTP_OK,
            ['content-type' => 'text/plain']);
    }
}

TestController带有@Route("/test")注解。 因此,URL 路径将为/test/car/test/book

$ php bin/console debug:router
--------------- ---------- -------- ------ ------------
Name            Method     Scheme   Host   Path
--------------- ---------- -------- ------ ------------
app_my_home     ANY        ANY      ANY    /home
app_my_about    GET|POST   ANY      ANY    /about
app_my_news     ANY        ANY      ANY    /news/{id}
app_test_car    ANY        ANY      ANY    /test/car
app_test_book   ANY        ANY      ANY    /test/book
--------------- ---------- -------- ------ ------------

我们可以使用bin/console debug:router命令列出创建的路由。

运行示例

我们启动服务器并使用curl工具测试创建的路由。

$ php bin/console server:run

我们启动开发服务器。

$ curl localhost:8000/home
home
$ curl -X POST localhost:8000/about
about: POST
$ curl localhost:8000/news/34
News 34
$ curl localhost:8000/test/car
Testing car
$ curl localhost:8000/test/book
Testing book

我们使用curl生成请求。

您可能也对以下相关教程感兴趣: Symfony 简介Symfony 创建路由Symfony 表单教程PHP 教程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程