Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jul 2009
    Posts
    68
    Thanks
    1
    Thanked 1 Time in 1 Post

    PDO MVC Framework

    I'm trying to build a simple mvc framework to understand. I've heard advice about rolling you own framework but I'd rather learn by
    playing with some basic examples since diving into using pre existing frameworks without understanding everything seems daunting to me.

    So, I have a controller, model and view. In my database file I'm able to retrieve data using $result[1]->title as an example, but I'd like to have
    that information passed to the view (which is basically just my template file).

    I'm not really sure how to do this, I've tried a couple of methods and googled, but I've got conflicting answers.

    I'd appreciate it if anyone could point me in the right direction and maybe give advice for best practices for mvc structure since I got a lot of conflicting
    answers on that.

    I've attached some of the code I'm using below.

    Controller.php
    PHP Code:
    <?php
    class Controller {
        private 
    $model;
        private 
    $load;
        private 
    $name;
        
        public function 
    __construct($model$load) {
            
    $this->model $model;
            
    $this->load $load;
        }
        
        public function 
    index() {
            
    $name 'view';
            
    $this->load->view($name);
            
    $this->model->index();
        }
    }

    Database.php
    PHP Code:
    <?php

    class Database {

        function 
    getData() {
            
    $username 'username';
            
    $password '****';
            try {
                
    $dbh = new PDO('mysql:host=localhost;dbname=db_name'$username$password);
            } catch (
    PDOException $e) {
                echo 
    $e->getMessage();
            }

            
    //PDO Class
            
    $dbh->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_WARNING);
            
    $dbh->setAttribute(PDO::ATTR_CASEPDO::CASE_LOWER);

            
    $sth $dbh->query("SELECT * FROM posts");
            
    $result $sth->fetchAll(PDO::FETCH_OBJ);
            
    //print_r($result);
            //echo $result[1]->title;

        
    }
    }
    Model.php
    PHP Code:
    <?php
    class Model {

        public function 
    __construct(){
            
    $this->string "MVC + PHP = Awesome!"
        }
        
        public function 
    index() {

        }

    }

  • #2
    New Coder
    Join Date
    Sep 2011
    Posts
    63
    Thanks
    0
    Thanked 7 Times in 7 Posts
    I too built my own MVC and I also ran into the same problem.

    The way I solved it was using the extract function. For example:

    PHP Code:
    extract$this->_tagsEXTR_PREFIX_SAME"wddx" ); 
    The way I work is every piece of data I want to use in a view, I add to the tags array, then just before the view is rendered the above snippit will run.

    Also it might be worth having a main controller that all your other controllers extend. The main controller would take care of setting scripts, styles and data for the view.

    If you need any inspiration I recommend taking a look at larvel. In my opinion it is one of the best frameworks around.

    I'd be happy to answer any other questions your might have

    Edit: Just had a closer look at your code, it doesn't seem like to you are getting the data from the model. It looks like to are just setting it to a property in the model but not using it in the controller, so your view would not have access to it.
    Last edited by davidjones1990; 07-07-2013 at 10:42 PM. Reason: Typeo

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    note on the Database class: you should remove the try...catch. currently, if you get no connection, the next call(s) to pdo will error out.

    rule of thumb for Exceptions: catch an Exception where you can handle the problem (not where the problem occurs).
    for a basic exception like a failed connection, that is rather towards the top level of your script.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •