SitePoint Sponsor

User Tag List

Page 1 of 3 123 LastLast
Results 1 to 25 of 68

Hybrid View

  1. #1
    SitePoint Guru rockit's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question My First crack at MVC

    well, i know it's been beaten to death all over the forums here but i'm giving it my first shot, i find every time i read a post they become confusing after a bit on how to properly structure everything. i understand the principles of M-V-C, but when it comes down writing an app myself i get overwhelmed, so hopefully someone can let me know if i'm going down the right path.

    the little project i'm doing is just a category manager for a backend storefront system. keep in mind, other sections will be added such as products (will run off the same idea as the category mangaer), reports, orders, customers.

    MODEL: will contain the DAO, queries to be run, any business logic to be run. Should i write seperate classes for add, edit, update, delete that extends the DAO? currently, i'm using a mysql data access class that was featured in PHP anthology 1. it handles the connection to the db, and i pass an sql statement to it for processing. but some examples i've seen, the class takes the connection to the database and has queries coded right in it. Should i just write a subclass for each one to handle the processing that contain the sql?

    class MySQL

    class addCategory extends MySQL ?

    i want my models to be portable so if i decide to switch to postgre for example, the switch is effortless.

    VIEW: will contain things like forms, messages, echo any output from the model.

    how should i structure my template in the view? maybe based on an xml file? or should something like that be based

    CONTROLLER: the controller will feed to the model the action required, (Add, Edit, Update, Delete). is this the front controller? I got a bit confused when the large MVC post started talking about interceptors and things.

    basically i understand the principles, but when it comes down to get down to coding, i get confused in how to structure my scripts, and the architecture. should i place my views in a sub-folder "templates" maybe? And place the model in an includes folder?

    i appreciate any help in the matter.

  2. #2
    SitePoint Guru rockit's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wow... thought i would have received at least one reply by now...

  3. #3
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    United Kingdom
    Posts
    208
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok I will try to answer a few things. First of all, a front controller is not part of the traditional MVC. A front controller is a single entry point to your appplication, which handles the request and serves a page (in this sense Apache can be considered a front controller). At it's most simple it could be a switch which gets a parameter from the query string, and either includes a file which the parameter points to, or returns a default page.
    PHP Code:
    // A basic front controller
    switch( $_GET['page]' )
    {
      case 
    'news':
        require_once( 
    'news.php' );
        break;
      case 
    'articles'
        
    require_once( 'articles.php' );
        break;
      default:
        require_once( 
    'home.php' );

    In MVC frameworks front controllers often locate an MVC controller and invoke it, and then display the view which it returns. It also provides an opportunity to do global operations, for example authentication. This is where the Intercepting Filter pattern fits in. You might set up a number of filters in the front controller, which are invoked and given the chance to alter the request in some way. Again intercepting filters are not really part of MVC.

    The model in MVC is where the business logic of your application should happen. For example a User class might have first name and last name properties and a method getFullname(). You could either put the methods which update the database in the model itself (Active Record), or in a Data Mapper (which contains the select/insert/update/delete methods) as a property of the Model. Either way the important part is that the implementation is hidden from the Controller and the View. I think separate classes for add/edit etc would be a little superfluous.

    You mention having your model classes extend your MySQL class. I would consider this a bad smell. The model shouldn't really care what the database type is, just that it has a way of writing to the database. As such passing the database object as a parameter to the model would be more useful. Then you could swap from MySQL to Postgres without the model knowing or caring.

    When I write Views they are generally just HTML pages with some interspersed PHP. The controller adds models to a container which the View has access to, and the View calls methods in the models to display data. This is a very basic approach, there are many others.

    For further reading I recommend PHP 5 Objects, Patterns, Pratice (Zandstra) as a good PHP based book, and of course Patterns of Enterprise Application Architecture (Fowler) if you get along with Java and C#.

    Hope this is of some help to you.

  4. #4
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As for the view, that could really be anything you want it to be. It could be PHP blocks embedded in the HTML, though I don't really like that method. You can use XML and XSL (XSLT), or, you can use templating systems such as Smarty, or you could make your own templating system if you so choose. It's really up to you. I would recommend XML/XSL, but it depends what you are doing and what you're willing to learn.
    Laudetur Iesus Christus!
    Christ's Little Flock
    Jesus is the Good Shepherd

  5. #5
    SitePoint Guru rockit's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    in your view, how do you write it. should i have a class for what would be considered the "outer template" (navigation), and then an "inner template" that would contain the view for the preticular inner workings. for example, a form to add fields to a database?

  6. #6
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Depends what method you use, whether you use XML/XSL, Smarty, or your own system.
    Laudetur Iesus Christus!
    Christ's Little Flock
    Jesus is the Good Shepherd

  7. #7
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    > that would contain the view for the preticular inner workings.

    Composite

  8. #8
    SitePoint Guru rockit's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh ok, i've heard of composite views before but never did anything with them before?

    i just read about them on the sun website (it seems like if you really want to learn MVC with good resources, read java articles then apply the principles to PHP).

    what would be a practical way to invoke composite views in my controller? build the outer workings of the template first, then based on whatever action the controller is invoking, render the inner template? this seems like potential for xml files possibly?

  9. #9
    SitePoint Guru rockit's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Composite View is the article i read.

  10. #10
    SitePoint Guru rockit's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i'm working on some shopping cart software now and reading some thoughts on Front Controllers and Interceptors, would this be the best way to go with session handling for the shopping cart?

  11. #11
    SitePoint Guru rockit's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, so i'm definitely making progress with structuring my app around MVC. I seem to have read some conflicting views (which seems to always happen in the PHP world), on how to pass data from the model to the view.

    What i'm looking for is the best way to pass mysql rows to the view. From what i've read, it's been stated as a bad practice if the view contains a while loop to output the mysqlrows, as you'd do in a procedural approach.

    i've found some code samples in PHP Anthology II which uses what the book calls as an N-Tier approach.

    This would be the controller.
    PHP Code:
    <?php
    // Data Access Layer Classes
    require_once('Database/MySQL.php');
    require_once(
    'ExampleApps/Articles.php');

    // Application Logic Class
    require_once('ExampleApps/Article.php');

    // Presentation Logic Classes
    require_once('ExampleApps/ArticlesView.php');
    require_once(
    'ExampleApps/ArticleView.php');

    $db = & new MySQL('localhost','harryf','secret','sitepoint');

    if ( isset(
    $_GET['id']) ) {
        
    $articles = & new Articles($db);
        
    $articles->getArticle($_GET['id']);
        
    $article $articles->fetch();
        
    $view = & new ArticleView($article);
    } else {
        
    $articles = & new Articles($db);
        
    $articles->getArticles();
        
    $view = & new ArticlesView($articles);
    }

    echo ( 
    $view->render() );
    ?>
    articles.php
    PHP Code:
    <?php
    /**
    * @package SPLIB
    * @version $Id: Articles.php,v 1.1 2003/12/12 08:06:06 kevin Exp $
    */
    /**
    * Include the Article class
    */
    require_once('ExampleApps/Article.php');
    /**
    * Articles class<br />
    * Fetches data for articles from MySQL
    * @access public
    * @package SPLIB
    */
    class Articles {
        
    /**
        * Instance of MySQL class
        * @access private
        * @var object
        */
        
    var $db;

        
    /**
        * Array of article data
        * @access private
        * @var array
        */
        
    var $articles;

        
    /**
        * Articles constructor
        * @param object instance of MySQL class
        * @access public
        */
        
    function Articles(& $db) {
            
    $this->db=& $db;
        }

        
    /**
        * Fetches a list of articles into the local array
        * @param int (optional) number of rows to fetch
        * @param int (optional) row to start from
        * @return boolean
        * @access public
        */
        
    function getArticles ($numRows=false,$startRow=false) {
            
    $sql="SELECT
                    article_id, title, intro, body, author, published, public
                  FROM
                    articles
                  ORDER BY
                    published
                  DESC"
    ;

            if ( 
    $numRows && $startRow ) {
                
    $sql.=
                 
    " LIMIT
                    
    $startRow$numRows";
            } else if ( 
    $numRows ) {
                
    $sql.=
                 
    " LIMIT
                    
    $numRows";
            }

            
    $result=$this->db->query($sql);

            if ( 
    $result->isError() ) {
                
    trigger_error('Articles::fetchArticles: '.
                              
    'Unable to fetch articles');
                return 
    false;
            }

            while ( 
    $row $result->fetch() ) {
                
    $this->articles[]=$row;
            }

            return 
    true;
        }

        
    /**
        * Fetches a single article into the local array
        * @param int article_id
        * @return boolean
        * @access public
        */
        
    function getArticle($articleID) {
            if ( !
    is_numeric($articleID) ){
                
    trigger_error(
                    
    'Articles::fetchArticle: Numeric value for'.
                    
    '$articleID required');
            }

            
    $articleID=addslashes($articleID);

            
    $sql="SELECT
                    article_id, title, intro, body, author, published, public
                  FROM
                    articles
                  WHERE
                    article_id = '"
    .$articleID."'";
            
    $result=$this->db->query($sql);

            if ( 
    $result->isError() ) {
                
    trigger_error('Articles::fetchArticle: '.
                              
    'Unable to fetch article');
                return 
    false;
            }

            
    $this->articles[]=$result->fetch();
            return 
    true;
        }

        
    /**
        * Returns the current article from the internal array
        * and moves the internal array point forward
        * @return object instance of Article
        * @access public
        */
        
    function fetch () {
            
    $article=each $this->articles );
            if ( 
    $article ) {
                return new 
    Article($article['value']);
            } else {
                
    reset $this->articles );
                return 
    false;
            }
        }
    }
    ?>
    article.php
    PHP Code:
    <?php
    /**
    * @package SPLIB
    * @version $Id: Article.php,v 1.1 2003/12/12 08:06:06 kevin Exp $
    */
    /**
    * Article class<br />
    * Stores data for a single article
    * @access public
    * @package SPLIB
    */
    class Article {
        
    /**
        * The article id
        * @access private
        * @var int
        */
        
    var $article_id;

        
    /**
        * The article title
        * @access private
        * @var string
        */
        
    var $title;

        
    /**
        * The article introduction
        * @access private
        * @var string
        */
        
    var $intro;

        
    /**
        * The article body
        * @access private
        * @var string
        */
        
    var $body;

        
    /**
        * The author
        * @access private
        * @var string
        */
        
    var $author;

        
    /**
        * Date published ( UNIX timestamp )
        * @access private
        * @var int
        */
        
    var $published;

        
    /**
        * Whether the article is "public"
        * @access private
        * @var int
        */
        
    var $public;

        
    /**
        * Article constructor
        * @param array data from database contain an Article
        * @access public
        */
        
    function Article($data) {
            
    $this->article_id = isset ( $data['article_id'] ) ?
                
    $data['article_id'] : FALSE;
            
    $this->title = isset ( $data['title'] ) ?
                
    $data['title'] : FALSE;
            
    $this->intro = isset ( $data['intro'] ) ?
                
    $data['intro'] : FALSE;
            
    $this->body = isset ( $data['body'] ) ?
                
    $data['body'] : FALSE;
            
    $this->author = isset ( $data['author'] ) ?
                
    $data['author'] : FALSE;
            
    $this->published = isset ( $data['published'] ) ?
                
    $data['published'] : FALSE;
            
    $this->public = isset ( $data['public'] ) ?
                
    $data['public'] : FALSE;
        }

        
    /**
        * Returns the article_id
        * @return int
        * @access public
        */
        
    function id () {
            return 
    $this->article_id;
        }

        
    /**
        * Returns the article title
        * @return string
        * @access public
        */
        
    function title() {
            return 
    $this->title;
        }

        
    /**
        * Returns the article intro
        * @return string
        * @access public
        */
        
    function intro() {
            return 
    $this->intro;
        }

        
    /**
        * Returns the article title
        * @return string
        * @access public
        */
        
    function body() {
            return 
    $this->body;
        }

        
    /**
        * Returns the author
        * @return string
        * @access public
        */
        
    function author () {
            return 
    $this->author;
        }

        
    /**
        * Returns the data published like "October 1st 2001"
        * @param string (optional) date format
        * @return string
        * @access public
        */
        
    function published ($format='F jS Y') {
            return 
    date($format,$this->published);
        }

        
    /**
        * Whether the article is "public" or not
        * @return boolean
        * @access public
        */
        
    function public () {
            return 
    $this->public == true false;
        }
    }
    ?>
    articlesview.php
    PHP Code:
    <?php
    /**
    * @package SPLIB
    * @version $Id: ArticlesView.php,v 1.1 2003/12/12 08:06:06 kevin Exp $
    */
    /**
    * ArticlesView<br />
    * Constructs an HTML table from a list of articles
    * @access public
    * @package SPLIB
    */
    class ArticlesView {
        
    /**
        * Instance of Articles
        * @access private
        * @var Articles
        */
        
    var $articles;

        
    /**
        * Rendered HTML placed here
        * @access private
        * @var string
        */
        
    var $html;

        
    /**
        * ArticlesView constructor
        * @param object instance of Articles
        * @access public
        */
        
    function ArticlesView(& $articles) {
            
    $this->articles=& $articles;
            
    $this->html="<table>\n";
            
    $this->build();
        }

        
    /**
        * Builds the body of the table
        * @return void
        * @access private
        */
        
    function build () {
            while ( 
    $article $this->articles->fetch() ) {
                
    $this->html.="  <tr>\n";
                
    $this->html.="    <td><a href=\"".
                    
    $_SERVER['PHP_SELF']."?id=".$article->id()."\">".
                    
    $article->title()."</a></td>\n";
                
    $this->html.="  </tr>\n";
            }
        }

        
    /**
        * Returns the constructed HTML
        * @return string
        * @access public
        */
        
    function render() {
            
    $this->html.="</table>\n";
            return 
    $this->html;
        }
    }
    ?>
    articleview.php
    PHP Code:
    <?php
    /**
    * @package SPLIB
    * @version $Id: ArticleView.php,v 1.1 2003/12/12 08:06:06 kevin Exp $
    */
    /**
    * ArticleView<br />
    * Renders article data as HTML
    * @access public
    * @package SPLIB
    */
    class ArticleView {
        
    /**
        * Instance of Article
        * @access private
        * @var Article
        */
        
    var $article;

        
    /**
        * Rendered HTML placed here
        * @access private
        * @var string
        */
        
    var $html;

        
    /**
        * ArticleView constructor
        * @param object instance of Article
        * @access public
        */
        
    function ArticleView(& $article) {
            
    $this->article=& $article;
            
    $this->html="<table>\n";
            
    $this->build();
        }

        
    /**
        * Builds the body of the table
        * @return void
        * @access private
        */
        
    function build () {
            
    $this->html.="  <tr>\n";
            
    $this->html.="    <td><b>".$this->article->title()."</b> by ".
                
    $this->article->author()." ".$this->article->published()."</td>";
            
    $this->html.="  </tr>\n";
            
    $this->html.="  <tr>\n";
            
    $this->html.="    <td>".$this->article->body()."</td>";
            
    $this->html.="  </tr>\n";
        }

        
    /**
        * Returns the constructed HTML
        * @return string
        * @access public
        */
        
    function render() {
            
    $this->html.="</table>\n";
            return 
    $this->html;
        }
    }
    ?>
    as you can see, it's coded in PHP 4, which is fine because that's all my host currently supports.

    as you can see in the articlesview.php (view), it relies on a while loop to loop through the results of the array of data.

    what concerns me here is having the same function render() in 2 seperate classes, which seems a bit unmaintainable besides this 1 situation. i've seen things like smarty among other things, and i'm just looking for the best approach towards having composite views.

    any insight in what to take from this example, get rid of in this example would definitely benefit me.

  12. #12
    PHP/Rails Developer Czaries's Avatar
    Join Date
    May 2004
    Location
    Central USA
    Posts
    806
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First thing, your code style and comments look very good, so keep up the hard work you have been doing so far.

    The first thing that strikes me when looking at this code is 'too specific'. I may be putting it together wrong, but the way it looks right now, you would be making yourself code 4-5 new php class files for each different piece of functionality you add to this framework, on top of modifying the controller class for the specific case. This would actually be making things much more difficult for you than a traditional coding style. The goal of a framework is to make things *easier* for you with each successive piece of functionality you add.

    You generally want to have only one main controller and view that you can re-use passing different values so you don't have to re-code the same functions over and over again.

    You may want to take a look at http://www.onlamp.com/pub/a/php/2005...ontroller.html - although it's not the best example of a well-designed framework, it does give a good example of a more general controller that will load different classes dynamically based on the request.

  13. #13
    SitePoint Guru rockit's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i'm wondering if my best bet is just to read Patterns of Enterprise Application Architecture. For the record, it's available at Safari Books Online, and a basic membership is $9.99 a month.

  14. #14
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rockit
    i'm wondering if my best bet is just to read Patterns of Enterprise Application Architecture. For the record, it's available at Safari Books Online, and a basic membership is $9.99 a month.
    A very good book indeed, but it's not a cookbook. It's also uses java for all examples, which may be a turnoff for some (Although java and PHP have quite similar syntaxes)

  15. #15
    SitePoint Guru Galo's Avatar
    Join Date
    May 2005
    Location
    Holland!
    Posts
    852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rockit
    i'm wondering if my best bet is just to read Patterns of Enterprise Application Architecture. For the record, it's available at Safari Books Online, and a basic membership is $9.99 a month.
    Head first, Patterns and Design i believe it's called, it's Java but man what a cool book, really, try it before reading EAA, it will help you understand the need for patterns and how they are submitted in concrete solutions as well as a lot of humor.

    http://www.oreilly.com/catalog/hfdesignpat/

    peace...
    Business as usual is off the menu folks, ...

  16. #16
    SitePoint Guru rockit's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ya, they are quite similar. i find that java references seem to be the only thing documented worth while reading regarding MVC. with all this java reading i've been doing lately, it's getting tempting to jump back into it.

    for once, it'd just be nice to read a good text that doesn't involve someone shoving their own framework down your throat, or 18 different people's view on this book.

  17. #17
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rockit
    i find that java references seem to be the only thing documented worth while reading regarding MVC.
    ??
    not sure where you're coming from with that. this forum alone has ridiculous amounts of MVC related content, with as many people's opinions (good and bad) on the subject as you could ever imagine. i myself spent weeks, months here reading old threads alone, post by post (some threads havig 100+ posts), and going off coding and see what i came up with. my best advice regarding getting into MVC properly is to just get into that DESIGN/TEST-CODE-REFACTOR cycle, read posts on a particular subject you have a problem with, take all the opinions/suggestions into consideration, then decide how you will tackle it. if you get it wrong, no matter; just refactor, and start again.

    Quote Originally Posted by rockit
    for once, it'd just be nice to read a good text that doesn't involve someone shoving their own framework down your throat, or 18 different people's view on this book.
    hmm, i kind of agree that it can be slightly annoying when people do that (classic: "TRY CAKE!!!" ) - but taking a positive from it, you should download a few frameworks, spend a good few days/weeks reading and UNDERSTANDING the code, why it has been written that way, etc, and drawing your own conclusions from it. then, go away and code (the cycle above)

    still - having said that - your comment comes across slightly brattish if i may say. you make it seem like you dont want opinions or help - which if is the case, why are you here? maybe ive read that wrong though.

  18. #18
    SitePoint Guru rockit's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry, wasn't trying to come off brattish if it may seem, frustration sitting in more often than not.

    what i meant with regards to the java reference, there is plenty of great published content by sun about MVC in the java platform.

    the problem i found i was having most with reading the threads was just trying to find a solid foundation of answers. when i really think about it, it makes sense because MVC is simply a principle, so principles are always going to have various view points.

    all things aside, i have a much better understanding of how MVC should be invoked in your apps now than i did before. my only confusion left now, is best pratice to iterate through resultsets. some help on that would be awesome.

  19. #19
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rockit
    the problem i found i was having most with reading the threads was just trying to find a solid foundation of answers. when i really think about it, it makes sense because MVC is simply a principle, so principles are always going to have various view points.

    all things aside, i have a much better understanding of how MVC should be invoked in your apps now than i did before. my only confusion left now, is best pratice to iterate through resultsets. some help on that would be awesome.
    youre spot on about MVC - thats the thing most people dont get; it is a principle, nothing more.

    but then thats the beauty of programming all together: there is no right or wrong, simply plenty of ways.

    regarding your last para - stuff like iterating through result sets is trivial, and you will find once you lay down a core design of your objects relationships, you will discover many natural ways to implement it, as well as other objects.

  20. #20
    SitePoint Guru rockit's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    exactly. after reading the chapter about MVC in Patterns of Enterprise Application Architecture, i see how most of what's done is simply just concepts.

    MVC is quite simply when it comes down to it. But there always plenty of different ways you can implement it, whether your view is based on a template view, a transform view or two step view etc.

    i think the biggest thing in the learning curve, is most articles regarding mvc over-complicate things for someone newer to OOP. really, MVC could be an implement practice in the early stages of OOP training.

    what i think MVC needs most for beginners, is a way of breaking down how to use it.

    Models: in models, you place...

    Views: views should consist of...

    Controllers: ...???

    get my point? for someone just trying to grasp the concept, it can begin to be overwhelming as many posts lead to conversation about intercepting filters, helper patterns, etc. without the core concepts being fully explained, leading to why many people get confused. i don't see why the topic needs to be so confusing for many to learn.

  21. #21
    SitePoint Guru rockit's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what's a good file system strucute?

    i was think along the lines of

    admin
    --lib
    --tpl
    --config

    a sample for the admin area of a web app i'm creating.

  22. #22
    SitePoint Guru rockit's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    had a question regarding input values from forms. i'm using a Dao from phppatterns.com. I have a class CategoryDAO

    PHP Code:
    class CategoryDao extends DAO {
         
         function 
    CategoryDao ( & $da ) {
            
    Dao::Dao($da);
        }

         function & 
    addCategory(do i pass Post Data or an Array???) {
            
    $sql="INSERT INTO category SET category_name='$data1', etc...";
            return 
    $this->update($sql);
        }


    how should i be assigning input data before passing it to my CategoryDao class?

    a sample would be
    <input type="text" name="category_name" />
    <input type="text" name="category_sort_order" />
    ...
    this is a basic input script that takes form data, and inserts a new category into the category table in a mysql table.

    secondly, if i was to do an edit of the data, for example changed the name. in a procedural script i would just assing the value option of the input tag to the value of a mysql result object. something like $rs->categoryname (sql statement ignored here)? what would be the best procedure in the MVC arena?

  23. #23
    SitePoint Guru rockit's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok... so understand that this primary role is for the controller to handle the processing, where it is the responsibility to pass data to the model. otherwise, this data would be going straight to a model without formatting.

  24. #24
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rockit
    (...)do i pass Post Data or an Array???(...)
    Quote Originally Posted by rockit
    ok... so understand that this primary role is for the controller to handle the processing, where it is the responsibility to pass data to the model. otherwise, this data would be going straight to a model without formatting.
    MVC says nothing about how your model layer is structured. You'll have to look otherwhere for that. You can pass post-data directly to a model component or you can do some validation/filtering in the controller first.
    There are pros and cons to doing both things.

  25. #25
    SitePoint Guru rockit's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what i'm working on now is a basic admin area of website that handles adding product categories into a db.

    i have it set up in such a way that a front controller handles the initial request and creates the appropriate page controller.

    PHP Code:
    $dao= new DataAccess ($localhost$user$pass$dbname); 

    switch (
    $_GET['page']) {
        case 
    "add":
            
    $controller = new AddCategoryController($dao$_GET);
        break;
        case 
    "edit":
            
    $controller = new EditCategoryController($dao$_GET);
        break;
        case 
    "delete":
            
    $controller = new DeleteCategoryController($dao$_GET);
        break;
        case 
    "view":
            
    $controller = new ViewCategoryController($dao$_GET); //handles a single record view
        
    default:
            
    $controller = new ListCategoryController($dao$_GET);  //handles a listing of all categories
        
    break;

    is this a good approach, or could there be a better way to suggest of doing things? also, should i have a seperate model for each one as well?

    here's an example of the AddCategoryController.

    in
    PHP Code:
    $this->model= new AddCategoryModel($CategoryDao); 
    should i have a seperate model for add, edit, delete, view? Should they extend a parent?

    PHP Code:
    class AddCategoryController {

        public function 
    __construct ($dao,$null) {
            
    $CategoryDao= new CategoryDao($dao); 
            
    $this->model= new AddCategoryModel($CategoryDao);
            
    $this->view= new AddCategoryView($this->model,$getvars);
        }
        
        public function 
    getView () {
            return 
    $this->view;
        }

    note i really haven't written the model entirely yet, as this has been blocking me a bit. i have my sql statement in a Dao class ie. CategoryDao.php which is basically a class with functions for each sql statement i would need.


Bookmarks

Posting Permissions

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