many pages, or one page with $_GET parameter

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
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

superdezign wrote:
d11wtq wrote:This one does include the view, albeit not a composite one, but it's not hard to modify to work with a composite view.
Then I'll certainly give it a read. ^_^
I just got to the view and was trying to decipher a good method for using the consistent outer template with the inner templates.
You can have a default "layout", but you could then provide additional methods in the controller for "setLayout()" and even "setLayoutEnabled()" to enable/disable the overall layout. I'm not going to go too far into a discussion on views unless ~scottayy wants us to though since I think it's out of scope and it's another topic again ;) The again, even the most complex views are probably simpler than any controller :P How to effectively provide View helpers in an scalable/encapsulated fashion is an interesting topic.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

d11wtq wrote:I did post the ActionController class for a reason, although I should really have had a "Controller" class which but FrontController and ActionController extend. I didn't want to overwhelm though ;) See, now that you've got all responsibility for invoking actions in the front controller it's a little more difficult to control flow between controllers. I guess you have your own clean way of doing that too, but I was going to post this next... I'd be interested in starting a new thread and discussing advtantages and disadvantages in depth because I know you have some great ideas on this sort of stuff. I find it an interesting topic too.

I'm throwing in forwarding here with only a little modification:
I updated my code above to add forward via returning a controller/action pair. I don't like to be required to inherit an ActionController class because they usually only provide MVC automation with is often not needed. I prefer to have the option.

Move to a new thread if there is interest in exploring these ideas. :)
(#10850)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

arborint wrote:I updated my code above to add forward via returning a controller/action pair. I don't like to be required to inherit an ActionController class because they usually only provide MVC automation with is often not needed. I prefer to have the option.
Nice :) I agree that that's very clean. I've never seen anyone do it that way but it's certainly given me some ideas. I'm gonna have a play around a bit and if anything interesting happens I'll start a new thread :)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

@aborint - Should error handling be processed in a different class? Like an error subcontroller? Or implicitly in each method, as your code seems to imply?

EDIT| @d11wtq, you're like the php thomas edison ;d New ideas flowing all the time. I likes it.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

scottayy wrote:@aborint - Should error handling be processed in a different class? Like an error subcontroller? Or implicitly in each method, as your code seems to imply?
I am not exactly sure what you are asking. I will say that I think the Front Controller should have two defaults: one for when now controller/action is specified and another for when there is an error trying to dispatch. That sort of implies that I am for having the Front Controller forward to an error controller if there is a dispatching error.

There are a few choices that you need to make with Front Controller default and errors. The main ones are what goes to the default controller and what goes to and error controller. Also what if the class is loaded, but the method is not found, is that an error or should it try the default method name (e.g., run() or indexController()) before erroring.

Once the Action Controller is dispatched then the error handling issues changes and most are application specific. But your idea of a perhaps standard error handler is interesting. Did you have some ideas?
(#10850)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

d11wtq wrote:I took the liberty of writing a short blog entry extending upon the sort of code I posted here ;) If it's of any help:

http://www.w3style.co.uk/a-lightweight- ... -for-php-5
That is a very enlightening article d11. I look forward to reading it again and playing with it at work a bit. Thanks for writing that.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

d11wtq, arborint thanks for posting examples, it helped me a lot writing my first Front Controller a couple days ago.
User avatar
xpgeek
Forum Contributor
Posts: 146
Joined: Mon May 22, 2006 1:45 am
Location: Kyiv, Ukraine
Contact:

Post by xpgeek »

d11wtq wrote:skipped...

Code: Select all

<?php

  public function dispatch() {
    $page = !empty($_GET["page"]) ? $_GET["page"] : "home";
    $action = !empty($_GET["action"]) ? $_GET["action"] : "index";
    $class = ucfirst($page) . "Controller";
    $file = PAGE_DIR . "/" . $class . ".php";
    if (!file_exists($file)) {
      exit("Page not found");
    }
    require_once $file;
    $page = new $class();
    $page->dispatchAction($action);
  }
Now point your browser to:

http://localhost/index.php?page=survey&action=create
Sorry for offtop, but with you dispath function i can include any files through you code.
Try something like this:

http://localhost/index.php?page=/../../ ... ion=create

Of course you have to find riht path to passwd file.

Correct me if i mistake.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

It may have been brought up in his article, but I know there was mention of validation in there somewhere.

Ideally you would have the page data being validated against what is known in a database (which could also control your page not found issues).
User avatar
hongster
Forum Newbie
Posts: 6
Joined: Fri Mar 30, 2007 12:54 am

Post by hongster »

Everah wrote:Ideally you would have the page data being validated against what is known in a database (which could also control your page not found issues).
"page data" - Are you referring to the controller and function names extracted fome the URL string? Are you suggesting that we store all controller names and function names in a database and verify that the page data is valid?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Verifying that the object exists as well as the method is usually enough for the front controller to continue on. E.g, is_callable(), method_exists(), etc.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Yep, there's no validation in there. ~arborint has a good example in his code by simply stripping non-alpha characters.
Post Reply