0

Is there easy way how to generate url using view url helper in format where custom params (except controller and action) will be after "?" ?

There is example:

<?php echo $this->url(array('controller' => "index", 'action' => "index", 'myParam' => "myValue")); ?>

This will generate:

domain.com/index/index/myParam/myValue

I want

domain.com?myParam=myValue or

domain.com/{controller}/{action}?myParam=myValue

1
  • 3
    Why do you want in this way? Is there any problem you are facing with this url structure ? Commented May 10, 2011 at 9:47

1 Answer 1

1

You can use the reverse field of a Regex route for example :

   $myroute = new Zend_Controller_Router_Route_Regex('\?myParam=(.*)',
                    array(1 => 'defaultValue', 'controller' => 'index', 'action' => 'index', 'module' => 'index'),
                    array(1 => 'myValue'),
                    '?myParam=%s'
    );

   $wwwDomainRoute = new Zend_Controller_Router_Route_Hostname(
                    'www.domain.com');

   $plainPathRoute = new Zend_Controller_Router_Route_Static('');

   //this default route is useful for the default routing
   $router->addRoute('wwwroute', $wwwDomainRoute->chain($plainPathRoute));
   $router->addRoute('myroute', $wwwDomainRoute->chain($myroute));

and in your view just use :

   <?php echo $this->url(array('controller' => "index", 'action' => "index", 'myParam' => "myValue"),'myroute'); ?>

note : be careful with your regex...

Sign up to request clarification or add additional context in comments.

3 Comments

Certainly, a literal read of the OP's question suggests that he wants the url() call to return a complete url with the domain.com portion. And under this interpretation, your answer correctly includes the hostname routing. But I suspect his interest is more about adding params to the query string. If so, then we could simplify the answer by removing the hostname routing and simply prepend a baseUrl(). Perhaps we should clarify the OP's intention?
You are certainly right, but, this solution is more robust, and my response was influenced by the fact he puts the domains in his example request. His question was more about how to format url (and parameters), and using Zend_Router is the solution. Regards
For sure, using routes is the way to go. To my read, the guts of it are as you have described with the regex routing. I'm just wondering whether adding in the hostname routing, while certainly more robust, makes it appear more complicated. Anyway, just being a pain in the neck and splitting hairs. Nice answer. Cheers! ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.