On the creation of variables

Asked

Viewed 72 times

0

I use ZEND 2 and I am in the controller. I created an action to print a field (description) of a table (receipt). The point is that when I connect the controller to the view nothing appears on the site. I realized I have to set the value of the variable $saidarecibo assigning to it the value of the table field that calls Description. How do I do this?

Below is my controller:

 public function imprecibosaidaAction() {

        $data_parametro = "";

        $saidarecibo = $descricao;

        $id = (int) $this->params()->fromRoute('id', 0);

        $saidarecibo = $this->getTable('Admin\Model\Saidarecibo')->get($id);

        //$this->view->saidarecibo = $saidarecibo;

        if ($id == 0) {
            throw new \Exception("Código obrigatório");
        }

         // Turn off the layout, i.e. only render the view script.
         $viewModel = new ViewModel();
         $viewModel->setTerminal(true);

        $view = new ViewModel(array(
            'saidarecibo' => $saidarecibo,
        ));

        //return $view;
        return $viewModel;

    }

Now follow the view I created, it should print only the table field:

<?php echo $this->saidarecibo; ?>

<a style="margin-top:10px;" href="javascript:self.print()">IMPRIMIR</a>
  • It seems that you are trying to echo an undefined template variable, since you defined it in $viewModel, but returned $view.

2 answers

1

You have two things wrong. One in the controller: You’re doing return of a different variable:

//estás a fazer return $viewModel; e devia ser 
return $view;

In the action you also have the variable used in the wrong way: Try using $saidarecibo instead of $this->saidarecibo In the action the object array key viewModel sets the name of the variable to use in the view.

0

The correct one when using the output $viewModel->setTerminal(true); would assign the return variables in the same instance as follows:

$viewModel = new ViewModel();

$viewModel->setTerminal( true )->setVariables(array( 'saidarecibo' => $saidarecibo));

return $viewModel;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.