Page 2 of 2

Posted: Sun Aug 12, 2007 2:25 pm
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.

Posted: Sun Aug 12, 2007 3:12 pm
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. :)

Posted: Sun Aug 12, 2007 4:57 pm
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 :)

Posted: Sun Aug 12, 2007 8:51 pm
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.

Posted: Sun Aug 12, 2007 11:12 pm
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?

Posted: Sun Aug 12, 2007 11:43 pm
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.

Posted: Fri Aug 24, 2007 8:35 am
by Zoxive
d11wtq, arborint thanks for posting examples, it helped me a lot writing my first Front Controller a couple days ago.

Posted: Fri Aug 24, 2007 8:52 am
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.

Posted: Fri Aug 24, 2007 10:48 am
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).

Posted: Tue Aug 28, 2007 4:49 am
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?

Posted: Tue Aug 28, 2007 11:51 am
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.

Posted: Tue Aug 28, 2007 12:03 pm
by Chris Corbyn
Yep, there's no validation in there. ~arborint has a good example in his code by simply stripping non-alpha characters.