跳转至

Symfony 创建路由

原文: 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 映射到MyControllerabout()方法。

Symfony 允许使用注解,XML,YAML 和 PHP 创建路由。

Symfony 创建路由示例

在以下示例中,我们以不同的方式创建路由。

$ composer create-project symfony/skeleton createroutes

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

$ cd createroutes

我们转到项目目录。

$ composer require maker
$ composer require annotations

我们安装了两个模块:annotationsmaker

$ composer require server --dev

我们安装开发 Web 服务器。

$ php bin/console make:controller MyController

创建了MyController

src/Controller/MyController.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 创建的四个路由。 每个路由均返回简单文本。

/**
* @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

index:
    path: /
    controller: App\Controller\MyController::index

索引路由映射到 YAML 配置文件中。

config/routes.xml

<?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

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 bin/console server:run

我们启动开发服务器。

$ 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 简介Symfony 表单教程PHP 教程


我们一直在努力

apachecn/AiLearning

【布客】中文翻译组