The Routing component 把HTTP request轉換為一系列的設定參數.
安裝
你有兩種方式來安裝這個元件:
<code>通过 Composer (symfony/routing on Packagist); 使用官方的 Git repository (https://github.com/symfony/Routing)。 </code>
然後, 需要Composer把vendor/autoload.php 這個檔案提供給。 否則,你的應用程式將找不到這個元件。
用法
你需要下面三部分來設定基本的路由系統:
<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>
未完待續
原文連結:
http://symfony.com/doc/current/components/routing/introduction.html