Home > Backend Development > PHP Tutorial > symfony routing component (The Routing Component)

symfony routing component (The Routing Component)

WBOY
Release: 2016-08-08 09:19:49
Original
1523 people have browsed it

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>
Copy after login

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:

  • RouteCollection, which contains the definition of the route (instances of the class Route)
  • RequestContext, information about the request;
  • UrlMatcher, which matches the request into A single route (that is, determine which route needs to be used)

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>
Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template