0

In Module.php I have a some code (simplified version):

namespace Application;

use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;

class Module{
    public $somevariable = 'test';
    public function onBootstrap( MvcEvent $e ) {
        $this->somevariable = 'test2';
    }
    public function getValue(){
        return $this->somevariable;
    }
}

Next, I want to get value from variable "somevariable" in template layout.phtml. I do this as follows:

echo Application\Module::getValue();

but this doesn't work. What is wrong with that?

P.S. I never programmed much in PHP, so maybe I missed something :-(

2 Answers 2

1

you can use

$e->getViewModel()->setVariable('somevariable', 'somethingvalue');

and in the view :

echo $this->layout()->somevariable;

for detail, see this article : http://samsonasik.wordpress.com/2012/07/27/zend-framework-2-mvcevent-layout-view-get-namespace/

Sign up to request clarification or add additional context in comments.

1 Comment

Ok, seems that way of passing variables working well in that case. Thanks.
0

If a variable is just a string it doesn't make much sense to go with that approach. And please don't take this offensively, but if you don't have much experience in PHP (you tried to call a static function that is not static), then i wonder why you would start learning PHP with such a high class framework.

And if still you insist on doing that, please follow the official Documentation and read yourself through the whole QuickStart again and again. Check out some of the Modules out there and see how they do stuff.

Try to do the easy stuff first until you hit those points where you really need such functionality.

1 Comment

"If a variable is just a string it doesn't make much sense to go with that approach." - maybe, but somehow the value of property must be passed. "if you don't have much experience in PHP (you tried to call a static function that is not static), then i wonder why you would start learning PHP with such a high class framework." - because I worked with ZF1 little bit, but very long time ago. Just want to refresh some knowledge and go forward. I think that there is nothing wrong to use ZF, even if this is high class framework.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.