Gotcha: Zend url($urlOptions) prints /”Array”

Ok…this is one for the album.

I’m learning Zend Framework (version 1.7.1) and doing some hacking.

I had read up on Zend’s Router Operations and was tinkering and trying to assemble an URL from within an Action Controller script.

In my bootstrap I had added the following line of code to set up a route

$router = $front->getRouter(); // returns a rewrite router

$router->addRoute(
'trial',
new Zend_Controller_Router_Route('trial/:username/:width‘,
array(’controller’ => ‘Trial’,
‘action’ => ‘index’)));

In my action script i was attempting to assemble an URL based on $urlOptions.

like so:


TrialController extends Zend_Controller_Action{

...
$urlOptions['username‘]=’jim’;
$urlOptions[’width‘]=’verywide’;

echo ‘url NOT assembled correctly :’ .

$this->_helper->url($urlOptions,’trial’,TRUE);

This was producing:


url NOT assembled correctly :/trial/Array

I.e. The $urlOptions appeared to be being treated like a string!

After much faffing…. i realised that I was calling upon the wrong method to assemble an url.

The line above was, in fact, calling the url Action Helper:
Zend_Controller_Action_Helper_Url::direct($action, $controller = null, $module = null, array $params = null)

I was doing a school boy error and thought I was calling the url View Helper:
Zend_View_Helper_Url::url(array $urlOptions = array(), $name = null, $reset = false, $encode = true)

So, to fix it, i had to change the code in my controller to be like so:


TrialController extends Zend_Controller_Action{

...

$urlOptions['username']='jim';
$urlOptions['width']='verywide';

echo 'url IS assembled correctly :' .
$this->view->url($urlOptions,’trial’,TRUE);

Which outputs correctly:


url IS assembled correctly :/trial/jim/verywide

Bear in mind: The Action Helper Url does actaully have an url() method :
url($urlOptions = array(), $name = null, $reset = false, $encode = true)

But that cannot be used directly. You have to get the object from the Action Helper Broker and use it like so:


$url_action_helper = $this->_helper->getHelper(’Url’);
//or: u could do:  $url_action_helper = $this->_helper->Url;

echo ‘url IS assembled correctly :’
.$url_action_helper->url($urlOptions,’trial’,TRUE);

See Docs on Action Helpers (I can’t spot any mention of Zend_Controller_Action_Helper_Url on that page, but there is an example of it being used here )

See Docs On View Helpers

Also Note: the reason why i pass TRUE as the third arg was purely a product of ‘pulling-my-hair-out-hacking’ rather than logical reasoning. So, it might not have been necessary. (I had hastily grabbed that idea from here)

Leave a Reply