0

Why params plugin magic function default forwarding route?

Why zf not able to get function from parameter like sample below?

public function __invoke($param = null,$type='Route', $default = null)
{
    if ($param === null) {
        return $this;
    }
    $type = 'from'.ucfirst($type); 
    // Need to check function exist, if not must throw some error
    return $this->$type($param, $default);
}

Example use

$this->params('name','post');
$this->params('token','get');
$this->params('action'); // this will return from route like default one.

How can i extend default params plugin like this? And is it a good move?

ugurerkan
  • 790
  • 7
  • 17

1 Answers1

0

The default params are always fetched from the Route. This is because ZF2 encourages people to do lot of manual routing. This is both for speed purposes, as well as SEO purposes.

$this->params('paramname', 'defaultValueIfNotFound');

In the cases where you need the params from specific regions of the request, you can do that, too, using the params-plugin itself. But this has already been explained greatly by @Matsemann

You could extend the params plugin, but you should use the third parameter for the additional option. Bear in mind though, that the other approach is simply cleaner and ultimately requires less work by the plugin. No switch-statement just to get a param ;)

Community
  • 1
  • 1
Sam
  • 16,435
  • 6
  • 55
  • 89
  • Thank you Sam. I need to learn more zf2 way solve :) I am reading your blog posts and i found this https://github.com/zendframework/zf-web project, i think it will help me to learn more. – ugurerkan Apr 16 '13 at 12:26