You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.9 KiB

3 years ago
  1. Routing Component
  2. =================
  3. The Routing component maps an HTTP request to a set of configuration variables.
  4. Getting Started
  5. ---------------
  6. ```
  7. $ composer require symfony/routing
  8. ```
  9. ```php
  10. use App\Controller\BlogController;
  11. use Symfony\Component\Routing\Generator\UrlGenerator;
  12. use Symfony\Component\Routing\Matcher\UrlMatcher;
  13. use Symfony\Component\Routing\RequestContext;
  14. use Symfony\Component\Routing\Route;
  15. use Symfony\Component\Routing\RouteCollection;
  16. $route = new Route('/blog/{slug}', ['_controller' => BlogController::class]);
  17. $routes = new RouteCollection();
  18. $routes->add('blog_show', $route);
  19. $context = new RequestContext();
  20. // Routing can match routes with incoming requests
  21. $matcher = new UrlMatcher($routes, $context);
  22. $parameters = $matcher->match('/blog/lorem-ipsum');
  23. // $parameters = [
  24. // '_controller' => 'App\Controller\BlogController',
  25. // 'slug' => 'lorem-ipsum',
  26. // '_route' => 'blog_show'
  27. // ]
  28. // Routing can also generate URLs for a given route
  29. $generator = new UrlGenerator($routes, $context);
  30. $url = $generator->generate('blog_show', [
  31. 'slug' => 'my-blog-post',
  32. ]);
  33. // $url = '/blog/my-blog-post'
  34. ```
  35. Sponsor
  36. -------
  37. The Routing component for Symfony 5.3 is [backed][1] by [redirection.io][2].
  38. redirection.io logs all your website’s HTTP traffic, and lets you fix errors
  39. with redirect rules in seconds. Give your marketing, SEO and IT teams the right
  40. tool to manage your website traffic efficiently!
  41. Help Symfony by [sponsoring][3] its development!
  42. Resources
  43. ---------
  44. * [Documentation](https://symfony.com/doc/current/routing.html)
  45. * [Contributing](https://symfony.com/doc/current/contributing/index.html)
  46. * [Report issues](https://github.com/symfony/symfony/issues) and
  47. [send Pull Requests](https://github.com/symfony/symfony/pulls)
  48. in the [main Symfony repository](https://github.com/symfony/symfony)
  49. [1]: https://symfony.com/backers
  50. [2]: https://redirection.io/
  51. [3]: https://symfony.com/sponsor