I have a problem, Ive been making my own MVC app but there seems to be a problen in passing variables between model and controller. Output from controller is a single variable containing some json format data and it looks simple.
Controller
<?php
class controllerLib
{
function __construct()
{
$this->view = new view();
}
public function getModel($model)
{
$modelName = $model."Model";
$this->model=new $modelName();
}
}
class controller extends controllerLib
{
function __construct()
{
parent::__construct();
}
public function addresses($arg = false)
{
echo'Addresses '.$arg.'<br />';
$this->view->render('addressesView');
$this->view->showAddresses = $this->model->getAddresses();
}
}
?>
View
<?php
class view
{
function __construct()
{
}
public function render($plik)
{
$render = new $plik();
}
}
class addressesView extends view
{
public $showAddresses;
function __construct()
{
parent::__construct();
require 'view/head.php';
$result = $this->showAddresses;
require 'view/foot.php';
}
}
?>
Now problem is that $this->showAddresses does not pass to view and im stuck.
The code have various issues:
render() save the new View in a local var so that don't exists after the function end
You can't expect $this->showAddresses
to have a value at the constructor time.
You should implement the render() method as a method outside of the View constructor.
function __construct()
{
parent::__construct();
require 'view/head.php';
$result = $this->showAddresses; // (NULL) The object is not created yet
require 'view/foot.php';
}
View class:
public function factory($plik) // Old render($splik) method
{
return new $plik();
}
addressesView class:
function __construct()
{
parent::__construct();
}
function render()
{
require 'view/head.php';
$result = $this->showAddresses; // Object is created and the field has a value
require 'view/foot.php';
}
Controller class:
$view = $this->view->factory('addressesView');
$view->showAddresses = $this->model->getAddresses();
$view->render();