#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Posts
    4
    Rep Power
    0

    Following MVC tutorial on Devshed by Alejandro Gervasio


    I've been working through the tutorial series on creating an MVC framework on Devshed by Alejandro Gervasio and everything works fine when all the files are in one directory.

    Sorry can't paste the url but it's under PHP tutorials and it's called Building a Simple MVC-based Framework in PHP 5

    What i want to do is keep a tidy directory structure and put all the files into their own relevant directories.

    I want to put all views in a 'views' folders, models in a 'models' folder, controllers in a 'controllers' folder and the rest in a 'core' folder.

    When I do this and try to view the page 'users', it tries to load the 'default_view.php' which doesn't exist. I am wondering if it's looking in the wrong directory but i can't see anywhere where i can change where it looks for the files.

    Has anyone else been able to do what i want or know how i can get it working with the new directory structure? I don't really want to keep it on one directory
  2. #2
  3. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,302
    Rep Power
    9400
    Building a Simple MVC-based Framework in PHP 5

    I'm really not keen on downloading files and perusing through them... What is the code that decides to use "default_view.php"?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Posts
    4
    Rep Power
    0
    This is the UsersController
    PHP Code:
    <?php

    class UsersController
    {
        private 
    $model NULL;
        
        
    // constructor
        
    public function __construct()
        {
            
    // store model object as property
            
    $this->model = new Model(Database::getInstance(array('localhost''root''28fulford''test')));
        }

        
    // fetch and display all users
        
    public function index()
        {
              
    // create view object
            
    $view = new View('users');
            
    // create view properties
            
    $view->title 'Using the MVC design pattern with PHP 5';
            
    $view->heading 'User List';
               
    $view->users $this->model->fetchAll();
            
    // display view
            
    echo $view->display();
        }

        
    // create new user
        
    public function create()
        {
            
    // create view object
            
    $view = new View('user_form');
            
    // create view properties
            
    $view->title 'Using the MVC design pattern with PHP 5';
            
    $view->heading 'Create new user';
            
    // display view
            
    echo $view->display();
        }
        
        
    // save user
        
    public function save()
        {
            
    // get POST params
            
    if (Input::post('send'))
            {
                
    $fname Input::post('fname');
                
    $lname Input::post('lname');
                
    $email Input::post('email');
                
    // save user data
                
    $this->model->save(array('fname' => $fname'lname' => $lname'email' => $email));
            }
        }

        
    // update existing user
        
    public function update($id)
        {
            
    $this->model->save(array('fname' => 'My first name''lname' => 'My last name''email' => 'myemail@domain.com'), (int)$id);
        }

        
    // delete existing user
        
    public function delete($id)
        {
           
    $this->model->delete((int)$id);
        }
    }
    // End UsersController class
    This is the View.php that calls the respective view (in this case 'users')
    PHP Code:
    <?php

    class View
    {
        private 
    $viewfile 'default_view.php';
        private 
    $properties = array();

        
    // factory method (chainable)
        
    public static function factory($viewfile '')
        {
            return new 
    self($viewfile);
        }

        
    // constructor
        
    public function __construct($viewfile '')
        {
            if (
    $viewfile !== '')
            {
                
    $viewfile $viewfile '.php';
                if (
    file_exists($viewfile))
                {
                   
    $this->viewfile $viewfile;
                }
            }
        }
        
        
    // set undeclared view property
        
    public function __set($property$value)
        {
            if (!isset(
    $this->$property))
            {
                
    $this->properties[$property] = $value;
            }
        }

        
    // get undeclared view property
        
    public function __get($property)
        {
            if (isset(
    $this->properties[$property]))
            {
                return 
    $this->properties[$property];
            }
        }

        
    // parse view properties and return output
        
    public function display()
        {
            
    extract($this->properties);
            
    ob_start();
            include(
    $this->viewfile);
            return 
    ob_get_clean();
        }
    }
    // End View class
    As i said before, it all works if on one directory but separating them out, it doesn't pass the parameter 'users' to the View class (i've echoed out what is being passed and it's empty)
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Posts
    4
    Rep Power
    0
    I've done another test just to make sure the UsersController is being called by replacing everything in the index() of the UsersController with a simple echo. When I called teh controller, it displayed the message i expected so the problem is getting the new View object to pass the parameter correctly
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2006
    Posts
    4
    Rep Power
    0
    I fixed the problem myself. It turned out it was a path issue.

    Changed the constructor to this by adding the path to the file i wanted to include

    PHP Code:
    // constructor
        
    public function __construct($viewfile '')
        {
            if (
    $viewfile !== '')
            {
                
    $this->path realpath(dirname(__FILE__)) . '/' $viewfile '.php';

                if (
    file_exists($this->path))
                {
                    
    $this->viewfile $viewfile '.php';
                }
                
            }
        } 

IMN logo majestic logo threadwatch logo seochat tools logo