[英]ZF2 - custom and dynamic URL
我想从定制URL动态路由到特定的操作和控制器。 例如,我有下表:
controller | action | alias
=============================
home | index | myalias
而且我想在路由(我不确定)之前附加一些功能,该功能将检查URL,以及是否会
my-site.com/myalias
那么应该使用控制器的home和action 索引而不进行重定向。 只需打开该动作和控制器,而无需更改URL。 有可能实现吗?
到目前为止,我有这样的事情:
public function customRoutes(MvcEvent $e)
{
$dbAdapter = $e->getApplication()->getServiceManager()->get('Zend\Db\Adapter\Adapter');
// pobierz routingi
$rowset = $dbAdapter->query("SELECT * FROM public.seo WHERE ghost IS NOT TRUE AND alias IS NOT NULL")->execute();
$routeName = 'custom_route_';
$i = 1;
if (sizeof($rowset) > 0) {
foreach ($rowset as $item) {
if (strpos($item['alias'], '/') !== 0) {
$alias = '/' . $item['alias'];
} else {
$alias = $item['alias'];
}
$route = \Zend\Mvc\Router\Http\Segment::factory(array(
'route' => $alias . '[/:id][/:page]',
'constraints' => array(
'id' => '[0-9a-zA-Z]+',
'page' => 'page\-[a-zA-Z0-9_-]*',
),
'defaults' => array(
'controller' => $item['controller'],
'action' => $item['action'],
),
));
$e->getRouter()->addRoute($routeName . $i, $route);
$this->custom_routes[] = $routeName . $i;
$i++;
}
}
}
现在,我需要更改url帮助程序以从我的自定义路由创建链接。 例如,如果我有:
$this->url('home');
结果链接应该是(因为别名在数据库中):
/myalias
除了:
/home
我不了解ZF2,但是我已经在ZF1中做过多次了……您只需要使用硬编码的控制器和操作来设置自定义路由。 这是我在ZF2文档中发现的(针对您的情况进行了修改):
// In bulk:
$router->addRoutes(array(
// providing configuration to allow lazy-loading routes:
'bar' => array(
'type' => 'literal',
'options' => array(
'route' => '/myalias',
'defaults' => array(
'controller' => 'home',
'action' => 'index',
),
),
),
));
ZF2 DocRef: http ://framework.zend.com/manual/2.0/en/modules/zend.mvc.routing.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.