views:

66

answers:

3

hi i have a simple question

where is my code wrong ?

in index controller and index action

i put

$this->view->username="user1";

and when i try in my layout i use

echo $this->username;

i got fllowing error or null value

Notice: Trying to get property of non-object in D:\Zend\Apache2\htdocs\test\application\layouts\layout.phtml on line 115

thanks

A: 

Have you tried echo $this->view->username?

Joe D
A: 

Your layout, where you are trying to echo your variable, is not your view.

Essentially, it can't be. Let me explain: Your layout is a global, well, layout. Something that's specific for IndexController's indexAction() is (/ should be) logically unavailable in the global layout, since your layout will not have this variable if you end up calling, say, FooController's barAction().

Your view would be at views/scripts/index/index.phtml - that's where you can use <?php echo $this->username; ?>

I haven't worked with variables in layouts yet, but this is what I'm gleaning from the documentation: If you want your layout to show a variable, try using $this->_helper->layout->username = "user1" in your action, that should let you use echo $this->layout()->username in the layout.

Be careful, though, if you do use layout variables, you also have to set them in each action. If you just want your indexAction() to show the username, you should try putting your echo into your view.

pinkgothic
it does not work , it shows nullanybody may help please?
user1400
Which part does not work? Using index.phtml, or using layout variables?
pinkgothic
i try to show username in my layout.phtmlbut it shows null;
user1400
+1  A: 

the same question is here maybe help you

ulduz114
but why it does not not work when i use $this->view->username="user1"; and echo $this->usename;
user1400