zf2中的SEO友好URL

时间:2013-05-28 06:34:01

标签: php routing zend-framework2

我通过这种方式重定向。我想我这样做错了,请指导我更好的方式。

<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 个答案:

答案 0 :(得分:2)

让我们先改变您的路线以匹配您想要的路线:

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

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

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

我还建议您使用小写路由名称。

修改

要摆脱开头的 / restaurantlist ,你必须将 view 路由放在它之外。

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

答案 1 :(得分: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>