Symfony - 路由



路由將請求 URI 對映到特定控制器的某個方法。一般來說,任何 URI 都有以下三個部分:

  • 主機名段
  • 路徑段
  • 查詢段

例如,在 URI/URL https://tutorialspoint.tw/index?q=data 中,www.tutorialspoint.com 是主機名段,index 是路徑段,q=data 是查詢段。通常,路由會根據一組約束檢查頁面段。如果任何約束匹配,則它會返回一組值。其中一個主要值是控制器。

註解

註解在 Symfony 應用程式的配置中起著重要作用。註解透過在程式碼本身中宣告配置來簡化配置。註解只不過是提供關於類、方法和屬性的元資訊。路由廣泛使用註解。即使路由可以在沒有註解的情況下完成,註解在很大程度上簡化了路由。

以下是一個註解示例。

/** 
   * @Route(“/student/home”) 
*/ 
public function homeAction() { 
   // ... 
} 

路由概念

考慮在“student”專案中建立的 StudentController 類。

StudentController.php

// src/AppBundle/Controller/StudentController.php 
namespace AppBundle\Controller;  

use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 

class StudentController extends Controller { 
   /** 
      * @Route(“/student/home”) 
   */ 
   public function homeAction() { 
      // ... 
   }  
    
   /** 
      * @Route(“/student/about”) 
   */ 
   public function aboutAction() { 
   } 
} 

在這裡,路由執行兩個步驟。如果您訪問 /student/home,則第一個路由匹配,然後執行 homeAction()。否則,如果您訪問 /student/about,則第二個路由匹配,然後執行 aboutAction()

新增萬用字元格式

假設您有一個學生記錄的分頁列表,其 URL 類似於 /student/2/student/3,分別對應第 2 頁和第 3 頁。然後,如果要更改路由的路徑,可以使用萬用字元格式。

示例

// src/AppBundle/Controller/BlogController.php 
namespace AppBundle\Controller;  

use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;  

class StudentController extends Controller {
   /**      
      * @Route(“/student/{page}", name = “student_about”, requirements = {"page": "\d+"})
   */ 
   public function aboutAction($page) { 
      // ... 
   } 
} 

這裡,\d+ 是一個正則表示式,匹配任何長度的數字。

分配佔位符

您可以在路由中分配佔位符值。定義如下。

// src/AppBundle/Controller/BlogController.php 
namespace AppBundle\Controller;  

use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;  

class StudentController extends Controller { 
   /**      
      * @Route(“/student/{page}", name = “student_about”, requirements = {"page": "\d+"})
   */ 
    
   public function aboutAction($page = 1) { 
      // ... 
   } 
}

在這裡,如果您訪問 /student,student_about 路由將匹配,並且 $page 將預設為值 1。

重定向到頁面

如果要將使用者重定向到另一個頁面,請使用 redirectToRoute()redirect() 方法。

public function homeAction() { 
   // redirect to the "homepage" route 
   return $this->redirectToRoute('homepage');  
   
   // redirect externally 
   \return $this->redirect('http://example.com/doc'); 
}

生成 URL

要生成 URL,請考慮路由名稱 student_name 和在該路由的路徑中使用的萬用字元名稱 student-names。生成 URL 的完整列表定義如下。

class StudentController extends Controller { 
   public function aboutAction($name) { 
      // ...  
      // /student/student-names 
      $url = $this->generateUrl( 
         ‘student_name’, 
         array(‘name’ =>
         ’student-names’) 
      ); 
   } 
}

StudentController

考慮一個在 StudentController 類中進行路由的簡單示例,如下所示。

StudentController.php

<?php  
namespace AppBundle\Controller;  

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 
use Symfony\Component\HttpFoundation\Response; 
use Symfony\Bundle\FrameworkBundle\Controller\Controller;  

class StudentController  { 
   /** 
      * @Route("/student/home") 
   */ 
   
   public function homeAction() { 
      $name = 'Student details application'; 
      return new Response( 
         '<html><body>Project: '.$name.'</body></html>' 
      ); 
   } 
}

現在,請求 url ”https://:8000/student/home”,它會產生以下結果。

Student Controller

類似地,您也可以為 aboutAction() 建立另一個路由。

廣告