SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 38
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Large(r) Scale PHP Code Structure

    I am looking for advice on getting started with a large PHP application. This application is going to have many users with different levels of access and each user has many functions. Nothing out of the ordinary. The "advice" that I am looking for is how other PHP developer's setup their code design. Currently I have one index page that contains the html header and footer and a giant case statement that directs the user to the correct page. After this I have a function page that contains all of the global functions and local functions go in each of the main pages.

    Any advice / articles would be helpful.

  2. #2
    SitePoint Guru
    Join Date
    May 2005
    Location
    Finland
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Check the FrontController and ApplicationController threads.

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ezku
    Check the FrontController and ApplicationController threads.

    holy moley! thanks for the reading material...looks like I'll be busy for the next week trying to catch up.

  4. #4
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've been reading that thread on and off since it started, and I can asure you that you'll proberly need more than a week to get through it all

    Currently I have one index page that contains the html header and footer and a giant case statement that directs the user to the correct page.
    Look at the Composite pattern, if you are planning large scale as this is just about the only approach worth considering. Look back on my postings as I've done extensive work with this pattern (in conjunction with the Visitor pattern) and those 2 patterns working together have never failed me, not once.

    And I've thrown pretty much everything at them, some heavy duty s**t To remove the SWITCH look at the Front Controller as suggested, and a method to map your requests wouldn't hurt you either

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is what I came up with for my front controller. I would like some constructive criticism and possible issues that I might run into down the road...please note: I haven't touched OOP for 4-5 yrs so be nice.

    PHP Code:
    <?
    $page 
    = new FrontController;

    $page->include_file("functions.inc.php");
    $page->session_db_managment();
    $page->form_submit();
    $page->css_file("css/default.css");
    $page->page_controller();
    $page->html_headers();
    $page->html_close();
    $page->display();

    // ---- BEGIN CLASS ----------------------------------------------------------------------------------->

    class FrontController {
        public 
    $timestamp;
        public 
    $html_title "Da Title";
        public 
    $css_code;
        public 
    $html_header;
        public 
    $html_code;
        public 
    $html_footer;
        public function 
    __construct() {
            
    $this->timestamp date("D M j G:i:s T Y");
        }
        public function 
    include_file($file) {
            if (
    file_exists($file))
                include(
    $file);    
            else {
                
    error_log("$this->timestamp Error($file not found)\n",3"logs/error_logs.txt");
            }
        }
        public function 
    session_db_managment() {
            
    session_start();
            
    // connect up to our database
            
    $this->db mssql_connect("192.168.1.8""*****""*****");
            
    $link mssql_select_db("alpha",$this->db);
            
    // error reporting
            
    if (!$db)    { error_log("$this->timestamp Error(Cannot connect to server)\n",    3"logs/error_logs.txt"); }
            if (!
    $link)    { error_log("$this->timestamp Error(Cannot connect to database)\n",    3"logs/error_logs.txt"); }
            
    // check for login, send to login.php if not
            
    if ((empty($_SESSION['login_id']))||($_SESSION['page_code'] <> "us-reports")) {            
                
    $this->include_file("login.php");
            exit;
            }
        }
        public function 
    form_submit($form_submit) {
            if (isset(
    $form_submit))
                
    $this->include_file('form_submit.php');    
        }
        public function 
    css_inline($code) {
            
    $this->css_inline_code .= $code;
        }
        public function 
    css_file($css_file) {
            
    $this->css_file_code .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"$css_file\">\n";
        }
        public function 
    addJ($code) {
            
    $this->javascipt_code .= $code;
        }
        public function 
    add($code) {
            
    $this->html_code .= $code;
        }  
        public function 
    page_controller () {
            
    $this->include_file("page_controller.php");        
        }
        public function 
    query($sql) {
            return  
    mssql_query($sql,$this->db);
        }
        public function 
    fetch_array($result) {
            return 
    mssql_fetch_array($result);    
        }
        public function 
    num_rows($result) {
            return 
    mssql_num_rows($result);    
        }
        public function 
    html_headers() {
            
    $this->html_header  "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n";
            
    $this->html_header .= "<html>\n";
            
    $this->html_header .= "<head>\n";
            
    $this->html_header .= "<META HTTP-EQUIV=\"PRAGMA\" CONTENT=\"NO-CACHE\">\n";
            
    $this->html_header .= "<META http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n";
            
    $this->html_header .= "<title>$this->html_title</title>\n";
            
    // inline javascript code
            
    if ($this->javascipt_code) {
                
    $this->html_header .= "<SCRIPT type=\"text/javascript\" language=\"javascript\">\n";
                
    $this->html_header .= "<!--\n";
                
    $this->html_header .= $this->javascipt_code;
                
    $this->html_header .= "//-->\n";
                
    $this->html_header .= "</SCRIPT>\n";
            }
            
    // include css files
            
    if ($this->css_file_code)
                
    $this->html_header .= $this->css_file_code;
            
    // css inline code
            
    if ($this->css_inline_code) {
                
    $this->html_header .= "<style type=\"text/css\">\n";
                
    $this->html_header .= $this->css_inline_code;
                
    $this->html_header .= "</style>\n";
            }
            
    $this->html_header .= "</head>\n";
            
    $this->html_header .= "<body>\n";
        }
        public function 
    html_close () {
            
    $this->html_footer .= "</body>\n";
            
    $this->html_footer .= "</html>\n";    
        }
        public function 
    display() {
            echo 
    $this->html_header;
            echo 
    $this->html_code;
            echo 
    $this->html_footer;
        }
    }
    ?>
    Last edited by tdawg; Sep 1, 2005 at 14:40.

  6. #6
    SitePoint Guru
    Join Date
    May 2005
    Location
    Finland
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Doesn't seem like you've grasped the idea of OOP, let alone MVC. Your "FrontController" there is a collection of functions and nothing else. Undoubtedly someone knowledgeable here can recommend a good book for starters. (PHP Power Programming has a section on the subject and is freely available.)

  7. #7
    SitePoint Member dandt1975's Avatar
    Join Date
    Sep 2005
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thinking

    ?
    Web Designer,
    dandt1975

  8. #8
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?
    $page 
    = new FrontController;
    ...
    ...
    ...
    That is very scary A quote from Martin Fowlers PoEAA (Patterns of Enterprise Application Architecture, Google for it) says this of a Front Controller pattern,

    Page 344;

    The Front Controller consolidates all request handling by channeling requests through a single handler object. This object can carry out common behaviour, which can be modified at runtime with decorators. The handler then dispatches to command objects for behaviour particular to a request.
    That is a more complete version of a Front Controller, if you were to implement the Command pattern as suggested but you could do without it if you were after something simple, to get you started.

    I would recommend you buy, borrow, beg or steal a copy of this book as soon as. In the meantime you can go to www.martinfowler.com to do some digging. PHP5 Power Programming would also help you in some form or other but for patterns, it wouldn't be my first choice.

    PoEAA is the first choice, no buts about it. As to your class, well there is just too many things that are wrong with it to be honest... The list would be the length of my -SNIP-

  9. #9
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ezku
    Doesn't seem like you've grasped the idea of OOP, let alone MVC. Your "FrontController" there is a collection of functions and nothing else.
    I think that's a little harsh - it isn't MVC, and it isn't a FrontController, and (most of all) he hasn't set out well defined responsibilities, BUT, the css_inline() and add() methods do take advantage of OOP, and could be the start of a Template class.

    If you refocused the class as a Template class, which is responsible for bringing together the data needed to render a page of HTML, we would get this:

    PHP Code:
    class FrontController 
        public 
    $html_title "Da Title"
        public 
    $css_code
        public 
    $html_header
        public 
    $html_code
        public 
    $html_footer

        public function 
    css_inline($code) { 
            
    $this->css_inline_code .= $code
        } 
        public function 
    css_file($css_file) { 
            
    $this->css_file_code .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"$css_file\">\n"
        } 
        public function 
    addJ($code) { 
            
    $this->javascipt_code .= $code
        } 
        public function 
    add($code) { 
            
    $this->html_code .= $code
        }   
        public function 
    html_headers() { 
            
    $this->html_header  "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n"
            
    $this->html_header .= "<html>\n"
            
    $this->html_header .= "<head>\n"
            
    $this->html_header .= "<META HTTP-EQUIV=\"PRAGMA\" CONTENT=\"NO-CACHE\">\n"
            
    $this->html_header .= "<META http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n"
            
    $this->html_header .= "<title>$this->html_title</title>\n"
            
    // inline javascript code 
            
    if ($this->javascipt_code) { 
                
    $this->html_header .= "<SCRIPT type=\"text/javascript\" language=\"javascript\">\n"
                
    $this->html_header .= "<!--\n"
                
    $this->html_header .= $this->javascipt_code
                
    $this->html_header .= "//-->\n"
                
    $this->html_header .= "</SCRIPT>\n"
            } 
            
    // include css files 
            
    if ($this->css_file_code
                
    $this->html_header .= $this->css_file_code
            
    // css inline code 
            
    if ($this->css_inline_code) { 
                
    $this->html_header .= "<style type=\"text/css\">\n"
                
    $this->html_header .= $this->css_inline_code
                
    $this->html_header .= "</style>\n"
            } 
            
    $this->html_header .= "</head>\n"
            
    $this->html_header .= "<body>\n"
        } 
        public function 
    html_close () { 
            
    $this->html_footer .= "</body>\n"
            
    $this->html_footer .= "</html>\n";     
        } 
        public function 
    display() { 
            echo 
    $this->html_header
            echo 
    $this->html_code
            echo 
    $this->html_footer
        } 

    The next refactoring I'd do, would be to move that big block of HTML into an external file:

    PHP Code:
    class Template 
        public 
    $title "Da Title"
        public 
    $javascript;
        public 
    $headers
        public 
    $css;
        public 
    $body;  

        public function 
    css_inline($code) { 
            
    $this->css_inline_code .= $code
        } 
        public function 
    css_file($css_file) { 
            
    $this->css_file_code .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"$css_file\">\n"
        } 
        public function 
    addJ($code) { 
            
    $this->javascipt_code .= $code
        } 
        public function 
    add($code) { 
            
    $this->html_code .= $code
        }
        public function 
    render() { 
            include(
    'html_template.php');
        }

    html_template.php:

    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN\" "http://www.w3.org/TR/html4/loose.dtd">

    <html> 

    <head>
        
        <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> 
        
        <title><?php echo htmlspecialchars($this->title?></title> 
        
        <?php if ($this->javascipt): ?>
            <script type="text/javascript" language="javascript">
            <?php echo $this->javascipt?>
            </script> 
        <?php endif; ?>
        
        <?php echo $this->headers ?>
        
        <?php if ($this->css): ?>
            <style type="text/css"> 
            <?php echo $this->css?>
            </style> 
        <?php endif; ?>
        
    </head> 

    <body> 

        <?php echo $this->body ?>
        
    </body>

    </html>
    Next I'd sort out the naming more consistent, and remove the unnecessary publics:

    PHP Code:
    class Template 
        protected 
    $title "Page Title"
        protected 
    $javascript;
        protected 
    $headers
        protected 
    $css;
        protected 
    $body;  
        
        function 
    set_title($title) {
            
    $this->title $title;
        }
        function 
    append_css($css) { 
            
    $this->css .= $css
        } 
        function 
    append_css_link($href) { 
            
    $this->headers .= '<link rel="stylesheet" type="text/css" href="'.$href.'">'."\n"
        } 
        function 
    append_javascript($javascript) { 
            
    $this->javascript .= $javascript
        } 
        function 
    append_body_html($html) { 
            
    $this->body .= $html
        }
        function 
    render() { 
            include(
    'html_template.php');
        }

    And finally I'd space the code out a little more so that it is easier to read:

    PHP Code:
    class Template 
        protected 
    $title "Page Title"
        protected 
    $javascript;
        protected 
    $headers
        protected 
    $css;
        protected 
    $body;  
        
        function 
    set_title $title )
        {
            
    $this->title $title;
        }
        
        function 
    append_css $css )
        { 
            
    $this->css .= $css
        } 
        
        function 
    append_css_link $href )
        {
            
    $this->headers .= '<link rel="stylesheet" type="text/css" href="'.$href.'">'."\n"
        }
         
        function 
    append_javascript $javascript )
        {
            
    $this->javascript .= $javascript
        }
        
        function 
    append_body_html $html )
        {
            
    $this->body .= $html
        }
        
        function 
    render ( )
        {
            include(
    'html_template.php');
        }

    That gives us a nice class we could use, it isn't a Front Controller though. In MVC, this would be part of the V.

    Douglas
    Hello World

  10. #10
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just a quick breakdown of which of the methods in the original class seem to belong to which part of MVC:

    Controller-like stuff: include_file, page_controller

    Model-like stuff: session_db_managment, query, fetch_array, num_rows

    View-like stuff: css_inline, css_file, addJ, add, html_headers, html_close, display

    You can see that most of the methods are view-like, that's why I pulled out the View-like class which was hidden in your original "FrontController". What is left could then be broken down into classes which would belong in the model layer and the controller layer.

    hth,
    Douglas
    Hello World

  11. #11
    SitePoint Guru
    Join Date
    May 2005
    Location
    Finland
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DougBTX
    I think that's a little harsh - it isn't MVC, and it isn't a FrontController, and (most of all) he hasn't set out well defined responsibilities, BUT, the css_inline() and add() methods do take advantage of OOP, and could be the start of a Template class.
    Harsh? Could be. But I was speaking out of personal experience - the code above bears certain similarities to my own productions of some time ago, and I daresay I'd know when I say I knew close to nothing of OOP and understood less.

    Alright, I didn't inspect the code thoroughly, I'll give you that.

  12. #12
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yea, it doesn't "look" like OO code

    Douglas
    Hello World

  13. #13
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just a quick breakdown of which of the methods in the original class seem to belong to which part of MVC:

    Controller-like stuff: include_file, page_controller

    Model-like stuff: session_db_managment, query, fetch_array, num_rows

    View-like stuff: css_inline, css_file, addJ, add, html_headers, html_close, display

    You can see that most of the methods are view-like, that's why I pulled out the View-like class which was hidden in your original "FrontController". What is left could then be broken down into classes which would belong in the model layer and the controller layer.
    Ok, so what I have learned so far is that instead of creating a FrontController I was creating a template in which a FrontController could be an element (or class) of that template. I was also combining methods that should be broke into another class because they didn't fit the realm of that class.

    I can already see many benifits to the way Doug broke down the code and created the html_template.php file. I'm going to take another swing at this and post back in awhile.

  14. #14
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perhaps these MVC articles I wrote would help?
    http://www.phparch.com/issue.php?mid=9 (free trial issue - registration required)
    and the follow up article:
    http://phparch.com/issuedata/2003/jun/sample.php
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  15. #15
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sweatje
    Perhaps these MVC articles I wrote would help?
    http://www.phparch.com/issue.php?mid=9 (free trial issue - registration required)
    and the follow up article:
    http://phparch.com/issuedata/2003/jun/sample.php
    I think he meant larger as in doing more things, rather than using more code to do the same things

    Douglas
    Hello World

  16. #16
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DougBTX
    I think he meant larger as in doing more things, rather than using more code to do the same things

    Douglas
    Ha Ha. I thought he might appreciate a more in depth explanation than "like stuff"

  17. #17
    SitePoint Addict
    Join Date
    May 2005
    Posts
    255
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In addition to the other things mentioned here, one thing to consider is that you should not build your templating purely around html output. One day you *will* want to distribute your data in xml, pdf, or any other number of formats. XML, Plain text, and email (which typically is plain text or html, but generally has some "special case" rules due to limitations in email clients) are among the most common that you'll encounter. Now, there's nothing wrong with an HTMLTemplate that extends a generic Template class (or whatever), but you'll be hurting later if you don't plan for those sorts of things.

  18. #18
    SitePoint Zealot
    Join Date
    Feb 2005
    Location
    UK
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just a quick aside - does anyone have an answer for those of us not yet using php 5 and OOP? Or am I mis-reading those threads and this can be done in php 4?

  19. #19
    SitePoint Wizard dreamscape's Avatar
    Join Date
    Aug 2005
    Posts
    1,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Etnu
    In addition to the other things mentioned here, one thing to consider is that you should not build your templating purely around html output. One day you *will* want to distribute your data in xml, pdf, or any other number of formats. XML, Plain text, and email (which typically is plain text or html, but generally has some "special case" rules due to limitations in email clients) are among the most common that you'll encounter. Now, there's nothing wrong with an HTMLTemplate that extends a generic Template class (or whatever), but you'll be hurting later if you don't plan for those sorts of things.
    Why, why why would a template engine care what format the template file fed to it is in? As long as the file follows the engine's syntax (for variables, conditions, loops, etc), it has no need to care or even think about what else is in the file. Its job is to to take whatever file it is given by the application, parse it, and give the application back the compiled template (or possibly include the compiled template based on what the application currently wants to do). There is no need to have multiple template engines for different output formats, generally speaking. (something like PDF templating might be tricky, and maybe you'd need something a little different, but definitely you do not need a different engine for formats like HTML, XML, plain text, etc).

  20. #20
    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 Roger Ramjet
    Just a quick aside - does anyone have an answer for those of us not yet using php 5 and OOP? Or am I mis-reading those threads and this can be done in php 4?
    Just replace the protected keywords in the code with var and it'll work fine on php4.

    (They probably ought to be declared private rather than protected btw.)

  21. #21
    SitePoint Addict
    Join Date
    May 2003
    Location
    The Netherlands
    Posts
    391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dreamscape
    Why, why why would a template engine care what format the template file fed to it is in? As long as the file follows the engine's syntax (for variables, conditions, loops, etc), it has no need to care or even think about what else is in the file. Its job is to to take whatever file it is given by the application, parse it, and give the application back the compiled template (or possibly include the compiled template based on what the application currently wants to do). There is no need to have multiple template engines for different output formats, generally speaking. (something like PDF templating might be tricky, and maybe you'd need something a little different, but definitely you do not need a different engine for formats like HTML, XML, plain text, etc).
    I don't think he's referring to using different engines, but to using different views (IIRC, HTMLTemplate would be simply a decorator). IMO, the confusion comes from the fact that the example is not as refactored as your own app might be, hence including views into the controller.
    There’s more than one way to skin a cat.

  22. #22
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    class Request {} // encapsulates $_GET & $_POST
    try {
    $fc = new FrontController( new Request() );
    $fc -> invoke();
    } catch( 
    Exception $e ) {
    // ...

    Don't ask for a PHP4 version as I don't do PHP4.x.x., This is just a skeleton for an example

    PHP Code:
    // no request mapper since this is a basic example
    interface IController {
    public function 
    __constructRequest $request );
    public function 
    invoke();
    }

    class 
    FrontController implements IController {
    private 
    $request;
    public function 
    __constructRequest $request ) {
    $this -> request $request;
    }
    public function 
    invoke() {
    $action $this -> select$this -> request -> getParameter'act' ) );
    $view $action -> execute$this -> request );
    // in this example the view is a template but could just
    // as well be an object
    echo( $view );
    }
    private function 
    select$action ) {
    switch( 
    $action ) {
    case 
    'view':
    require_once( 
    'view-action.class.php' );
    return new 
    ViewAction();
    break;
    case 
    'create':
    require_once( 
    'create-action.class.php' );
    return new 
    CreateAction();
    break;
    case 
    'modify':
    require_once( 
    'modify-action.class.php' );
    // could be to modify a news item for example?
    return new ModifyAction();
    break;
    case 
    'remove':
    require_once( 
    'remove-action.class.php' );
    return new 
    RemoveAction();
    break;
    default:
    require_once( 
    'list-action.class.php' );
    // no action given so default to a listing instead
    return new ListAction();
    break;
    }
    }

    PHP Code:
    interface IAction {
    public function 
    __construct();
    public function 
    isSecure();
    }

    abstract class 
    BaseAction implements IAction {
    public function 
    __construct() {}

    abstract public function 
    executeRequest $request );
    }

    class 
    ListAction extends BaseAction {
    public function 
    __construct() {}
    public function 
    executeRequest $request ) {
    $model SomeGateway::loadSomeFinder::findByLimit) );
    // model holds multiple rows of data
    // load template document into view helper
    $helper = new ViewHelper'templates/en/template.tpl' );
    // pass model data to view helper
    $helper -> run$model );
    // whereby the template is returned with the inserted model data
    return $helper -> toHtml();
    }
    }

    class 
    ViewHelper {
    private 
    $template;
    public function 
    __construct$template ) {
    $this -> template file_get_contents$template );
    }

    public function 
    toHtml() {
    return 
    str_replace'<news />'$this -> html$this -> template );
    }

    public function 
    run$resultset ) {
    $html '';
    $template_row file_get_contents'templates/en/row-template.tpl' );
    foreach( 
    $resultset as $row ) {
    $fragment str_replace'<item-id />'$row['id'], $template_row );
    $fragment str_replace'<item-title />'$row['title'], $fragment );
    $fragment str_replace'<item-body />'$row['body'], $fragment );
    $html .= $fragment;
    }
    $this -> html $html;
    }

    So, your template is like this,

    Code:
    <html>
    <head>
    <title>Todays News</title></head><body>
    <h1>Todays News</h1>
    <news />
    </body></html>
    The row template therefore is where you place each news item (row) to, which is then added to the template at the end for you,

    Code:
    <!-- row template -->
    <div><h2><item-title /></h2></div>
    <div><p><item-body /></p><p>Click <a href="index.php?act=view&id=<id />">here</a> to read more</p></div>
    By no means is what I've posted comprehensive but it's only to give you an idea, I'm sure other members can contribute something of their own

  23. #23
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you give me an example of what ViewAction, CreateAction, ModifyAction, RemoveAction or ListAction might look like?

    PHP Code:
    // no request mapper since this is a basic example
    interface IController {
    public function 
    __constructRequest $request );
    public function 
    invoke();
    }

    class 
    FrontController implements IController {
    private 
    $request;
    public function 
    __constructRequest $request ) {
    $this -> request $request;
    }
    public function 
    invoke() {
    $action $this -> select$this -> request -> getParameter'act' ) );
    $view $action -> execute$this -> request );
    // in this example the view is a template but could just
    // as well be an object
    echo( $view );
    }
    private function 
    select$action ) {
    switch( 
    $action ) {
    case 
    'view':
    require_once( 
    'view-action.class.php' );
    return new 
    ViewAction();
    break;
    case 
    'create':
    require_once( 
    'create-action.class.php' );
    return new 
    CreateAction();
    break;
    case 
    'modify':
    require_once( 
    'modify-action.class.php' );
    // could be to modify a news item for example?
    return new ModifyAction();
    break;
    case 
    'remove':
    require_once( 
    'remove-action.class.php' );
    return new 
    RemoveAction();
    break;
    default:
    require_once( 
    'list-action.class.php' );
    // no action given so default to a listing instead
    return new ListAction();
    break;
    }
    }


  24. #24
    SitePoint Guru 33degrees's Avatar
    Join Date
    May 2005
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Humm, that switch statement doesn't smell so good... something like this would be more flexible

    PHP Code:

    private function select$action ) {
        
    $filename "$action-action.class.php";
        
        if (!
    file_exists($filename)) {
            if (
    $action == 'list') {
                die(
    "The default action class file was not found");
            } else {
                
    $this->select('list');    
            }
        }    
        
        require_once(
    $filename);
        
        
    $classname ucfirst($action).'Action';
        
        if (!
    class_exists($classname)) {
            
    $this->select('list');
            
        } else {
            return new 
    $classname;    
            
        }
        


  25. #25
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you give me an example of what ViewAction, CreateAction, ModifyAction, RemoveAction or ListAction might look like?
    The examples are your basic CRUD functions...

    PHP Code:
     private function select$action ) {
        
    $filename "$action-action.class.php";
        
        if (!
    file_exists($filename)) {
            if (
    $action == 'list') {
                die(
    "The default action class file was not found");
            } else {
                
    $this->select('list');    
            }
        }    
        
        require_once(
    $filename);
        
        
    $classname ucfirst($action).'Action';
        
        if (!
    class_exists($classname)) {
            
    $this->select('list');
            
        } else {
            return new 
    $classname;    
            
        }
        

    Yes, the SWITCH could be seen as a bad smell (proberly), and I would suggest using the Chain of Responsibility as one possible solution. If done properly you could have them configurable, so you create the chain on the fly via an XML file


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
  •