2013-05-28 102 views
0

我正在通过这种方式进行重定向。我想我是这样错了,请引导我更好的方式。zf2中的SEO友好URL

<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/Bluestemhttp://test.localhost.com/157-Bluestem

我尝试了很多,但没有成功。

回答

2

让我们先改变你的路线,以符合您需要的路线:

'route' => '/[:id]-[:name]', 

然后在您的视图,您使用的路线路径创建正确的网址。

echo $this->url('Restaurantlist/view', array('id' => $list['id'], 'name' => $list['name'])); 

我还想推荐你使用小写路由名。

编辑

为了摆脱年初/restaurantlist你必须把视图路线的它之外。

'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'])); 
+0

是的,它几乎是正确的,但它仍然是“餐馆列表”请参阅链接http://test.localhost.com/restaurantlist/157-Bluestem是否可以像这样http://test.localhost .com/157-Bluestem –

+0

@RajatModi看到我的编辑:-) –

+0

Kuiperes是的,你是对的,但我的默认行动是列表行动我不能通过这种方式改变我必须在子路线只写下这个逻辑:( –

0

希望它会帮助你

在你的路由文件

//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>