Symfony-routing
Symfony-ルーティング
ルーティングはリクエストURIを特定のコントローラーのメソッドにマップします。 一般的に、任意のURIには次の3つの部分があります-
- ホスト名セグメント
- パスセグメント
- クエリセグメント
たとえば、URI/URLでは、 http://www.finddevguides.com/index?q = data、www.finddevguides.com はホスト名セグメント、indexはパスセグメント、q = dataはクエリセグメントです。 。 一般に、ルーティングは一連の制約に対してページセグメントをチェックします。 一致する制約がある場合、値のセットが返されます。 主な価値の1つはコントローラーです。
アノテーション
注釈は、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() {
}
}
ここで、ルーティングは2つのステップを実行します。 /student/home に移動すると、最初のルートが一致し、* homeAction()が実行されます。 それ以外の場合、 */student/about に移動すると、2番目のルートが一致し、* aboutAction()*が実行されます。
ワイルドカード形式の追加
ページ2および3に対応する_/student/2および/student/3_のようなURLを持つ学生レコードのページ分割されたリストがあるとします。 その後、ルートのパスを変更する場合は、ワイルドカード形式を使用できます。
例
//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 * http://localhost:8000/student/home” を要求すると、次の結果が生成されます。
同様に、* aboutAction()*の別のルートを作成することもできます。