0

Ok, so I finally was able to convert my framework to the PSR-0 standards, but now I am getting errors in the sample application that I have not been getting before. I am not sure why, so I need a fresh pair of eyes. Any help is greatly appreciated. The two errors are:

Notice: Undefined property: tinyPHP\Classes\Controllers\User::$model in /Applications/MAMP/htdocs/tPHP/tinyPHP/Classes/Controllers/user.php on line 41

Fatal error: Call to a member function userList() on a non-object in /Applications/MAMP/htdocs/tPHP/tinyPHP/Classes/Controllers/user.php on line 41

So here are the three files that deal with user.

User Controller:

 namespace tinyPHP\Classes\Controllers;

 class User extends \tinyPHP\Classes\Core\Controller {

    //public $model = null;

    public function __construct() {
        parent::__construct();
        \tinyPHP\Classes\Core\Session::init();
        $logged = \tinyPHP\Classes\Core\Session::get('loggedIn');
        $role = \tinyPHP\Classes\Core\Session::get('role');

        if ($logged == false || $role != 'owner') {
            \tinyPHP\Classes\Core\Session::destroy();
            header('location: ../login');
            exit;
        }

    }

    public function index() {
        $this->view->userList = $this->model->userList();
        $this->view->render('user/index');
    }

    public function create() {
        $data = array();
        $data['login'] = $_POST['login'];
        $data['password'] = $_POST['password'];
        $data['role'] = $_POST['role'];

        $this->model->create($data);
        header('location: ' . BASE_URL . 'user');
    }

    public function edit($id) {
        $this->view->user = $this->model->userSingleList($id);
        $this->view->render('user/edit');
    }

    public function editSave($id) {
        $data = array();
        $data['id'] = $id;
        $data['login'] = $_POST['login'];
        $data['password'] = $_POST['password'];
        $data['role'] = $_POST['role'];

        $this->model->editSave($data);
        header('location: ' . BASE_URL . 'user');
    }

    public function delete($id) {
        $this->model->delete($id);
        header('location: ' . BASE_URL . 'user');
    }
 }

User Model:

namespace tinyPHP\Classes\Models;

class UserModel {

    private $db;

    public function __construct() {
        $this->db = new \tinyPHP\Classes\Core\MySQLiDriver();
        $this->db->conn();
    }

    public function userList() {
        $q = $this->db->select(TP . 'user', 'id,login,role', null, null);
        while($r = $q->fetch_assoc()) {
            $array[] = $r;
        }
        return $array;
    }

    public function userSingleList($id) {       
        $q = $this->db->select(TP . "user", "id,login,role", "id = '$id'", null);
        while($r = $q->fetch_assoc()) {
            $array[] = $r;
        }
        return $array;
    }

    public function create($data) {
        $tp_hasher = new PasswordHash(8, FALSE);

        $this->db->insert('user', array(
            $this->db->escape( $data['login'] ),
            $tp_hasher->HashPassword( $this->db->escape( $data['password'] ) ),
            $this->db->escape( $data['role'] )
            ),
            'login,
            password,
            role');
    }

    public function editSave($data) {
        $tp_hasher = new PasswordHash(8, FALSE);
        $pass = $this->db->escape($data['password']);

        $login = $this->db->escape( $data['login'] );
        $password = $tp_hasher->HashPassword( $pass );
        $role = $this->db->escape( $data['role'] );
        $id = $data['id'];

        $this->db->query( "UPDATE " . TP . "user SET login='$login',password='$password',role='$role' WHERE id = '$id' ");
    }

    public function delete($id) {
        $q = $this->db->query("SELECT role FROM " . TP . "user WHERE id = '$id'");
        $r = $q->fetch_array();

        if ($r['role'] == 'owner')
        return false;

        $this->db->delete('user', "id = '$id'");
    }

    public function __destruct() {
        $this->db->disconnect();
    }

}

User View:

<h1><?php echo _t( 'User' ); ?></h1>

<form method="post" action="<?php echo BASE_URL;?>user/create">
    <label><?php echo _t( 'Login' ); ?></label><input type="text" name="login" /><br />
    <label><?php echo _t( 'Password' ); ?></label><input type="text" name="password" /><br />
    <label><?php echo _t( 'Role' ); ?></label>
        <select name="role">
            <option value="default"><?php echo _t( 'Default' ); ?></option>
            <option value="admin"><?php echo _t( 'Admin' ); ?></option>
        </select><br />
    <label>&nbsp;</label><input type="submit" />
</form>

<hr />

<table>
<?php
    foreach($this->userList as $key => $value) {
        echo '<tr>';
        echo '<td>' . $value['id'] . '</td>';
        echo '<td>' . $value['login'] . '</td>';
        echo '<td>' . $value['role'] . '</td>';
        echo '<td>
                <a href="'.BASE_URL.'user/edit/'.$value['id'].'">' . _t( 'Edit' ) . '</a> 
                <a href="'.BASE_URL.'user/delete/'.$value['id'].'">' . _t( 'Delete' ) . '</a></td>';
        echo '</tr>';
    }
?>
</table>
1
Contributor
1
Reply
2
Views
5 Years
Discussion Span
Last Post by joshmac
0

After further investigation, I think I found out where the issue lies. It is between my bootstrap and my controller files. The bookstrap has a namespace to controller and the controller has a namespace to the model(s) but the controller namespace is being appended to the model(s) namespace (due to loadModel($name)) causing it to look for the model in the controller directory. Below are the files. Can any see how I can maybe fix this issue. Thanks.

Bootstrap.php

class Bootstrap {

    public function __construct() {

        $url = isset($_GET['url']) ? $_GET['url'] : null;
        $url = rtrim($url, '/');
        $url = filter_var($url, FILTER_SANITIZE_URL);
        $url = explode('/', $url);

        if (empty($url[0])) {
            $controller = new \tinyPHP\Classes\Controllers\Index();
            $controller->index();
            return false;
        }

        $file = SYS_PATH . 'Classes' . DS . 'Controllers' . DS . $url[0] . '.php';
        if (file_exists($file)) {
            require $file;
        } else {
            $this->error();
        }

        $url[0] = "\\tinyPHP\\Classes\\Controllers\\".$url[0];
        $controller = new $url[0];
        $controller->loadModel($url[0]);

        // calling methods
        if (isset($url[2])) {
            if (method_exists($controller, $url[1])) {
                $controller->{$url[1]}($url[2]);
            } else {
                $this->error();
            }
        } else {
            if (isset($url[1])) {
                if (method_exists($controller, $url[1])) {
                    $controller->{$url[1]}();
                } else {
                    $this->error();
                }
            } else {
                $controller->index();
            }
        }


    }

    public function error() {
        $controller = new \tinyPHP\Classes\Controllers\Error();
        $controller->index();
        return false;
    }

}

Controller.php

class Controller {

    public function __construct() {
        $this->view = new \tinyPHP\Classes\Core\View();
    }

    public function loadModel($name) {

            $modelName = $name . 'Model';
            $modelName = "\\tinyPHP\\Classes\\Models\\".$modelName;
            $this->model = new $modelName();    
    }

}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.