Zend_Controller_Router_Route_Chain
Zend_Controller_Router_Route_Chainは、
複数のルートを一緒にチェーンできるルートです。
これは、たとえばホスト名とルート、パスとルート、または複数のパスとルートをチェーンできます。
チェインは、プログラム的に、または、構成ファイルの範囲内で行なえます。
パラメータ優先度
When chaining routes together, the parameters of the outer route
have a higher priority than the parameters of the inner route. Thus
if you define a controller in the outer and in the inner route,
the controller of the outer route will be selected.
When chaining programatically, there are two ways to achieve this. The
first one is to create a new
Zend_Controller_Router_Route_Chain instance and then
calling the chain() method multiple times with all routes
which should be chained together. The other way is to take the first
route, e.g. a hostname route, and calling the chain()
method on it with the route which should be appended to it. This
will not modify the hostname route, but return a new instance of
Zend_Controller_Router_Route_Chain, which then has both
routes chained together:
chain($hostnameRoute)
->chain($pathRoute);
// Second way, chain them directly
$chainedRoute = $hostnameRoute->chain($pathRoute);
]]>
When chaining routes together, their separator is a slash
by default. There may be cases when you want to have a different
separator:
chain($secondRoute, '-');
// Assemble the route: "foo-bar"
echo $chainedRoute->assemble();
]]>
Zend_Configを介したルートのチェイン
To chain routes together in a config file, there are additional
parameters for the configuration of those. The simpler approach is
to use the chains parameters. This one is simply a list
of routes, which will be chained with the parent route. Neither the
parent- nor the child-route will be added directly to the router but
only the resulting chained route. The name of the chained route in
the router will be the parent route name and the child route name
concatenated with a dash (-) by default. A simple config in XML
would look like this:
www.example.com
:language
imprint
users.example.com
:username
misc
]]>
This will result in the three routes www-language-index,
www-language-imprint and
users-language-profile which will only match based on
the hostname and the route misc, which will match with
any hostname.
The alternative way of creating a chained route is via the
chain parameter, which can only be used with the
chain-route type directly, and also just works in the root level:
www.example.com
:language
imprint
www, language, index
www, language, imprint
]]>
You can also give the chain parameter as array instead
of separating the routes with a comma:
www
language
index
www
language
imprint
]]>
When you configure chain routes with Zend_Config and
want the chain name separator to be different from a dash, you
need to specify this separator separately:
array(
'type' => 'Zend_Controller_Router_Route_Static',
'route' => 'foo',
'chains' => array(
'subRouteName' => array(
'type' => 'Zend_Controller_Router_Route_Static',
'route' => 'bar',
'defaults' => array(
'module' => 'module',
'controller' => 'controller',
'action' => 'action'
)
)
)
)
));
//構成追加前にセパレータを設定
$router->setChainNameSeparator('_separator_')
//構成を追加
$outer->addConfig($config);
//そしてルート名はこうなります: chainName_separator_subRouteName
echo $this->_router->assemble(array(), 'chainName_separator_subRouteName');
//検証: /foo/bar をエコーします。
]]>