0

I am redirecting by this way. I think I'm wrong in this way please guide me better way.

<a href="<?php echo $this->serverUrl()."/restaurantlist/view/".$list['id']."/".$list['name']?>">
   <img  alt="" src="<?php echo $image; ?>">
</a> 

my code of 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',
    ),
),

);

it is redirecting like this http://test.localhost.com/restaurantlist/view/157/Bluestem and works perfectly fine but I want this http://test.localhost.com/Bluestem or http://test.localhost.com/157-Bluestem

I tried a lot but no success.

4

2 回答 2

2

Let's first change your route to match your desired route:

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

Then in your view you use the route path to create the right url.

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

I would also like to recommend you to use lowercase route names.

EDIT

To get rid of the beginning /restaurantlist you have to put the view route outside of it.

'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',
                ),
            ),
        ),
    ),
),

In your view:

echo $this->url('restaurantlist-view', array('id' => $list['id'], 'name' => $list['name']));
于 2013-05-28T06:41:33.497 回答
0

Hope it will help you

In your route file

//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>
于 2013-05-28T07:00:59.100 回答