简体   繁体   中英

Zend pass variable to view

I'm new to Zend, before used laravel and other frameworks. So i got one Zend project to modify, the problem is that i can't pass variables from controller to view.

Controller:

$temp_id = $this->_request->getParam ('temp_id');

if($temp_id){
    $data = $card_temp_model->getSingle('*', 'id='.(int)$temp_id);

    if(!$data['icon_id'])
        $data['icon_id'] = 9;
    if(!$data['icon2x_id'])
        $data['icon2x_id'] = 10;
    $data['template_only']=1;

    $form->setDefaults($data);
}

$locations = $card_location_model->getAll('*', 'card_id='.(int)$temp_id);

$this->view->locations = $locations;
$this->view->form = $form;

And my view file:

<?=
    die(print_r($this->locations));
?>

I can't get locations in my view file. Someone maybe can explain me why?

To assign the variable to view in ZF you should use this code

$this->view->var = "some text"; // where "some text" may be variable or everthing else.

Your problem is rather in placing your files. ZF directory structure can be changed so if you use directory structure.

APPLICATION
  controllers
     NameController.php
Views
  Name
     Index.html

then in Index.html just echo $var ;

in NameController.php create IndexAction() method and assign variable to view.

To show this controler use domain.com/name/ or domain.com/name/index

That's all to make it works.

In the controller ...

$this->view->foo = 'sample string or content';

In the view use the escape method to prevent xss issues.

echo $this->escape($this->foo);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM