1

I am trying to understand the MVC method with the use of OOP. However it seems like I've hit the wall here.

I am trying to pass multiple objects to the view. But all I can do so far is pass just one object. The ideal result would be passing multiple objects, while keeping the names that are assigned to them in the controller.

The render, start and end functions in the View class go something like this:

public function render($viewName, $data){
    $viewAry = explode('/', $viewName);
    $viewString = implode(DS, $viewAry);
    if(file_exists(ROOT . DS . 'app' . DS .  'views' . DS . $viewString . '.php')){
        include(ROOT . DS . 'app' . DS .  'views' . DS . $viewString . '.php');
        include(ROOT . DS . 'app' . DS . 'views' . DS . 'layouts' . DS . $this->_layout . '.php');
    }else{
        die('The view \"' . $viewName . '\" does not exist.');
    }
}

public function content($type){
    if($type == 'head'){
        return $this->_head;
    }elseif ($type == 'body'){
        return $this->_body;
    }
    return false;
}

public function start($type){
    $this->_outputBuffer = $type;
    ob_start();
}

public function end(){
    if($this->_outputBuffer == 'head'){
        $this->_head = ob_get_clean();
    }elseif($this->_outputBuffer == 'body'){
        $this->_body = ob_get_clean();
    }else{
        die('You must first run the start method.');
    }
}

And this is how would the controller look like:

public function indexAction(){
    $items =  $this->PortalModel->getItems();
    $collections =  $this->PortalModel->getCollections();

    $this->view->render('home/index', $items);
}

So this is how I get the one $data object to the view and loop trough it. But how could I store multiple results from the database to the view?

tereško
  • 58,060
  • 25
  • 98
  • 150
Badaydush73
  • 43
  • 1
  • 6
  • Maybe `$this->view->render('home/index', ['items' => $items, 'anotherObject' => $anotherObject/*, etc*/]);` – Ermac Apr 05 '18 at 13:09
  • I get it, but how are theese accessible to the view. If I `var_dump($items)` I gat an undefined variable. – Badaydush73 Apr 05 '18 at 13:13
  • This has nothing to do with MVC and what you have there is **not** a view. Just read this: http://chadminick.com/articles/simple-php-template-engine.html – tereško Apr 05 '18 at 13:15
  • So you think I posted something about templating? What i postet is not a view, its a core class where i store my View functions. A view is made out of HTML and some or none php. – Badaydush73 Apr 05 '18 at 13:22
  • @Nikola Kail Cava Popović how did you accessed $items at first place? – Ermac Apr 05 '18 at 13:24
  • @NikolaKailCavaPopović maybe read this then: https://stackoverflow.com/questions/16594907/understanding-mvc-views-in-php/16596704#16596704 .. and, yes, what you wrote about is actually templating. You just do not seem to realize it. – tereško Apr 05 '18 at 13:27
  • @tereško Is it not so, that the View class takes care of the templating? – Badaydush73 Apr 05 '18 at 13:37
  • No, that is done by templating engines (either self-written or third-party ones, like Twig). And what you have there is a simplified templating engine. – tereško Apr 05 '18 at 14:12
  • @NikolaKailCavaPopović yes .. I know, what it says. So, basically, it's what's in your "controller" :D – tereško Apr 05 '18 at 14:31
  • @tereško I do not understand what are you trying to accomplish here. Are you trying to say that what I have written would never be used in an MVC pattern? – Badaydush73 Apr 05 '18 at 14:51
  • I am trying to tell you, that what you have here has nothing to do with MVC. What you call "view" is a template, your "controller" contains all of the actual logic and what you refer to as "models" are probably active-record style table abstractions. – tereško Apr 05 '18 at 15:06

1 Answers1

1

You should pass an array of variables into view instead of one variable.

public function indexAction(){
    $variables = [
        'items' => $this->PortalModel->getItems(),
        'collections' => $this->PortalModel->getCollections()
    ];

    $this->view->render('home/index', $variables);
}