-- PHPScriptor <[email protected]> wrote
(on Saturday, 14 February 2009, 07:23 AM -0800):
>
> Ok, maybe I'm totally wrong but can I do something like this and how:
>
> class MyController extends Zend_Controller_Action
> {
> public $value
>
> public function __construct() {
> if($this->_request->getParam('value'))
> $this->value = $this->_request->getParam('value');
> else
> $this->value = 0;
> }
>
> ...
> }
>
> is this possible? I want to set my $value so that I can use it in all my
> actions (when called). But the value can be a default (0) or it can be
> passed by getParam. Don't know if this is the correct way. Maybe someone can
> help me with this.
There are two problems here. First, don't override the constructor.
You're doing it wrong above, because it's not following the signature of
Zend_Controller_Action::__construct(), which is why you're getting the
issues you have below (it expects a request object, response object, and
an array of parameters injected from the dispatcher). We provide another
method you can call if you need specialized initialization: init():
class MyController extends Zend_Controller_Action
{
public function init()
{
/* ... */
}
}
Second, there are two better ways to do this. One is to use the
controller's _getParam() method with a second argument indicating the
default to use, and the second is to do as you do above, but, again,
passing the second argument, which indicates a default value; I'd
personally use _getParam() as it'll lead to more succinct syntax.
class MyController extends Zend_Controller_Action
{
public function init()
{
$this->value = $this->_getParam('value', 0);
}
}
The above should accomplish what you're expecting.
> when I left out all the thing with getParam I get:
> Fatal error: Call to a member function notifyPreDispatch() on a non-object
> in C:\wamp\php\Zend\Controller\Action.php on line 490
>
> when I do it with the getParam I get:
> Fatal error: Call to a member function getParam() on a non-object in
> C:\wamp\www\app\modules\my\controllers\MyController.php on line 16
--
Matthew Weier O'Phinney
Software Architect | [email protected]
Zend Framework | http://framework.zend.com/