routing
v7.2.0
路由元件將 HTTP 請求對應到一組設定變數。
composer require symfony/routing
use App Controller BlogController ;
use Symfony Component Routing Generator UrlGenerator ;
use Symfony Component Routing Matcher UrlMatcher ;
use Symfony Component Routing RequestContext ;
use Symfony Component Routing Route ;
use Symfony Component Routing RouteCollection ;
$ route = new Route ( ' /blog/{slug} ' , [ ' _controller ' => BlogController::class]);
$ routes = new RouteCollection ();
$ routes -> add ( ' blog_show ' , $ route );
$ context = new RequestContext ();
// Routing can match routes with incoming requests
$ matcher = new UrlMatcher ( $ routes , $ context );
$ parameters = $ matcher -> match ( ' /blog/lorem-ipsum ' );
// $parameters = [
// '_controller' => 'AppControllerBlogController',
// 'slug' => 'lorem-ipsum',
// '_route' => 'blog_show'
// ]
// Routing can also generate URLs for a given route
$ generator = new UrlGenerator ( $ routes , $ context );
$ url = $ generator -> generate ( ' blog_show ' , [
' slug ' => ' my-blog-post ' ,
]);
// $url = '/blog/my-blog-post'
Symfony 7.1 的路由元件由redirection.io 支援。
redirection.io 記錄您網站的所有 HTTP 流量,並讓您可以在幾秒鐘內修復重定向規則的錯誤。為您的行銷、SEO 和 IT 團隊提供正確的工具來有效管理您的網站流量!
透過贊助 Symfony 的開發來幫助它!