Wednesday, March 1, 2017

PHP MVC - Undefined variable

I'm learning PHP: separation of concerns, MVC, functions, arrays, the works. I'm lost right now, admittedly.

I am using an existing authentication framework:

http://ift.tt/1Oz41H8
// app/core/View.php 
/**
 * Class View
 * The part that handles all the output
 */
class View
{
    /**
     * simply includes (=shows) the view. this is done from the controller. In the controller, you usually say
     * $this->view->render('help/index'); to show (in this example) the view index.php in the folder help.
     * Usually the Class and the method are the same like the view, but sometimes you need to show different views.
     * @param string $filename Path of the to-be-rendered view, usually folder/file(.php)
     * @param array $data Data to be used in the view
     */
    public function render($filename, $data = null)
    {
        if ($data) {
            foreach ($data as $key => $value) {
                $this->{$key} = $value;
            }
        }

        require Config::get('PATH_VIEW') . '_templates/header.php';
        require Config::get('PATH_VIEW') . $filename . '.php';
        require Config::get('PATH_VIEW') . '_templates/footer.php';
    }

Above is a note to inform you about the View class.

In my IndexCotroller:

public function index()
    {
        $this->View->render('index/index', array(
            'images' => ImagesModel::getImages())
        );
    }

In my ImageModel:

class ImageModel
{
    /**
     *
     *
     */
    public static function getImages($xxx)
    {
        $xxx = "test test test";
    }
}

In index/index view I have:

<?php $this->images; ?>

I get an error such as:

Warning: Missing argument 1 for ImageModel::getImages(), called in IndexController.php on line 21 and defined in ImageModel.php on line 13

I simply want to echo a variable to test that I can get data from my model onto my index view. I am very new to PHP, certainly have bitten off more than I can chew but this challenging me and have to know.

Are my functions and arrays wacky? Flame me, but provide knowledge too?!




via Evan

Advertisement