Controllers?

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

Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Controllers?

Post by Ree »

I keep finding threads with discussions about front/application/page controllers which aren't that clear to me. Currently I'm trying to understand how front controllers are used. I've read the article on phppatterns.com, but somehow it wasn't very useful.
As far as I know, a front controller should get request data and 'decide' what action should be taken. A simple switch statement can also be called a front controller, no? It gets request data (say, $_GET['action']) and decides what to do (switch ($_GET['action'])). But I also know that front controllers can be more complex than switch()'es. How should I implement such front controller myself? Is it used alone or together with other objects? Where do I start? Would be really nice to see a few examples with your comments.
User avatar
dbevfat
Forum Contributor
Posts: 126
Joined: Tue Jun 28, 2005 2:47 pm
Location: Ljubljana, Slovenia

Post by dbevfat »

You could read a thread about it @SP forums, although not all have the nerves ... http://www.sitepoint.com/forums/showthread.php?t=271112. The thread is all about building a very thin FC, with no extra weight.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

dbevfat wrote:You could read a thread about it @SP forums, although not all have the nerves ...
Sorry to go a tiny bit off topic, but that thread is the perfect answer to the comment "not all have the nerves". A thread that does pop-ups every page, and has 16 pages of responses to the simple question of a switch statement and a front controller is exactly why many "dont have the nerves". I still get shocked each time I visit that site that they still have the nerves to continue that style.
User avatar
dbevfat
Forum Contributor
Posts: 126
Joined: Tue Jun 28, 2005 2:47 pm
Location: Ljubljana, Slovenia

Post by dbevfat »

Well, I've been following that thread and I must say that I am pleased with the result which is a nice 'core' for a front controller. And for that to be achieved, many things were said and a lot of stuff had to be debated through. The thread is not an ordinary forum thread, it's actually more like a developers' correspondence channel. :)
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Well, there have been a lot of posts at sitepoint about creating a xxx altogether.. And despite their is a lot of intellectual debate i haven't seen much usable code..

Anyway, i think the most usable code in that specific thread was the first post =P There used to be a nice article on phppatterns.com too but that site seems to be down again :(
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Post by Ree »

dbevfat wrote:You could read a thread about it @SP forums, although not all have the nerves ... http://www.sitepoint.com/forums/showthread.php?t=271112. The thread is all about building a very thin FC, with no extra weight.
Yes, I did check the thread before but it's way (WAY) too much for this kind of question. There's a lot of 'extra weight' there, which newcommers like me usually do not want. :) For now, I want to find out the basics and advantages of using the method.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Here is what (imho) a frontcontroller pattern is all about: Make sure all subprograms/scripts/page perform the same initial code.

http://java.sun.com/blueprints/corej2ee ... oller.html
http://msdn.microsoft.com/library/defau ... roller.asp
http://www.martinfowler.com/eaaCatalog/ ... oller.html

And then you can start looking for a solution to this problem. In PHP i've seen the following approaches:
1-) direct everything to a single page and then dispatch from there
2-) make sure each script includes/requires init.php, or inherit from a base class (or use auto_prepend)
User avatar
dbevfat
Forum Contributor
Posts: 126
Joined: Tue Jun 28, 2005 2:47 pm
Location: Ljubljana, Slovenia

Post by dbevfat »

timvw wrote:... And despite their is a lot of intellectual debate i haven't seen much usable code...
Hmmm, really? Did you download, look at, and try the skeleton FC?
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

dbevfat wrote:Hmmm, really? Did you download, look at, and try the skeleton FC?
Not being sarcastic: Where is it? I've read the first few pages and the last few pages of the 16 page thread, and don't see the link to it. Its not in a recurring header on all pages in the thread, and I dont see the link.
User avatar
dbevfat
Forum Contributor
Posts: 126
Joined: Tue Jun 28, 2005 2:47 pm
Location: Ljubljana, Slovenia

Post by dbevfat »

http://ap3.sf.net

as said before, this thread is a comm-chan for developers, that's why it looks very much confused. The link was mention a few times in the middle, that's about it.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

dbevfat wrote:as said before, this thread is a comm-chan for developers, that's why it looks very much confused. The link was mention a few times in the middle, that's about it.
/me howls with laughter

This is such a horrible (or great, depending on the bias!) example for OOP and sitepoint.

What the original poster on that thread posted, at ~ 30 lines, becomes hundreds of lines to form a "skeleton". A skeleton that includes magic quotes handling, page controlling, action mapping, and much much more. ie, hardly a skeleton in any sense, despite being named that, and that being the goal for it. This from a thread that gets turned into a "comm channel", starting with a simple/common request.

The fact that it was hard to find (in 16 pages, ya think?) just adds to the humor for me. I really don't know that the thread was a good resource to send someone trying to find a clear thread about front controllers. If any thread would be unclear, its that one.

Sadly, I don't know of many better resources to offer instead, beyond the already mentioned martinfowler page. I'd link phppatterns, but it goes down more often than a well-designed OOP system. ;)
User avatar
dbevfat
Forum Contributor
Posts: 126
Joined: Tue Jun 28, 2005 2:47 pm
Location: Ljubljana, Slovenia

Post by dbevfat »

I agree it's possibly one of the worst still-readable resources about front controllers on the web. But it totally doesn't matter that it started out of a simple question, don't you think? It started and results came up, that's it. So what if it was a simple question.

It's a well designed FC skeleton, not a class too much for a flexible FC system. It's a FC, with a Request, RequestMapper, Actions and a FilterChain. Seems quite light and flexible to me ;).

PS. the word on the web is that phpPatterns will soon be back, rewritten for dokuWiki.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

dbevfat wrote:I agree it's possibly one of the worst still-readable resources about front controllers on the web. But it totally doesn't matter that it started out of a simple question, don't you think? It started and results came up, that's it. So what if it was a simple question.
I was contrasting the result with the question and initial post on that thread. My point was that the "pure" OOP solution in "skeleton" form was over 10 times more complicated/long. Thats the reason I mentioned the initial post.
dbevfat wrote:It's a well designed FC skeleton, not a class too much for a flexible FC system. It's a FC, with a Request, RequestMapper, Actions and a FilterChain. Seems quite light and flexible to me ;).
Light is certainly relative. I think it is a fantastic example of the "hidden complexity" many in the pure-OOP world hint at, while more pragmatic programmers call it out for what it is: More code.

I don't totally agree that it is well-designed (many aspects didn't need to be included for a pure FC), but it appears the intent was for it to be far more than a pure FC. As a skeleton for developing sites/apps in general, I would agree its fairly well-designed. But then again, I disagree that its all needed for most sites/apps in general - making it less of a 'skeleton', and more of a 'baseline framework for pure OOP development'.

Again, contrast that with the original poster here and on that thread, who were looking for a pure-play FC, and you can understand my laughter (I hope).
dbevfat wrote:PS. the word on the web is that phpPatterns will soon be back, rewritten for dokuWiki.
Eww. But at least we'll have an url to point people at again. :)
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Re: Controllers?

Post by McGruff »

Ree wrote:As far as I know, a front controller should get request data and 'decide' what action should be taken. A simple switch statement can also be called a front controller, no?
FrontController is a design pattern which could be implemented in many ways, including a simple switch. You wouldn't find something like that in an OOP design but yes it does fit the pattern.

Both PageControllers and FrontControllers receive input and decide how to respond. The key difference is that a FrontController also has to identify the request type. "view topic", "post message", and so on. PageControllers just have to figure out how to respond to a given type. For a "view topic" request are they authenticated? are they authorised? can the topic data be pulled? Whatever influences the choice of page served up to the user.

I woudn't worry too much about allegedly pragmatic programmers. The more code argument gets tiresome and is rather naive. The general shape of the skeleton code sounds right although I haven't looked at it in detail (unit testing didn't seem to be a feature so quickly I lost interest). The InterceptingFilter pattern is often used with a FrontController to carry out processing tasks common to all requests. You can do the same kind of thing in a PageController superclass.
Last edited by McGruff on Sun Oct 02, 2005 1:53 pm, edited 1 time in total.
Ree
Forum Regular
Posts: 592
Joined: Fri Jun 10, 2005 1:43 am
Location: LT

Re: Controllers?

Post by Ree »

McGruff wrote:Both PageControllers and FrontControllers receive input and decide how to respond. The key difference is that a FrontController also has to identify the request type.
I think the FC should be universal enough to accept ALL request data. I guess you could pass the whole $_REQUEST to the front controller. Then from the $_REQUEST 'data soup' it would determine which action should be taken and then some page controller would do its job. Could it be something like that? Or maybe I'm wrong here? I guess you could also pass some Request object instead of $_REQUEST.
Locked