<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 `@Route`注解教程 > 原文: [http://zetcode.com/symfony/routeannotation/](http://zetcode.com/symfony/routeannotation/) Symfony `@Route`注解教程展示了如何在 Symfony 中使用`@Route`注解創建路由。 ## Symfony Symfony 是一組可重用的 PHP 組件和一個用于 Web 項目的 PHP 框架。 Symfony 于 2005 年發布為免費軟件。Fabien Potencier 是 Symfony 的原始作者。 Symfony 受到 Spring 框架的極大啟發。 ## `@Route`注解 路由是從 URL 路徑到控制器的映射。 例如,`/about` URL 映射到`MyController`的`about()`方法。 `@Route`注解用于創建路徑。 其他選項是 XML 和 YAML 配置文件以及 PHP 代碼。 該注解用于文檔字符串中。 ## Symfony `@Route`示例 在下面的示例中,我們使用`@Route`的各種選項。 ```php $ composer create-project symfony/skeleton routeanno $ cd routeanno ``` 使用`composer`,我們創建一個新的 Symfony 骨架項目。 我們導航到項目目錄。 ```php $ composer require maker $ composer require annotations ``` 我們安裝了兩個模塊:`annotations`和`maker`。 `@Route`在`annotations`模塊中定義。 ```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\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`創建的三個路由。 ```php /** * @Route("/home") */ public function home() { return new Response("home", Response::HTTP_OK, ['content-type' => 'text/plain']); } ``` 在這里,我們將`/home`路徑映射到`home()`方法。 ```php /** * @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()`方法。 ```php /** * @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 $ php bin/console make:controller TestController ``` 我們創建一個新的控制器。 `src/Controller/TestController.php` ```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 $ 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 $ php bin/console server:run ``` 我們啟動開發服務器。 ```php $ 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`生成請求。 In this tutorial we have created routes in Symfony using `@Route` annotation. 您可能也對以下相關教程感興趣: [Symfony 簡介](/symfony/intro/), [Symfony 創建路由](/symfony/createroutes/), [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>

                              哎呀哎呀视频在线观看