SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 47
  1. #1
    SitePoint Evangelist Ian R. Gordon's Avatar
    Join Date
    Feb 2004
    Location
    New York
    Posts
    474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb Procedural FrontController

    Okay, so I lied, I really can't wrap my mind around the object oriented methodology as it applies to creating an application. Anywho, here is what I am curious about a procedural approach...

    The way I am structuring my application is that based on the URL ("index.php?section=blah&page=main"), I want the application the query the MySQL database for a page.

    Now what I am curious about is how to set this up. Here is what I was thinking:

    PHP Code:
      // DECLARE GLOBALS
       
    switch($request) {
      
           case 
    "action";
           break;
      
           case 
    "page";
           break;
      
           case 
    "section";
           break;
      
           case 
    "subsection";
           break;
           
           default 
    "page";
           break;
      
       }
       
       global 
    $_REQUEST['$request'];
       
       
    // ACTION HANDLERS
       
    action_request() {
       
          if(isset(
    $_REQUEST['$request']) {
          
              if (
    $_REQUEST['action']) {
              
                  
    // query mysql database for index.php?action=addpage
              
              
    } elseif($_REQUEST['page']) {
              
                  
    // do something else
              
              
    } elseif($_REQUEST['section']) {
      
                  
    // do something else
      
              
    } elseif($_REQUEST['subsection']) {
      
                  
    // do something else
      
              
    }
          
          } else {
          
              
    // show custom $_REQUEST variable not allowed
          
          
    }
       
       }
       
       
    process_request() {
      
           
    // process query result for output back to browser
      
       

    The idea is that based on a global variable "action, page, section, subsection" a query is processed to a MySQL database for data based on the value of that variable ("index.php?page=main" - returns the 'main' content from the mysql database) however, if that variable doesn't exist or if the value doesn't exist an error is shown.

    So,

    Code:
      // variable AND value exist
      index.php?page=moo
       - MySQL is queried for "moo" in pages, then shown in browser
      
      // variable exists but value doesn't
      index.php?section=foobar&page=nonpage
       - Error is triggered, "Page doesn't exist in section foobar".
      
      // variable NOR value exist
      index.php?section=foobar&apple=green
       - Error is triggered, "Incorrect variable 'apple' please check URL"
      
      // Basically the idea is to prevent users from entering unauthorized data
      // and code in the variables that could hijack the script
    I am really now sure how I would code it without a serious nest of "if" statements or Switch statements (i expect a few if and switch statements).

    Can someone help me out?

    NOTE: I am not interested in Object Oriented approaches or the effectiveness an object oriented approach would yield.
    Ian Gordon
    CSS / XHTML / PHP Programmer
    http://www.iangordon.us

  2. #2
    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 Ian R. Gordon
    NOTE: I am not interested in Object Oriented approaches or the effectiveness an object oriented approach would yield.
    Why?

    Quote Originally Posted by Ian R. Gordon
    I am really now sure how I would code it without a serious nest of "if" statements or Switch statements (i expect a few if and switch statements).
    You could use a dictionary (array). Sort of like :
    PHP Code:
    define('DEFAULT_CONTROLLER', -1);
    $GLOBALS['controllers'] = Array(
        
    'action' => 'action_controller',
        
    'page' => 'page_controller',
        
    'section' => 'section_controller',
        
    'subsection' => 'subsection_controller',
        
    DEFAULT_CONTROLLER => 'page_controller'
    );

    function 
    get_controller() {
        if (isset(
    $_GET['controller']) && array_key_exists($_GET['controller'], $GLOBALS['controllers'])) {
            return 
    $GLOBALS['controllers'][$_GET['controller']];
        } else {
            return 
    $GLOBALS['controllers'][DEFAULT_CONTROLLER];
        }
    }

    function 
    page_controller() {
        if (!isset(
    $_GET['page_id'])) {
            return 
    trigger_error("No page_id is given");
        }
        
    $page find_page_by_id($_GET['page_id']);
        if (
    is_null($page)) {
            return 
    trigger_error("Page doesn't exist");
        }
        
    // do stuff with $page here ...
    }

    error_reporting(E_ALL);
    call_user_function(get_controller()); 

  3. #3
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ian R. Gordon
    NOTE: I am not interested in Object Oriented approaches or the effectiveness an object oriented approach would yield.
    If you don't care how the app is designed you can hack it together any way you like. Most php apps are coded like that including many very successful ones such as phpBB and phpMyAdmin for example. Just don't ask me to look at it. I feel nauseous already.

    Will be glad to help with "advanced" questions though. You can get a good educaton here if you make the effort and quite frankly you'd be crazy to miss out on that opportunity.

  4. #4
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    You can get a good educaton here if you make the effort and quite frankly you'd be crazy to miss out on that opportunity.


    Not the impression that I've been left with recently

    I am not interested in Object Oriented approaches or the effectiveness an object oriented approach would yield.
    I wouldn't rule this out entirely...

  5. #5
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    Not the impression that I've been left with recently
    Post less. Listen more.

  6. #6
    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 McGruff
    Will be glad to help with "advanced" questions though. You can get a good educaton here if you make the effort and quite frankly you'd be crazy to miss out on that opportunity.
    Bad day McGruff?

    You don't need to tell people "shut up and listen", this is a discussion forum not a drill school.

    Douglas
    Hello World

  7. #7
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DougBTX
    You don't need to tell people "shut up and listen"
    I know. I didn't think I had. I thought I was encouraging the poster to keep trying with OOP. You'll probably get better advice here than you would on a lot of college courses.

    Edit: sorry you mean my comments about the good Dr? I agree I shouldn't need to say that. I'd prefer it if the moderators would have a word.
    Last edited by McGruff; Apr 13, 2005 at 10:20.

  8. #8
    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 McGruff
    You'll probably get better advice here than you would on a lot of college courses.
    That might be true - it was my general impression of straight IT college courses - that's why I'm not on one

    Douglas
    Hello World

  9. #9
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd prefer it if the moderators would have a word.
    Have I done something wrong McGruff? Now I think your just making it personal, and that is something we don't need on the forum

  10. #10
    SitePoint Evangelist Ian R. Gordon's Avatar
    Join Date
    Feb 2004
    Location
    New York
    Posts
    474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post

    You guys have hijacked my thread, can we get back on topic?

    Procedural Method because I don't understand Object Oriented Methods, they have a learning curve, I don't have time to learn about, well for the deadline I am trying to meet anyway.

    I know the advantages and power of OO I am just saying, I want procedural methodologies...
    Ian Gordon
    CSS / XHTML / PHP Programmer
    http://www.iangordon.us

  11. #11
    SitePoint Evangelist Ian R. Gordon's Avatar
    Join Date
    Feb 2004
    Location
    New York
    Posts
    474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken
    You could use a dictionary (array). Sort of like :
    PHP Code:
     define('DEFAULT_CONTROLLER', -1);
     
    $GLOBALS['controllers'] = Array(
         
    'action' => 'action_controller',
         
    'page' => 'page_controller',
         
    'section' => 'section_controller',
         
    'subsection' => 'subsection_controller',
         
    DEFAULT_CONTROLLER => 'page_controller'
     
    );
     
     function 
    get_controller() {
         if (isset(
    $_GET['controller']) && array_key_exists($_GET['controller'], $GLOBALS['controllers'])) {
             return 
    $GLOBALS['controllers'][$_GET['controller']];
         } else {
             return 
    $GLOBALS['controllers'][DEFAULT_CONTROLLER];
         }
     }
     
     function 
    page_controller() {
         if (!isset(
    $_GET['page_id'])) {
             return 
    trigger_error("No page_id is given");
         }
         
    $page find_page_by_id($_GET['page_id']);
         if (
    is_null($page)) {
             return 
    trigger_error("Page doesn't exist");
         }
         
    // do stuff with $page here ...
     
    }
     
     
    error_reporting(E_ALL);
     
    call_user_function(get_controller()); 
    Thanks, this is exactly what I was looking for, does anyone else have an opinion on how this could be made better using procedural coding?

    Thanks.
    Ian Gordon
    CSS / XHTML / PHP Programmer
    http://www.iangordon.us

  12. #12
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am really now sure how I would code it without a serious nest of "if" statements or Switch statements (i expect a few if and switch statements).

    Can someone help me out?
    You probably have three general options:

    1. "a serious nest of "if" statements or Switch statements"

    2. Use various PHP features, such as calling a function using a variable for the function name or hashes.

    3. OOP implementing the appropriate pattern.

    And, no.


    PS - Hey guys I'm feeling left out, can I get in on this spat? (Or is spat the wrong pattern? Maybe the tiff or dust-up or kerfuffle patterns would be a better fit here)
    Christopher

  13. #13
    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 McGruff
    I agree I shouldn't need to say that. I'd prefer it if the moderators would have a word.
    I'm not sure what you mean here. The mods come into play if someone is making trouble - I've not seen anything like that from Dr Livingston(e). IMO, you learn about programming from reading and writing code, but most of all by talking about your code. (That's what pair programming is all about , and it also happens to be somewhere where a college course could go wrong - too much lecturing, not enough talking.)

    Douglas
    Hello World

  14. #14
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ian R. Gordon
    I know the advantages and power of OO I am just saying, I want procedural methodologies...
    Sorry my only advice would be don't do it like that. I'm not sure if this would be a topic for the advanced forum.

  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 arborint
    2. Use various PHP features, such as calling a function using a variable for the function name or hashes.

    3. OOP implementing the appropriate pattern.
    Douglas votes for 2 & 3, but I guess you all know that by now
    Hello World

  16. #16
    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 Ian R. Gordon
    You guys have hijacked my thread, can we get back on topic?
    Sorry, should probably have just ignored the thread; you've declared the answer off topic! (Why do you think OO techniques were invented for?)

    Douglas
    Hello World

  17. #17
    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 Ian R. Gordon
    Thanks, this is exactly what I was looking for, does anyone else have an opinion on how this could be made better using procedural coding?

    Thanks.
    Some articles I wrote many moons ago: part 1 and part 2 when I did procedural programming. If I still used this style, I would probably have a double dispatch which looked something like:
    PHP Code:
    include 'setup.php'//some constants, db connections and such

    switch ($action figure_out_action()) {
    case 
    'action1':
    case 
    'action2':
    case 
    'action3':
    include 
    'actions/'.$action.'.php';
    default:
    }

    switch (
    $view figure_out_view()) {
    case 
    'page1':
    case 
    'page2':
    case 
    'page3':
    include 
    'views/'.$view.'.php';
    default:
    include 
    'views/index.php';

    or perhaps validate the actions and views off of some arrays.

    most code in the actions/*.php files would redirect back to a "view" mode, especially if they performaned any commands which updated the database.

    HTH
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  18. #18
    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 Ian R. Gordon
    Thanks, this is exactly what I was looking for, does anyone else have an opinion on how this could be made better using procedural coding?
    PHP Code:
    error_reporting(E_ALL);

    define('DefaultController''article');

    function 
    article_controller() {
        if (!isset(
    $_GET['id'])) {
            return 
    trigger_error("Pick a page.");
        }
        
    $page find_page_by_id($_GET['id']);
        if (
    is_null($page)) {
            return 
    trigger_error("Page doesn't exist");
        }
        
    // do stuff with $page here ...
    }

    $requested_controller = isset($_GET['controller']) ? $_GET['controller'] : DefaultController;

    if (
    function_exists($requested_controller.'_controller')) {
        
    $controller $requested_controller.'_controller';
    } else {
        
    trigger_error("Controller \"$requested_controller\" not found");
        
    $controller DefaultController.'_controller';
    }

    $controller(); 
    Convention over configuration anyone?

    Douglas
    Hello World

  19. #19
    SitePoint Evangelist Ian R. Gordon's Avatar
    Join Date
    Feb 2004
    Location
    New York
    Posts
    474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Doug, I appreciate your help. I know it is difficult to jump from OO back to prodecural, I am just trying to keep this application as simple as possible without making it too complicated for me to understand.
    Ian Gordon
    CSS / XHTML / PHP Programmer
    http://www.iangordon.us

  20. #20
    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 Ian R. Gordon
    Thanks Doug, I appreciate your help. I know it is difficult to jump from OO back to prodecural, I am just trying to keep this application as simple as possible without making it too complicated for me to understand.
    My example is based on object orientated ideas: all you need to call a function in PHP is a string with the name of the function.

    That's what the "if" doeas at the bottom of the code - make a string you can use to call a function.

    To make it an "object orientated" example, it would just return an object instead of a string. Beyond that, the example would be identical. Objects are that easy to use

    Douglas
    Hello World

  21. #21
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would probably have a double dispatch which looked something like:
    Which is the widely used (an proven) Page Controller pattern, or if split up it is the widely used (an proven) combination of a Front Controller disparching an Application Controller.
    Christopher

  22. #22
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by McGruff
    I feel nauseous already
    Sorry that came across as being extremely impolite which wasn't what I meant to say at all. I guess most OOP programmers react to globals like finding a caterpillar in your sandwich but I guess you know that and have chosen not to work in this way. If I could have persuaded you to give OOP a second try I would - but best of luck anyway.

  23. #23
    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 McGruff
    Sorry that came across as being extremely impolite which wasn't what I meant to say at all. I guess most OOP programmers react to globals like finding a caterpillar in your sandwich but I guess you know that and have chosen not to work in this way. If I could have persuaded you to give OOP a second try I would - but best of luck anyway.
    I just assumed it was a joke and laughed at the code

    I mean, define('DEFAULT_CONTROLLER', -1);??

    Douglas
    Hello World

  24. #24
    SitePoint Guru OfficeOfTheLaw's Avatar
    Join Date
    Apr 2004
    Location
    Quincy
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ian: I've often found the best way to learn oop is by refactoring old procedural code. Becasue you're used to procedural code, it's what you'll understand and be able to write best. I've often started out by writing the simplest thing I can think of, and then once the functionality is there, revisit it and identify the "objects" that are there, i.e. combining code and methods related to a user to a user object, etc. Then identify various patterns and refactor them to their OOP equivalents, i.e. a switch statment that performs actions for a page to a page controller, table queries to tableGateway, multiple if/elsif statments to StrategyPattern, etc.

    James Carr, Software Engineer


    assertEquals(newXPJob, you.ask(officeOfTheLaw));

  25. #25
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ian, just for fun, here's a quote for you:

    I don't predict the demise of object-oriented programming, by the way. Though I don't think it has much to offer good programmers, except in certain specialized domains, it is irresistible to large organizations. Object-oriented programming offers a sustainable way to write spaghetti code. It lets you accrete programs as a series of patches. Large organizations always tend to develop software this way, and I expect this to be as true in a hundred years as it is today.
    http://www.paulgraham.com/hundred.html
    Hello World


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
  •