SitePoint Sponsor

User Tag List

Page 5 of 5 FirstFirst 12345
Results 101 to 120 of 120
  1. #101
    SitePoint Wizard
    Join Date
    May 2003
    Location
    Berlin, Germany
    Posts
    1,829
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well my approach is that The FrontController really should only execute a PageController based on system input. The PC itself is then responsible for what filters it has to pass.

    So you'd have:

    PHP Code:
    class UserAddController extends PageController {
       var 
    $filters = array();

        function 
    addFilter() {}
        function 
    executeFilters() {}

        function 
    execute() {} // execute PC

    Provides a lot more flexibility.

    And then again Fowler said that a pattern is only a guide to a solution. One always has to modify the pattern for his own use - a pattern is not defined to do this and that.

  2. #102
    SitePoint Guru
    Join Date
    Oct 2001
    Posts
    656
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ironically as it is, this thread was meant to sort out some confusion, but it turns out that I was one of the people who was confused. I can see now that I have mixed up the PageController and FrontController patterns. What I posted initially as PageController classes in this example, are what Martin Fowler in his Patterns of Enterprise Application Architecture (PoEAA) book refers to as Commands. I only recently bought this book and based on that decided to change this example. Everything named *PageController is now named *Command. The example is still valid, but the names are now compatible with Fowler's definition of the patterns.

    So, apologies go out to everyone that participated in this thread, especially seratonin, dagfinn. The reason that I did not reply to some of the questions you posed to me in the later part of this discussion is that I wanted to read PoEAA first before I caused even more confusion.

    Some rectifications are in order:

    Quote Originally Posted by Captain Proton
    From post #10: What Fowler proposes is that a FrontController invokes commands on the model of the application and contains the logic for choosing a view. Fowler's FrontController is essentially a mix of the potentially large switch statements of my FrontController plus the logic for choosing a view that you find inside each PageController.

    The code that you probably have in 'ViewNewsCommand' is probably something like this: create a DAO, find a News object by id and create a View object to display that news. When you follow Fowler's FrontController pattern, this code would be implemented inside the FrontController itself with the help of something like a GetNewsCommand. Now this may not seem like a big difference, but trust me, it is. GetNewsCommand is independent of the kind of presentation layer involved (it only loads a News object from the DB, no view choosing whatsoever), while ViewNewsCommand is not: it knows what View object it must create to display the news.
    Wrong of course. The Commands in Fowler's FrontController pattern are not necessarily commands on the model of the application. Completely ignore the second paragraph.

    After re-reading the PoEAA chapter on Web Presentation Patterns, I am convinced that there is NOTHING you can do with this combination of Front Controller and Page Controller that you cannot do with Front Controller alone.
    Completely agree. Apologies again.

    From post #22: Seratonin, alright, but then I don't even see the difference between your Commands and my PageControllers, except the name of course.
    I see a similarity between Commands and PageControllers in that they are both MVC input controllers. But there is a difference of course.

    Quote Originally Posted by Seratonin
    From post #22: I applaud your effort here to make things clear and simple, but I still think that you are blurring the definition of the page controller pattern by mixing the page controller and the front controller patterns together. Someone coming here wanting to understand the page controller pattern might be confused by how you are using the pattern. I propose that you name your new pattern something different than page controller to distinguish it from a true page controller pattern (as Fowler and others intended).
    You are right of course. I think that after the changes I made to my example, I have now simply given an example of the FrontController pattern as applied to PHP. It does not have anything to do with the PageController pattern anymore now.

    Quote Originally Posted by seratonin
    Captain, A while back in this thread you stated that Mojavi wasn't a true Front Controller implemnetation. I was hoping you would spend some time elaborating on that statement.
    I pull back my statement for reasons explained above

    Quote Originally Posted by Widow Maker
    From post #126:Shame also, that Fowler didn't explain the Command pattern either, since he made a reference to it. Btw, wasn't the Command pattern given a spot on the ISA site prior to the books publication ?
    Just a note: the Command pattern is explained in the GoF book.

    In short: replace most instances of the word 'PageController' with the word 'Command' as in the context of the FrontController pattern, and this thread is still valid mostly..

  3. #103
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I read back through this thread and though it dissects the definitions of the different Controllers, it does not address whether any of them are a good idea in PHP. And probably more important, does the Smalltalk/Java implementation of these patterns make sense in PHP. My experience is that for web pages, PageControllers and InterceptingFilters are not such a good idea. And, FrontControllers can cause as many problems as they solve in PHP.

    All of these patterns seem to drift together in PHP because the lack of app server, servlets and massive library. Pages like the following link are interesting in one way, but in the PHP problem space they can seem downright silly.

    http://java.sun.com/developer/techni...nsRoadmap.html

    I just don't think we PHP programmers need to make things seem as esoteric as the GoF crowd do. We have patterns, but they are more straightforward. The workflow for a PHP page is just simpler that a system with an app server (and Workflows may be a better design model).

    Perhaps rather than look at the patterns we should look to their app server and libraries. For example, I think RequestController is probably a clearer name than FrontController for PHP (as this thread proves).

  4. #104
    Non-Member
    Join Date
    Oct 2004
    Location
    downtown
    Posts
    145
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thought I'd add this, may have some kind of revelance to this discussion.
    Attached Files Attached Files

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

    Exclamation

    Can anyone post a complete example of a dummy script that used the MVC Design pattern for PHP4.

    - FrontpageController
    - PageController
    - DataAccess Object (with relation to view i.e. MySQL queries)
    - Initialization (in index.php)

    Thanks.
    Last edited by Ian R. Gordon; Feb 14, 2005 at 02:57.
    Ian Gordon
    CSS / XHTML / PHP Programmer
    http://www.iangordon.us

  6. #106
    SitePoint Enthusiast
    Join Date
    Jan 2003
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ian R. Gordon
    Can anyone post a complete example of a dummy script that used the MVC Design pattern for PHP4.

    - FrontpageController
    - PageController
    - DataAccess Object (with relation to view i.e. MySQL queries)
    - Initialization (in index.php)

    Thanks.
    Read over this current thread which has a lot of excellent material in it as well as this one which is more recent.

  7. #107
    SitePoint Evangelist Ian R. Gordon's Avatar
    Join Date
    Feb 2004
    Location
    New York
    Posts
    474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But is that thread have code that is 100% supported by PHP4 Object Oriented Programming?

    PHP Code:
     class Dummy {
     
        var 
    $page // PHP4 Syntax
        
    private var $page //PHP5 Syntax
     
        
    function Dummy() {
     
             
    // PHP4 Constructor
     
        
    }
     
        
    __constructor Dummy() {
     
            
    // PHP5 Constructor
     
        
    }
     
     } 
    I am looking for PHP4 Syntax not PHP5!
    Ian Gordon
    CSS / XHTML / PHP Programmer
    http://www.iangordon.us

  8. #108
    SitePoint Zealot
    Join Date
    May 2004
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Shouldn't be TOO hard to apply these threads to PHP4 coding. After all, they're discussing principles and techniques rather than posting actual code.

    My host uses PHP4, I've not used PHP5 yet, but I've been able to more or less port all the PHP5 examples here to PHP4 - yes, there are exceptions to this of course, but in general terms I've not had a problem.

  9. #109
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cool

    Quote Originally Posted by Captain Proton

    class UserEditCommand extends BaseCommand
    {
    .....
    }

    class UserListCommand extends BaseCommand
    {
    ......
    }
    Hi,

    Could you clarify what is BaseCommand, please ?

    I think I do not understand what should be inside ...


  10. #110
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)
    I have a few more questions and thought since I've had so many lately, I'd start asking them here.

    1) If your app has functions that service many things, and are really considered generic in usage, how do you handle them? Do you:

    A) Put them all in a single class and make them static?
    B) Put each in ait's own class and make it static? (SRP)
    C) Put them in a linear include file?

    2) What is your balance between include and autoload?

    A) I autoload everything.
    B) I include all common things in a single file and autoload the rest.
    C) I include all common things in a single file include the rest as needed.
    D) I include everything individually as needed.

    How granular is your file structure?

    1) I put related things in a single file.
    2) I use an even mix of compound and single class files.
    3) I put everything in it's own file.

    I know there's no real right or wrong here, I just want to get a feel for what others are doing.

    Last edited by Jelena; Apr 3, 2007 at 04:58.

  11. #111
    SitePoint Wizard
    Join Date
    May 2003
    Location
    Berlin, Germany
    Posts
    1,829
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Captain proton
    I see a similarity between Commands and PageControllers in that they are both MVC input controllers. But there is a difference of course.
    Hrm what difference? I have always wondered what the real difference is and yet I have never seen any. They both act as Input Controllers and select the appropriate View based on the input.

    So what's the difference between Commands and PageControllers?

  12. #112
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi all
    check it out
    i found it's useful
    http://www.tonymarston.net/php-mysql...ontroller.html

  13. #113
    Afraid I can't do that Dave Hal9k's Avatar
    Join Date
    Mar 2004
    Location
    East Anglia, England.
    Posts
    640
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Very interesting thread.

    I especially like the code Proton post in post #24. Something about chaining the intercepting filters together just seemed to click, a little like linked-lists I suppose.

    I intend to code a system as Pallan suggested basically for code modularity.

    However I guess then I'd have to call it something different than "Front Controller" as in post #24, as I would have to have multiple "Front Controllers" invalidating the description of a "single entry point".

    I guess what I'd end up is with a Page Controller that uses Intercepting Filters. It would mean I'd have to repeat the intercepting filter calls, but I don't mind if the payoff is modularity.

    Though getting the intercepting filter output into some sort of View will be interesting...

    Edit:

    ...in regards to that I thought of a little header redirect trick that redirects to a login page. hmm.
    Last edited by Hal9k; Jun 30, 2006 at 06:03.

  14. #114
    SitePoint Evangelist
    Join Date
    Apr 2003
    Location
    North Carolina, USA
    Posts
    415
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    After reading about 5 pages of this thread, I took a stab at my own MVC classes based heavily on other code presented in this thread so far:

    PHP Code:
    <?php

    define
    ('NL'"<br />\r\n");

    interface 
    RequestProcessor
    {
        public function 
    execute();
    }

    class 
    FilterManager implements RequestProcessor
    {
        private 
    $controller null;
        private 
    $filters = array();
        private 
    $pointer 0;

        function 
    FilterManager($controller)
        {
            
    $this->controller $controller;
        }

        function 
    addFilter($filter)
        {
            
    $this->filters[] = $filter;
        }

        function 
    execute()
        {
            
    $this->executeNext();
        }

        function 
    executeNext()
        {
            if (
    $this->pointer count($this->filters))
            {
                
    $filter $this->filters[$this->pointer];
                
    $this->pointer++;
                
    $filter->execute($this);
            }
            else
            {
                
    $this->controller->execute();
            }
        }
    }

    class 
    InterceptingFilter
    {
        const 
    BAD_VALUE 2;
        private 
    $test;

        function 
    InterceptingFilter($test)
        {
            
    $this->test $test;
        }

        function 
    execute(FilterManager $filterManager)
        {
            
    $this->test++;
            echo 
    "Preprocessing value of test: " $this->test NL;

            if (
    $this->test != self::BAD_VALUE)
            {
                
    $filterManager->execute();
            }
            else
            {
                echo 
    "Bad value detected in InterceptingFilter. Application must exit." NL;
                exit();
            }

            
    $this->test++;
            echo 
    "Postprocessing value of test: " $this->test NL;
        }
    }

    class 
    FrontController implements RequestProcessor
    {
        function 
    FrontController()
        {
        }

        function 
    execute()
        {
            
    $pageController PageControllerFactory::getPageController($_REQUEST['page']);

            
    $pageController->execute();
            
    $view $pageController->getView();
            
    $view->render();
        }
    }

    class 
    PageControllerFactory
    {
        static function 
    getPageController($pageController)
        {
            
    $filename 'includes/controllers/' $pageController '.controller.php';
            
    $classname $pageController 'Controller';
            include_once(
    $filename);
            return new 
    $classname();
        }
    }

    $filterManager = new FilterManager(new FrontController());
    $filterManager->addFilter(new InterceptingFilter(0));
    $filterManager->addFilter(new InterceptingFilter(1));
    $filterManager->addFilter(new InterceptingFilter(2));
    $filterManager->execute();

    ?>
    This prints out:
    Preprocessing value of test: 1
    Preprocessing value of test: 2
    Bad value detected in InterceptingFilter. Application must exit.
    I have a couple of issues with my own design though:

    1. FilterManager seems to act as the main class in this application. The reason I say this is because if FrontController desires a FilterManager, then the FrontController must wait for an instance of the FilterManager class to be created so that it can attach itself to it. This is shown on the bottom part of the code.

    2. Do you think InterceptingFilter needs to be derived from some set interface or abstract class? InterceptingFilter just seems to be some random class that is floating by itself and is suddenly added on to the FilterManager without having to follow any 'rules'.

    I hope this makes some sense.

    By the way, thanks for the good read everyone who contributed to this thread. I learned a lot today.

  15. #115
    SitePoint Member
    Join Date
    May 2006
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could someone please help me out. I'm very new to this and I have a quick question. Let's say you have form:

    Code:
    <form method="post" action="XXXXXXXXXX">
    
    <input type="text" name="title">
    <input type="submit" name="submit" value="Add">
    
    </form>
    Firstly, what would the action be on this form, and how would you read it using classes? Let's say the action was: controllers/addController.php?page=members or something. In that page you had this:

    Code:
    class addController { 
    
           function addController() {
                     
                  switch($_GET['page']) {
    
                            case "members": 
                                        $insert = new Member
                                         break;
                  }
    
           }
    
    }
    How would you get it to use that swtich statement?

    Silly questions I know, but I'm just a bit lost here.

    }

  16. #116
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does anyone have I guess a mini MVC framework which is usable instead of using the Zend Framework only for their MVC feature.

  17. #117
    SitePoint Enthusiast
    Join Date
    May 2007
    Location
    Lappeenranta, Finland
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gcbdm View Post
    Does anyone have I guess a mini MVC framework which is usable instead of using the Zend Framework only for their MVC feature.
    Here's a simple MVC framework I have used. I like the design and it works quite well. Make sure to download the updated version (MVC 1.1) further in the comments. Btw, the site is in Finnish but don't let that scare you
    PHP web application development: Perhosgrafiikka

  18. #118
    SitePoint Addict crabby80's Avatar
    Join Date
    May 2007
    Posts
    387
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    To hopefully clear up some MVC confusion

    Pretty cool MVC tutorial here guys for those who are still trying to grasp the concept.

    Peace!

    Crabby

  19. #119
    SitePoint Member
    Join Date
    May 2010
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,

    I used this. I cant capture the variable out side switch.

    class BaseController{

    static $action1 = $_GET['cat'];

    public static function get_cat(){

    switch($action1){

    case "gui":
    $rec=Category::display_sub_cat(1);
    //$category=Category::display_cat_name(1);
    include(VIEW_PATH.'category.php');
    return $rec;
    //break;

    case "wwind":
    $rec=Category::display_sub_cat(2);
    $category=Category::display_cat_name(2);
    include(VIEW_PATH.'category.php');
    break;

    }

    If I use switch($_GET['cat']){} it works. Why isn't it working this way???
    this is the error
    Parse error: syntax error, unexpected T_VARIABLE

    Also after i use it like switch($_GET['cat']){} my 'category.php' is not getting included. No errors but the file is not getting included.. Please someone help me to solve this.

  20. #120
    SitePoint Addict webaddictz's Avatar
    Join Date
    Feb 2006
    Location
    Netherlands
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please note that this topic has not been active since May 8, 2009. You might want to start a new topic instead.

    [ EDIT: Excellent point. If anyone has an MVC "tip" type of contribution, please add them to the "help" sticky until the current sticky improvements are completed.
    - Or PM a member of the Progam Your Site team.
    If you have a question or problem that is not being discussed elsewhere, please start a new thread.
    Thanks. Mittineague ]
    Yes, I blog, too.


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
  •