<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 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/)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看