Question

I'm attempting to extend Lithium PHP with smarty, everything is working except my smarty plugin to allow access to lithium helpers.

My smarty function is meant to be a "catchall" for lithium helpers. The part I'm struggling with is dynamically adding the helpers parameters to my callback.

Simplified Code

$params = array('Google', 'http://www.google.com');

$helper = function($args){  
    return $this->html->link($args);
};

return call_user_func_array($helper, $params);

This results with <a href="/Google">Google</a>

Some further testing showed that the above only passed the first item in the $params array to the callback... strange. So I wrapped it in its own array. Take that, thing I cant explain!

return call_user_func_array($helper, array($params));

This produces a lithium error which I wont get into, however it appears that the helper is being passed an array, not translated arguments from the array as individual parameters.

Error snippet 'No parameter match found for URL ( 0 => 'Google', 1 => 'http://www.google.com', 'controller' => 'pages', 'action' => 'index').

So I gather that I'm missusing the call_user_func_array method.

I'm not sold on this being the only solution to achieve my end, if you can think of a better way to dynamically pass a function any number of arguments at any time I would be greatful.

Please note: $this->html->link is a simplified method to explain my issue, the html and link parts of that object will actually dynamically changed, hence the requirement for completely dynamic function arguments.

Was it helpful?

Solution

$helper is declared with only one argument and only one is transfered to $this->html->link.

Why not use call_user_func_array(array($this->html, 'link'), array($params)); directly?

OTHER TIPS

Why don't you just do

return $helper($params);

No need for call_user_func_array() in my opinion.
Or please clarify why you want this, because for me this works.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top