views:

280

answers:

1

I am using the standard router in Zend Framework to route most URLs in the system.

For one particular URL, I want to access it via a controller alias (in addition to the real controller)

For example.

Actual URL:

/mymodule/mycontroller/myaction/*

Alias URL:

/mymodule/mycontrolleralias/myaction/*

mycontrolleralias is not a real controller, but I want any requests to it to route to mycontroller (query params and all)

To do this, I have tried to setup this route:

$router->addRoute('controlleralias', new Zend_Controller_Router_Route(
    ':module/mycontrolleralias/:action/*',
    array(
        'module'     => 'mymodule',
        'controller' => 'mycontroller',
        'action'     => 'myaction'
    )
));

But in my view helper if I try to create a URL:

$this->view->url(array('sort' => array('param1','param2')));

I get an error:

urlencode() expects parameter 1 to be string, array given

#0 [internal function]: __lambda_func(Array)
#1 /library/Zend/Controller/Router/Route.php(398): urlencode(Array, false, true)
#2 /library/Zend/Controller/Router/Rewrite.php(441): Zend_Controller_Router_Route->assemble(Array, NULL, false, true)
#3 /library/Zend/View/Helper/Url.php(49): Zend_Controller_Router_Rewrite->assemble(Array)

If I remove the star (*) from my custom route, no error will occur - but the urls generated in my view are then not correct because they will not be matching the query parameters:

$router->addRoute('controlleralias', new Zend_Controller_Router_Route(
    ':module/mycontrolleralias/:action',
    array(
        'module'     => 'mymodule',
        'controller' => 'mycontroller',
        'action'     => 'myaction'
    )
));

I have also found that if I don't pass in an array for a parameter, it will not generate an error:

$this->view->url(array('sort' => 'param1'));

Unfortunatly I do need to pass in an array for 'sort' (as shown above)

Does anyone know what I'm doing wrong? Perhaps there is an easier way to achieve this?

I am using Zend Framework 1.9.0

A: 

I think you should do something like that,

$this->view->url(array('sort' => 'param1,param2'),'controlleralias');

Also parameters cannot be arrays since the are based to urlencode as this.

RageZ
Thanks, but it still gives the error. I have also found if I don't pass an array of values for 'sort' it will also work (see updated question)
asgeo1
yeah sorry also make sense the params cannot be arrays
RageZ
The standard router does support arrays for the parameter values, and will generate the correct URL.But if I use a commar-seperated value, it won't build a correct URL. I.e. I get /mymodule/mycontrolleralias/myaction/sort/param1%2Cparam2 instead of /mymodule/mycontrolleralias/myaction/sort/param1/sort/param2It's just Zend_Controller_Router_Route that seems to treat them differently and throws the error when I'm trying to build my alias route :(
asgeo1