Determining Commands in Request

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

Post Reply
Theory?
Forum Contributor
Posts: 138
Joined: Wed Apr 11, 2007 10:43 am

Determining Commands in Request

Post by Theory? »

So I'm trying to make my Front Controller and I want to just be able to validate the request, then pass it to a wrapper class to act as a handler. Then back in the FC it would then see what command is being requested and then execute the command and pass it any additional parameters given in the request.

Where I'm running into trouble is trying to determine how to accurately be aware that a request being made is passing the right parameters to the application. So on forums for example, I may have urls like:

something.com/2/4321/new

Where the first parameter is the forum id, the second is the thread id, and the fourth is the command to make a new post. Or:

something.com/2/new

Where NOW the new command is still being called, but because it's called at the forum level, it's now going to call the new thread command

This doesn't even begin to try and take into account threaded posts. Sure I can know which thread I'm posting in, but to which post am I responding to? I would imagine that part of the request would come from some Ajax element in the system, but I don't know how to add those parameters to my request in addition to parameters from the URI.
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Determining Commands in Request

Post by allspiritseve »

You can still have normal parameters even on a clean url system:

Code: Select all

/forums/forum?action=new
/forums/forum/thread?action=reply
/forums/form/thread/post?action=reply
That might make things a bit easier for you. A good rule of thumb is to keep resources in the URL, and everything else as parameters.

I would also try and use names rather than ids for urls as much as possible, because they have meaning for the user.
Theory?
Forum Contributor
Posts: 138
Joined: Wed Apr 11, 2007 10:43 am

Re: Determining Commands in Request

Post by Theory? »

allspiritseve wrote:You can still have normal parameters even on a clean url system:

Code: Select all

/forums/forum?action=new
/forums/forum/thread?action=reply
/forums/form/thread/post?action=reply
That might make things a bit easier for you. A good rule of thumb is to keep resources in the URL, and everything else as parameters.

I would also try and use names rather than ids for urls as much as possible, because they have meaning for the user.
How would those URI's help me determine which post I'm responding to or which thread I'm in, etc?
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Determining Commands in Request

Post by allspiritseve »

Theory? wrote:How would those URI's help me determine which post I'm responding to or which thread I'm in, etc?
Substitute names or ids for "forum", "thread", and "post"
Theory?
Forum Contributor
Posts: 138
Joined: Wed Apr 11, 2007 10:43 am

Re: Determining Commands in Request

Post by Theory? »

So I would need to have all CRUD classes inherit from the same generic class to ensure the actions new and reply are always there, right? I was toying with the idea of using the command pattern and just make generic New and Reply objects, but I've never read anything about the performance gains or losses. The issue I then ran into is how do these generic command objects understand their context? Would they even be able to or would i have to make child classes for specific implementations of the commands?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Determining Commands in Request

Post by Christopher »

Theory? wrote:So on forums for example, I may have urls like:

something.com/2/4321/new

Where the first parameter is the forum id, the second is the thread id, and the fourth is the command to make a new post. Or:

something.com/2/new
The problem with URLs like these is that they require logic to determine the context. It is probably easier to use more traditional class/method/param style URLs. So something like:

something.com/thread/new/2/4321

Where thread::new() knows to check for two more parameters. And

something.com/forum/new/2

Where forum::new() knows to check for one more parameter.
(#10850)
Theory?
Forum Contributor
Posts: 138
Joined: Wed Apr 11, 2007 10:43 am

Re: Determining Commands in Request

Post by Theory? »

arborint wrote:
Theory? wrote:So on forums for example, I may have urls like:

something.com/2/4321/new

Where the first parameter is the forum id, the second is the thread id, and the fourth is the command to make a new post. Or:

something.com/2/new
The problem with URLs like these is that they require logic to determine the context. It is probably easier to use more traditional class/method/param style URLs. So something like:

something.com/thread/new/2/4321

Where thread::new() knows to check for two more parameters. And

something.com/forum/new/2

Where forum::new() knows to check for one more parameter.
Would it then be smarter to have the new() methods on the forum and thread models utilize the same generic NewCommand object?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Determining Commands in Request

Post by Christopher »

Theory? wrote:Would it then be smarter to have the new() methods on the forum and thread models utilize the same generic NewCommand object?
If you want to, especially as a default, that would be fine. I don't think you want to tie the URL convention too far into the code. Selecting the controller class and method is probably enough. Beyond that it would just be a default for the common cases. But I often have multiple models used within a request, so need a way to load specific models and not just the one with the same name as the controller.
(#10850)
Post Reply