Page 1 of 1
Determining Commands in Request
Posted: Thu Dec 25, 2008 3:33 pm
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.
Re: Determining Commands in Request
Posted: Thu Dec 25, 2008 4:42 pm
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.
Re: Determining Commands in Request
Posted: Thu Dec 25, 2008 5:03 pm
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?
Re: Determining Commands in Request
Posted: Thu Dec 25, 2008 5:23 pm
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"
Re: Determining Commands in Request
Posted: Fri Dec 26, 2008 12:26 pm
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?
Re: Determining Commands in Request
Posted: Fri Dec 26, 2008 1:10 pm
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.
Re: Determining Commands in Request
Posted: Fri Dec 26, 2008 1:26 pm
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?
Re: Determining Commands in Request
Posted: Fri Dec 26, 2008 1:35 pm
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.