Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A question about data sent to the view from the controllers #891

Open
mdk1980 opened this issue Oct 6, 2020 · 1 comment
Open

A question about data sent to the view from the controllers #891

mdk1980 opened this issue Oct 6, 2020 · 1 comment

Comments

@mdk1980
Copy link

mdk1980 commented Oct 6, 2020

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.

@cristopher
Copy link
Contributor

cristopher commented Oct 6, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants