Front/Action Controller and Template solution

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Front/Action Controller and Template solution

Post by Eran »

I see.. I misread you. I don't see arborint suggesting he handles form data in his views, he just says he sometimes uses models directly in the views which is very acceptable when the situation demands it.
blueyon
Forum Commoner
Posts: 76
Joined: Tue Oct 30, 2007 9:53 am

Re: Front/Action Controller and Template solution

Post by blueyon »

Yeah, sorry this is something he said in anolher thead.

I don't mind using models in a view, but when you are submitting data from small part of the page hes sort of using a view object linked to a template.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Front/Action Controller and Template solution

Post by Christopher »

blueyon wrote:This is probably not the best one because its from tony marstons web site:

http://www.tonymarston.co.uk/php-mysql/ ... ler-02.png
Tony Marston can be depended on to always get it wrong. :) He is a smart guy who is widely considered a fool ... I would strongly recommend not following him...

How about:
IBM: http://www-128.ibm.com/developerworks/w ... /wi-arch6/
Martin Fowler: http://martinfowler.com/eaaCatalog/mode ... oller.html
Microsoft: http://msdn.microsoft.com/en-us/library/ms978748.aspx
Sun: http://java.sun.com/blueprints/patterns ... ailed.html
Wikipedia: http://en.wikipedia.org/wiki/Model-view-controller
Trygve Reenskaug: http://heim.ifi.uio.no/trygver/themes/m ... index.html

For the record, I don't have any problem with your code. I think it is very good. Nor is Controller::render() a guarantee of not being MVC ... but it is a strong indicator in my experience because (to me) it indicates a misunderstanding MVC. Certainly your code is has more separation than Hurreman's -- but again that is neither good nor bad ... just a design choice.

And for the record, I don't think MVC is either good or bad -- it is just a pattern that solves some problems well. I know Marston simply changes design concepts into something he can understand and then writes copiously and erroneously about them. I think Zend, Smfony, Akelos, RonR, Solar, etc. also have changed MVC mostly due to their lack of understanding. A few have knowingly sought something different (though still keeping the MVC name for marketing points).
(#10850)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Front/Action Controller and Template solution

Post by Christopher »

blueyon wrote:This is what I have just stated, but arborint is processing form data in his views. Thats why I advised him to rename the some of his views to controllers.
Where am I "processing form data in his views"? (not that I have a problem with that if done knowingly ;))
(#10850)
User avatar
Hurreman
Forum Commoner
Posts: 61
Joined: Sat Apr 29, 2006 8:42 am

Re: Front/Action Controller and Template solution

Post by Hurreman »

arborint wrote:You might want to get involved with Skeleton. There are 3-4 of us working on it now. It is so close to what you have that you wouldn't need to change your code much at all. But you would get the benefit of all the other things available. We are always looking for people to get involved.
I've had a couple of looks at Skeleton, but I have trouble grasping it all. I'd probably need to actually sit down and use it for a couple of weeks in order to understand what's going on :roll:.
That's the reason that I decided to write something of my own, so I can fully understand each step of the process, even it that means having less functionability. But so far, Skeleton has been my largest source for inspiration whenever I've gotten stuck. My main problem is that I don't spend nearly enough time coding, since I actually work as one of those xhtml/css designers all coders seem to love(to hate) :wink:.

It's frustrating, being in love with both design, usability, and also server code. As I look back over the years, I've probably spent the time 50% coding and 50% designing. I've got experiences from C++ and OpenGL programming, just as I can put together some neat stuff in graphics software like Photoshop (but who can't these days?) and 3D Studio Max. I know I'd be able to become a better coder if I focused more on PHP, but I also love not needing to rely on other artists and designers to actually get stuff done (and look good).

Ok, this reply ended up a bit :offtopic:, so I'll just get back to work, and see if I can actually have another look at Skeleton tonight.
blueyon
Forum Commoner
Posts: 76
Joined: Tue Oct 30, 2007 9:53 am

Re: Front/Action Controller and Template solution

Post by blueyon »

I think all those diagrams are for desktop application MVC.

They are using the observer pattern to exchange data between the model and view. If you look at the diagram in the right hand corner on wiki (http://en.wikipedia.org/wiki/Model-view-controller) it shows the observer pattern.

Web MVC should be a lot simpler.
arborint wrote:Nor is Controller::render() a guarantee of not being MVC ... but it is a strong indicator in my experience because (to me) it indicates a misunderstanding MVC
(http://www.phpwact.org/pattern/model_view_controller)

Model - Encapsulates core application data and functionality Domain Logic.
View - obtains data from the model and presents it to the user.
Controller - receives and translates input to requests on the model or the view.

Remember the controller decides what view to render. The controller does not do the rendering its self. Its just calling which ever class is required to render the page.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Front/Action Controller and Template solution

Post by Christopher »

blueyon wrote:I think all those diagrams are for desktop application MVC.

They are using the observer pattern to exchange data between the model and view. If you look at the diagram in the right hand corner on wiki (http://en.wikipedia.org/wiki/Model-view-controller) it shows the observer pattern.

Web MVC should be a lot simpler.
As I often say, there are two MVC discussions. One is about the actual pattern and the other is about frameworks. If you are going to say things like it's "desktop application MVC" or "Web MVC" then you are really talking about framework design. I have no problem diverging from the MVC pattern. Indeed I encourage it. But it needs to be done with awareness of the trade-offs.
blueyon wrote:(http://www.phpwact.org/pattern/model_view_controller)

Model - Encapsulates core application data and functionality Domain Logic.
View - obtains data from the model and presents it to the user.
Controller - receives and translates input to requests on the model or the view.

Remember the controller decides what view to render. The controller does not do the rendering its self. Its just calling which ever class is required to render the page.
I think if you read what Jeff Moore wrote about MVC from the top -- rather than jumping to his generalization about the parts -- you will start to get to the deeper meaning of MVC. I'd recommend reading Reenskaug and Fowler. Like related patterns it is about finding ways for the user to interact with the Domain.
(#10850)
User avatar
Hurreman
Forum Commoner
Posts: 61
Joined: Sat Apr 29, 2006 8:42 am

Re: Front/Action Controller and Template solution

Post by Hurreman »

I wasn't sure if I should make a new thread or not, but this questions is relevant, so I'll post it here as well.

I'm currently rewriting an old registration form, which uses three steps before the user is registered and ready to go ( four steps if you count the mail verification process ).
Previously, I had four separate files/actions for this (procedural), and now that I'm back to using my MVC:ish approach, I'm not sure if I should stick with separate files for each action, or let one "controller" handle all steps. However, I feel that this is kind of the job of the front/action-controller.

Either I end up with 4-5 different actions (and a couple of views/templates), or I go for the more 'bloated' file and limit the number of files. What would you guys suggest?

Example: (not tested)

Code: Select all

 
<?php
require_once('../dev/lib/Template.php');
class register
{
    public function execute()
    {
        switch($_GET['do'])
        {
            case 'step1':
                $template = 'tpl/register_step1.php';
                break;
                
            case 'step2':
                $template = 'tpl/register_step2.php';
                /* .. Code to set sessions for the input from step 1 .. */
                break;
                
            case 'step3':
                $template = 'tpl/register_step2.php';
                /* .. Code to set sessions for the input from step 2 .. */
                break;
            
            case 'finish':
                $template = 'tpl/register_step2.php';
                /* .. Code to save registration to the database .. */
                break;
            
            default: 
                $template = 'tpl/register_step1.php';
                break;
        }
        
        $tpl = new cTemplate($template);
     
        echo $tpl->render(); 
    }
}
?>
 
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Front/Action Controller and Template solution

Post by Christopher »

You probably want to move to multi-Action controllers, if only to have the option. I think being able to select the the file/class ('controller') and method ('action') in the URL is becoming pretty standard. Many frameworks also allow you to select the sub-directory ('module') with the URL as well.

It really becomes as question of code organization at that point. You need to decide how to group things. If you push most of the functionality out to the Model and View classes, then even with many Action methods your Controller will only be request handling and program flow code.
(#10850)
User avatar
Hurreman
Forum Commoner
Posts: 61
Joined: Sat Apr 29, 2006 8:42 am

Re: Front/Action Controller and Template solution

Post by Hurreman »

arborint wrote:You probably want to move to multi-Action controllers, if only to have the option. I think being able to select the the file/class ('controller') and method ('action') in the URL is becoming pretty standard. Many frameworks also allow you to select the sub-directory ('module') with the URL as well.
You mean I'd do something like www.domain.com/register/step1 , and that the controller could look something like below?

Code: Select all

 
<?php
class register
{
    public function step1()
    {
        /* Step 1 specfic code... */
    }
    
    public function step2()
    {
        /* Step 2 specfic code... */
    }
    
    public function step3()
    {
        /* Step 3 specfic code... */
    }
}
?>
 
But then again, it's like you say. It comes down to preference on how to organize the code. But most of the code for the registration will be put in the model, where I'll just be doing something like $register->addMember($name,$login,$pwd, .. etc ..); from within the controller/action, keeping it from being too bloated. So yes, the controller will mostly just be handling requests.

Now I just have to adapt my Front Controller to call different methods instead of just calling execute.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: Front/Action Controller and Template solution

Post by Kieran Huggins »

You've got the idea!

A question though: why all the registration steps? You need to collect data & send a verification e-mail in one step, then verify the registration in a second step. What are the other steps for?
User avatar
Hurreman
Forum Commoner
Posts: 61
Joined: Sat Apr 29, 2006 8:42 am

Re: Front/Action Controller and Template solution

Post by Hurreman »

Kieran Huggins wrote:You've got the idea!

A question though: why all the registration steps? You need to collect data & send a verification e-mail in one step, then verify the registration in a second step. What are the other steps for?
It's for a World of Warcraft site, and one of the registration steps is being used for character details. Step 1 = User details, Step 2 = Character details, Step 3 = Collect data and send e-mail verification, Step 4 = Link from email clicked, activate account.

But I suppose I could merge the user and character details into one step.
User avatar
Hurreman
Forum Commoner
Posts: 61
Joined: Sat Apr 29, 2006 8:42 am

Re: Front/Action Controller and Template solution

Post by Hurreman »

Is there any way to find out if a method exists? When I now pass a method through the querystring, I need to check if there's such a method before calling it.

Code: Select all

 
if(isset($_GET[$this->method_param]))
{
    $this->method = preg_replace('/[^a-zZ-Z0-9\_\-]/', '', $_GET[$this->method_param]);
}
else
{
    $this->method = 'execute';  
}
 
$obj->{$this->method}();
 
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Front/Action Controller and Template solution

Post by Christopher »

Hurreman wrote:Is there any way to find out if a method exists?
method_exists() ;)
(#10850)
User avatar
Hurreman
Forum Commoner
Posts: 61
Joined: Sat Apr 29, 2006 8:42 am

Re: Front/Action Controller and Template solution

Post by Hurreman »

Ah yes.. there it is! I was googling for is_method... :p
Post Reply