[英]SEO friendly URL in zf2
我通过这种方式重定向。 我想我这样做错了请指导我更好的方法。
<a href="<?php echo $this->serverUrl()."/restaurantlist/view/".$list['id']."/".$list['name']?>">
<img alt="" src="<?php echo $image; ?>">
</a>
我的module.config.php代码
<?php
return array(
'controllers' => array(
'invokables' => array(
'Restaurantlist\Controller\Restaurantlist' => 'Restaurantlist\Controller\RestaurantlistController',
),
),
'router' => array(
'routes' => array(
'Restaurantlist' => array(
'type' => 'segment',
'options' => array(
// Change this to something specific to your module
'route' => '/restaurantlist[/]',
'defaults' => array(
'controller' => 'Restaurantlist\Controller\Restaurantlist',
'action' => 'list',
),
),
'may_terminate' => true,
'child_routes' => array(
'view' => array(
'type' => 'Segment',
'options' => array(
'route' => '[/]view[/:id][/:name][/]',
'constraints' => array(
'name' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Restaurantlist\Controller\Restaurantlist',
'action' => 'view',
),
),
),
),
),
),
),
'view_helpers' => array(
'factories' => array(
'Requesthelper' => function($sm){
$helper = new \Restaurantlist\View\Helper\Requesthelper;
$request = $sm->getServiceLocator()->get('Request');
$helper->setRequest($request);
return $helper;
}
)
),
'view_manager' => array(
'template_path_stack' => array(
'Restaurantlist' => __DIR__ . '/../view',
),
),
);
它重定向像这样http://test.localhost.com/restaurantlist/view/157/Bluestem并且工作得非常好,但我想要这个http://test.localhost.com/Bluestem或http://test.localhost。 COM / 157-须芒草
我尝试了很多但没有成功。
让我们首先更改您的路线以匹配您想要的路线:
'route' => '/[:id]-[:name]',
然后在您的视图中使用路径路径来创建正确的URL。
echo $this->url('Restaurantlist/view', array('id' => $list['id'], 'name' => $list['name']));
我还建议您使用小写路由名称。
编辑
要摆脱开头/餐馆列表,您必须将视图路径放在其外面。
'router' => array(
'routes' => array(
'restaurantlist-view' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:id]-[:name]',
'constraints' => array(
'name' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Restaurantlist\Controller\Restaurantlist',
'action' => 'view',
),
),
),
),
),
在你看来:
echo $this->url('restaurantlist-view', array('id' => $list['id'], 'name' => $list['name']));
希望它会对你有所帮助
在您的路线文件中
//by placing other custom routes before (:any) will prevent to match any url
$route['other/(:any)'] = 'mypage/other';
// Other routes as needed...
$route['(:any)'] = '/restaurantlist/view/$1';
HTML
<a href='<?php echo site_url($list['name']);?>'>Click</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.