How to output the content of a variable in Zend Framework 2?

Home Forums Learn ZF2 How to output the content of a variable in Zend Framework 2?

This topic contains 1 reply, has 2 voices, and was last updated by  Slavey Karadzhov 2 years, 5 months ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #646

    yerbaguy@gmail.com
    Participant

    I have started to learn Zend Framework 2 and I’ve got a problem with how to output the content of a variable from a class.

    I wrote a simple class like below:

    ` <?php
    namespace example\Model;

    class example{

    protected $name = “sth”;

    public function show_name(){
    echo $this-> name;
    }

    }
    `

    I instantiated it like below, in the Module.php file:

    `
    public function getServiceConfig()
    {
    return array(
    ‘factories’ => array(
    // ‘example\Model\example’ => function($sm){
    ‘example’ => function($sm){
    // $example_ = new example();
    $example_ = new example\Model\example();
    return $example_;
    },
    ),
    );
    }
    `

    I wrote a controller like below:

    `
    namespace example\Controller;

    use Zend\Mvc\Controller\AbstractActionController;
    use Zend\View\Model\ViewModel;

    class IndexController extends AbstractActionController
    {

    protected $show_example_;

    public function indexAction()
    {

    return new ViewModel(array(‘example’ => $this->show_example()));

    // return array();
    }

    public function show_example()
    {
    if(!$this->show_example_){
    $this->show_example_ = $sm->get(‘example\Model\exmaple’);
    }
    return $this->show_example_;
    }
    `
    I also wrote an index.phtml:

    `
    <?php

    echo $example;

    ?>
    `

    Could I please ask you to help me with this?

    #647

    Slavey Karadzhov
    Participant

    Hi yerbaguy,
    As far as I understand you are not able to see the value “sth” printed in the template? If yes, would it be possible to put the code somewhere in github and I can take a look at it send you explanation as changes in the code.

    Regards,
    Slavey

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.