The Routing component converts the HTTP request into a series of configuration parameters.
Installation
You have two ways to install this component:
<code>通过 Composer (symfony/routing on Packagist); 使用官方的 Git repository (https://github.com/symfony/Routing)。 </code>
Then, Composer is required to provide the vendor/autoload.php file to the autoloading mechanism . Otherwise, your application will not find this component.
Usage
You need the following three parts to set up the basic routing system:
Here is a simple example. Here you need to make sure your autoloader has loaded this component:
<code><span>use</span><span>Symfony</span>\<span>Component</span>\<span>Routing</span>\<span>Matcher</span>\<span>UrlMatcher</span>; <span>use</span><span>Symfony</span>\<span>Component</span>\<span>Routing</span>\<span>RequestContext</span>; <span>use</span><span>Symfony</span>\<span>Component</span>\<span>Routing</span>\<span>RouteCollection</span>; <span>use</span><span>Symfony</span>\<span>Component</span>\<span>Routing</span>\<span>Route</span>; <span>$route</span> = <span>new</span> Route(<span>'/foo'</span>, <span>array</span>(<span>'controller'</span> => <span>'MyController'</span>)); <span>$routes</span> = <span>new</span> RouteCollection(); <span>$routes</span>->add(<span>'route_name'</span>, <span>$route</span>); <span>$context</span> = <span>new</span> RequestContext(<span>$_SERVER</span>[<span>'REQUEST_URI'</span>]); <span>$matcher</span> = <span>new</span> UrlMatcher(<span>$routes</span>, <span>$context</span>); <span>$parameters</span> = <span>$matcher</span>->match(<span>'/foo'</span>); <span>// array('controller' => 'MyController', '_route' => 'route_name')</span></code>
It should be noted that when using $_SERVER[‘REQUEST_URI’], any parameters can be included in the URL. A simple solution is to use the HttpFoundation component, which is explained below.
To be continued
Original link:
http://symfony.com/doc/current/components/routing/introduction.html
The above has introduced the symfony routing component (The Routing Component), including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.