💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# Symfony 创建路由 > 原文: [http://zetcode.com/symfony/createroutes/](http://zetcode.com/symfony/createroutes/) Symfony 创建路由教程展示了如何在 Symfony 中使用注解,XML,YAML 和 PHP 创建路由。 ## Symfony Symfony 是一组可重用的 PHP 组件和一个用于 Web 项目的 PHP 框架。 Symfony 于 2005 年发布为免费软件。Symfony 的原始作者是 Fabien Potencier。 Symfony 受到 Spring 框架的极大启发。 ## 路由 路由是从 URL 路径到控制器的映射。 例如,`/about` URL 映射到`MyController`的`about()`方法。 Symfony 允许使用注解,XML,YAML 和 PHP 创建路由。 ## Symfony 创建路由示例 在以下示例中,我们以不同的方式创建路由。 ```php $ composer create-project symfony/skeleton createroutes ``` 使用`composer`,我们创建一个新的 Symfony 骨架项目。 ```php $ cd createroutes ``` 我们转到项目目录。 ```php $ composer require maker $ composer require annotations ``` 我们安装了两个模块:`annotations`和`maker`。 ```php $ composer require server --dev ``` 我们安装开发 Web 服务器。 ```php $ php bin/console make:controller MyController ``` 创建了`MyController`。 `src/Controller/MyController.php` ```php <?php namespace App\Controller; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; class MyController extends AbstractController { /** * @Route("/about", name="about") */ public function about() { return new Response("This is About page", Response::HTTP_OK, ['content-type' => 'text/plain']); } public function index() { return new Response("This is Index page", Response::HTTP_OK, ['content-type' => 'text/plain']); } public function news() { return new Response("This is News page", Response::HTTP_OK, ['content-type' => 'text/plain']); } public function contacts() { return new Response("This is Contacts page", Response::HTTP_OK, ['content-type' => 'text/plain']); } } ``` `MyController`具有使用注解,XML,YAML 和 PHP 创建的四个路由。 每个路由均返回简单文本。 ```php /** * @Route("/about", name="about") */ public function about() { return new Response("This is About page", Response::HTTP_OK, ['content-type' => 'text/plain']); } ``` About 路由与`@Route`注解映射。 `config/routes.yaml` ```php index: path: / controller: App\Controller\MyController::index ``` 索引路由映射到 YAML 配置文件中。 `config/routes.xml` ```php <?xml version="1.0" encoding="UTF-8" ?> <routes xmlns="http://symfony.com/schema/routing" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> <route id="contacts" controller="App\Controller\MyController::contacts" path="/contacts" > </route> </routes> ``` 联系人路由映射到 XML 配置文件中。 `config/routes.php` ```php <?php use App\Controller\MyController; use Symfony\Component\Routing\Route; use Symfony\Component\Routing\RouteCollection; $routes = new RouteCollection(); $routes->add('news', new Route('/news', [ '_controller' => [MyController::class, 'news'] ])); return $routes; ``` 新闻路由是使用 PHP 代码创建的。 ```php $ php bin/console server:run ``` 我们启动开发服务器。 ```php $ curl localhost:8000/about This is About page $ curl localhost:8000/news This is News page $ curl localhost:8000/ This is Index page $ curl localhost:8000/contacts This is Contacts page ``` 我们使用`curl`生成请求。 在本教程中,我们使用注解,XML,YAML 配置和 PHP 代码在 Symfony 中创建了路由。 您可能也对以下相关教程感兴趣: [Symfony `@Route`注解教程](/symfony/routeannotation/), [Symfony 简介](/symfony/intro/), [Symfony 表单教程](/symfony/form/), [PHP 教程](/lang/php/)。