それでは始めましょう。
この記事では、symfonyでのルーティングの仕組みについては説明しません。 あなたはここでそれについて読むか、 コードを見ることができます ! 次に、フレームワークの2番目のブランチで何が変更されたかを説明します。
新機能
- ルーティングシステムはコンポーネントになり、フレームワークとは別に使用できるようになりました。
- コード自体を再設計し、パフォーマンスを改善しました。 ルーティングルールごとにオブジェクトは作成されなくなりました(もちろん、キャッシュが使用されていない限り!)。
- ルートとurl'aの比較時。 strpos関数が最初に(可能な場合)使用され、次にpreg_matchのみが使用されます。
- さまざまなオブジェクトがURLを生成し、それらを比較しています(ProjectUrlGenerator、ProjectUrlMatcher)。
- Symfony \ Components \ Routing \ Routerクラスのコンストラクターに適切なパラメーターを渡すことで、ルーティングシステムの任意のクラスを置き換えることができます。
- PHPでのルールのダンプに加えて、Apacheのダンプがあります。 必要に応じて、nginxのファイルダンパーを作成することが可能になると思います。
どうして?
- パターンに星を使用する機能が削除されました(またはまだ実行されていない可能性があります)。 ルール「/:module /:action / *」は以前と同様に機能しません。
以下に、新規を説明するコードのセクションを示します)。 フレームワークとは別にルーティングをテストしたため、ルール内のデータは異なりますが、本質は同じままです。 つまり、$ defaults配列は異なります。 パラメーター_bundle、_controller、_actionの代わりに、module、actionを実行しました。
Symfony 1.2のルールのダンプの例:
<?php
// auto-generated by sfRoutingConfigHandler
// date: 2010/03/14 00:46:57
return array(
'addTopic' => new sfRoute( 'blogs/add-topic/' , array (
'module' => 'blogsEdit' ,
'action' => 'addTopic' ,
), array (), array ()),
'editTopic' => new sfRoute( 'blogs/edit-topic/:id/' , array (
'module' => 'blogsEdit' ,
'action' => 'editTopic' ,
), array (), array ()),
'themeBlog' => new sfRoute( 'blogs/:name/' , array (
'module' => 'blogs' ,
'action' => 'blog' ,
), array (), array ())
// .. ..
);
* This source code was highlighted with Source Code Highlighter .
Symfony 1.4のルールのダンプの例:
<?php
// auto-generated by sfRoutingConfigHandler
// date: 2010/03/14 00:46:57
$ this ->routes[ 'route-1' ] = unserialize( ' ' );
$ this ->routes[ 'route-2' ] = unserialize( ' ' );
$ this ->routes[ 'route-6' ] = unserialize( ' ' );
$ this ->routes[ 'route-7' ] = unserialize( ' ' );
$ this ->routes[ 'route-8' ] = unserialize( ' ' );
$ this ->routes[ 'route-9' ] = unserialize( ' ' );
$ this ->routes[ 'route-10' ] = unserialize( ' ' );
$ this ->routes[ 'route-11' ] = unserialize( ' ' );
$ this ->routes[ 'route-12' ] = unserialize( ' ' );
$ this ->routes[ 'homepage' ] = unserialize( ' ' );
$ this ->routes[ 'default_index' ] = unserialize( ' ' );
$ this ->routes[ 'default' ] = unserialize( ' ' );
* This source code was highlighted with Source Code Highlighter .
Symfony 2のルーティングルールの例:
# blog
addTopic:
pattern: blogs/add-topic/
defaults: { module: blogsEdit, action: addTopic }
editTopic:
pattern: blogs/edit-topic/:id/
defaults: { module: blogsEdit, action: editTopic }
themeBlog:
pattern: blogs/:name/
defaults: { module: blogs, action: blog }
* This source code was highlighted with Source Code Highlighter .
Symfony 2のProjectUrlMatcherクラスの例:
<?php
/**
* ProjectUrlMatcher
*
* This class has been auto-generated
* by the Symfony Routing Component.
*/
class ProjectUrlMatcher extends Symfony\\Components\\Routing\\Matcher\\UrlMatcher
{
/**
* Constructor.
*/
public function __construct(array $context = array(), array $defaults = array())
{
$ this ->context = $context;
$ this ->defaults = $defaults;
}
public function match($url)
{
$url = $ this ->normalizeUrl($url);
if (0 === strpos($url, '/blogs/add-topic' ) && preg_match( '#^/blogs/add\-topic$#x' , $url, $matches))
return array_merge($ this ->mergeDefaults($matches, array ( 'module' => 'blogsEdit' , 'action' => 'addTopic' ,)), array( '_route' => 'addTopic' ));
if (0 === strpos($url, '/blogs/edit-topic' ) && preg_match( '#^/blogs/edit\-topic/(?P<id>[^/\.]+?)$#x' , $url, $matches))
return array_merge($ this ->mergeDefaults($matches, array ( 'module' => 'blogsEdit' , 'action' => 'editTopic' ,)), array( '_route' => 'editTopic' ));
if (0 === strpos($url, '/blogs' ) && preg_match( '#^/blogs/(?P<name>[^/\.]+?)$#x' , $url, $matches))
return array_merge($ this ->mergeDefaults($matches, array ( 'module' => 'blogs' , 'action' => 'blog' ,)), array( '_route' => 'themeBlog' ));
// ..
return false ;
}
}
* This source code was highlighted with Source Code Highlighter .
サンプルクラスProjectUrlGenerator Symfony 2:
<?php
/**
* ProjectUrlGenerator
*
* This class has been auto-generated
* by the Symfony Routing Component.
*/
class ProjectUrlGenerator extends Symfony\\Components\\Routing\\Generator\\UrlGenerator
{
/**
* Constructor.
*/
public function __construct(array $context = array(), array $defaults = array())
{
$ this ->context = $context;
$ this ->defaults = $defaults;
}
public function generate($name, array $parameters, $absolute = false )
{
if (!method_exists($ this , $method = 'get' .$name. 'RouteInfo' ))
{
throw new InvalidArgumentException(sprintf( 'Route "%s" does not exist.' , $name));
}
list($variables, $defaults, $tokens) = $ this ->$method();
return $ this ->doGenerate($variables, $defaults, $tokens, $parameters, $name, $absolute);
}
protected function getaddTopicRouteInfo()
{
return array(array (), array_merge($ this ->defaults, array ( 'module' => 'blogsEdit' , 'action' => 'addTopic' ,)), array ( 0 => array ( 0 => 'text' , 1 => '/' , 2 => '' , 3 => NULL, ), 1 => array ( 0 => 'text' , 1 => '/' , 2 => 'add-topic' , 3 => NULL, ), 2 => array ( 0 => 'text' , 1 => '/' , 2 => 'blogs' , 3 => NULL, ),));
}
protected function geteditTopicRouteInfo()
{
return array(array ( 'id' => ':id' ,), array_merge($ this ->defaults, array ( 'module' => 'blogsEdit' , 'action' => 'editTopic' ,)), array ( 0 => array ( 0 => 'text' , 1 => '/' , 2 => '' , 3 => NULL, ), 1 => array ( 0 => 'variable' , 1 => '/' , 2 => ':id' , 3 => 'id' , ), 2 => array ( 0 => 'text' , 1 => '/' , 2 => 'edit-topic' , 3 => NULL, ), 3 => array ( 0 => 'text' , 1 => '/' , 2 => 'blogs' , 3 => NULL, ),));
}
protected function getthemeBlogRouteInfo()
{
return array(array ( 'name' => ':name' ,), array_merge($ this ->defaults, array ( 'module' => 'blogs' , 'action' => 'blog' ,)), array ( 0 => array ( 0 => 'text' , 1 => '/' , 2 => '' , 3 => NULL, ), 1 => array ( 0 => 'variable' , 1 => '/' , 2 => ':name' , 3 => 'name' , ), 2 => array ( 0 => 'text' , 1 => '/' , 2 => 'blogs' , 3 => NULL, ),));
}
}
* This source code was highlighted with Source Code Highlighter .
Symfony 2のApacheのルールのダンプ例:
RewriteCond %{PATH_INFO} ^/blogs/add\-topic/$
RewriteRule .* index.php [QSA,L,E=_ROUTING__route:addTopic,E=_ROUTING_module:blogsEdit,E=_ROUTING_action:addTopic]
RewriteCond %{PATH_INFO} ^/blogs/edit\-topic/([^/\.]+?)/$
RewriteRule .* index.php [QSA,L,E=_ROUTING__route:editTopic,E=_ROUTING_id:%1,E=_ROUTING_module:blogsEdit,E=_ROUTING_action:editTopic]
RewriteCond %{PATH_INFO} ^/blogs/([^/\.]+?)/([^/\.]+?)/$
RewriteRule .* index.php [QSA,L,E=_ROUTING__route:topicInThemeBlog,E=_ROUTING_name:%1,E=_ROUTING_id:%2,E=_ROUTING_module:blogs,E=_ROUTING_action:topic]
* This source code was highlighted with Source Code Highlighter .
結論:
Symfonyの2番目のブランチでは、ルーティングシステムが完全に書き直されています。 かさばると思っていました。 今、私は言葉を取り戻します。 開発者が生成されたファイルを最適化し、Apacheでルールをダンプできるようになったことは非常に楽しいことです。 ルーティングシステムをフレームワークとは別に使用できるという事実は、間違いなくプラスです! 私にとって、そのようなコンポーネントは開発者の生活を向上させます!