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 的开发来帮助它!