A question about data sent to the view from the controllers
mdk1980 opened this issue · comments
In view.php (application/core/view.php) there is a foreach loop for data (an array) to be sent to the view when necessary. I can't find a reason for this code nor do I see where in view.php this information is used in any of its internal methods.
The code in question: class: view.php method: render( )
public function render($filename, $data = null)
{
if ($data) {
foreach ($data as $key => $value) {
$this->{$key} = $value;
}
}
This foreach loop information is not used apparently in the subsequent code in view.php that displays information to the user.
require Config::get('PATH_VIEW') . '_templates/header.php';
require Config::get('PATH_VIEW') . $filename . '.php';
require Config::get('PATH_VIEW') . '_templates/footer.php';
heres an example.
In the LoginController (application/controller/) you have a method: index() that feeds information to the view.php
in the index() method in LoginController.php (path=application/controller/LoginController.php)
public function index()
{
// if user is logged in redirect to main-page, if not show the login/index() method
if (LoginModel :: isUserLoggedIn()) {
Redirect::home();
} else {
$data = array('redirect' => Request::get('redirect') ? Request::get('redirect') : NULL); -> THIS LINE HERE
$this->View->render('login/index', $data);
}
}
The 'else' part containing $data sends an array to the render method of view.php that appears to do nothing. Im sure I'm missing something simple here. Any help would be appreciated.
This foreach only used if you need send information or data to the view
if you need send data to the view use:
$this->View->render('note/index', $data)
if you no need send info to the view use:
$this->View->render('note/index')
'note/index' is a file in view folder
$data is index array
$data = ["pizza" => true, "ingredients" = "cheese, tomato"];
foreach only run if $data != null
application/core/view.php
foreach ($data as $key => $value) {
$this->{$key} = $value;
}
for each add you data array in element $this of Class View
Ej:
in controller/NoteController.php create new function:
public function hello(){
$this->View->render('note/hello', array(
'title' => 'Hello men')
);
}
in folder view/note create new file "hello.php"
put in this file "Hello.php"
<p><?= $this->title; ?></p>
open you browser and go to http://localhost/note/hello
you see "Hello men"
in this array you send anything string number array or object, and you use in view
$this->anything