Zend_Controller_Router_Route_Regex Additional parameters with reverse

I have successfully created my route using Regex. I have some additional parameters in my route that I don't want to show in the URL helper unless the user has specified them. How can i do this?

This is what I currently have

        $route = new Zend_Controller_Router_Route_Regex(
        '([a-zA-Z-_0-9-]+)-Widgets(?:/page/(\d+))?(?:/limit/(\d+))',
        array(
            'controller'    => 'widget',
            'action'        => 'list',
        ),
        array(
            1 => 'color',
            2 => 'page',
            3 => 'limit'

        ),
        '%s-Widgets/'
    );

    $router->addRoute('color_widgets', $route);

      

Then I call the helper url with the following code

        echo $this->url(array('page' => $page), 'color_widgets', false); 

      

This results in / Blue-Widgets / and doesn't post the page to the url. I can fix this by changing the reverse in the router

    $route = new Zend_Controller_Router_Route_Regex(
        '([a-zA-Z-_0-9-]+)-Widgets(?:/page/(\d+))?(?:/limit/(\d+))',
        array(
            'controller'    => 'widget',
            'action'        => 'list',
            'page'      => 1
        ),
        array(
            1 => 'color',
            2 => 'page',
            3 => 'limit'

        ),
        '%s-Widgets/page/%d'
    );

      

However, this does not solve my problem: I have Url

/ Blue-Widgets / page / 1 / limit / 10 The limit is not displayed, again I can fix it with the following

    $route = new Zend_Controller_Router_Route_Regex(
        '([a-zA-Z-_0-9-]+)-Widgets(?:/page/(\d+))?(?:/limit/(\d+))',
        array(
            'controller'    => 'widget',
            'action'        => 'list',
            'page'      => 1,
            'limit'     => 10
        ),
        array(
            1 => 'color',
            2 => 'page',
            3 => 'limit'

        ),
        '%s-Widgets/page/%d/limit/%d'
    );

      

The problem with this is that the user is on / Blue-Widgets / and I want to move them to the next Blue Widgets page with the following code

        echo $this->url(array('page' => $page), 'color_widgets', false); 

      

They are actually taken to / Working Widgets / page / 2 / limit / 10

When I Really Want / Working Widgets / Page / 2

How can I accomplish this using Zend Framework.

+3


source to share


2 answers


You cannot use the reverse regex route with a variable number of values. You could:

  • Write a different route for each optional parameter (not recommended)
  • Use a different route structure

You can, for example, change your route to:



$route = new Zend_Controller_Router_Route(
    'widgets/:color/*',
    array(
        'controller'    => 'widget',
        'action'        => 'list',
        'page'      => 1,
        'limit'     => 10
    ),
    array(
        'color' => '[a-zA-Z-_0-9-]+',
        'page' => '\d+',
        'limit' => '\d+',
    )
);

      

Another option is to create your own custom route class that can parse and build the correct uri.

+2


source


You have specified the wrong indices of the Regex match variables, which is why you get strange results. Your code should look like this:



$route = new Zend_Controller_Router_Route_Regex(
'([a-zA-Z-_0-9-]+)-Widgets(?:/page/(\d+))?(?:/limit/(\d+))',
array(
    'controller'    => 'widget',
    'action'        => 'list',
),
array(
    1 => 'color',
    3 => 'page',
    5 => 'limit'
),
'%s-Widgets/'
);

$router->addRoute('color_widgets', $route);

      

0


source







All Articles