Page 1 of 1
Request Forwarding
Posted: Sun Oct 25, 2009 11:01 am
by superdezign
I'm curious as to how other people handle request forwarding. I'm redeveloping my MVC framework to work faster and be easier to develop on, as it was developed when I was still new to PHP and has many design flaws.
One of these is request forwarding. Basically, I mean that I interpret URLs recursively. So, if I had this URL:
Code: Select all
http://domain.tld/user/profile/username
My code would interpret it like this:
Code: Select all
"/user/profile/username" sent to Front Controller
controller = null
action = "user"
"user" action forwards request to User Controller
"/profile/username" sent to User Controller
controller = "user"
action = "profile"
"profile" action forwards request to Profile Controller
"/username" sent to Profile Controller
controller = "profile"
action = "username"
Default action uses "username" to display profile
This works fine for me if I send variables as the end of the URL, like query variables. But if I were to send a URL like:
Code: Select all
http://domain.tld/user/profile/username/comments
Then, the default action of the Profile Controller would need to look ahead in the URL, and, if it finds another element, forward the request to that controller (i.e. Comments Controller) and, somehow, also forward the username.
I'd just like to know how other people deal with this problem. Is it better to solve this problem in the URL itself, or handle request forwarding differently?
Re: Request Forwarding
Posted: Sun Oct 25, 2009 2:59 pm
by alex.barylski
Forwarding could be implemented by simply calling the next controller:action method explicitly, so long as you don't need any additional parameters, etc.
One caveat being, that if your front controller supports pre/post filters, they should be re-executed upon a forward, incase you perform authentication checks in a pre-filter, forwarding to a controller without re-filtering request might result in a controller action being invoked illegally.
For that reason I had to re-engineer my controller architecture. If your requirements do not include pre or post you should be OK with directly calling the action method.
Cheers,
Alex
Re: Request Forwarding
Posted: Sun Oct 25, 2009 5:03 pm
by superdezign
But how would I define my controller and action in the given situation? As of now, I'd have to manually check for additional actions.
Which, actually, brings me to another problem that I have with my forwarding. In a normal website, if you go to a page that does not exist, you would get an error. If I go to "
http://domain.tld/user/list" and get a website, and then go to "
http://domain.tld/user/list/invalidPage," I would get the same page for the both of these if my controller stops at the list action of the user controller. The only way to know if there are invalid pieces of the URL is to manually check in every action.
Do you guys feel that this is an issue that needs addressing or do you just allow it to work this way?
Re: Request Forwarding
Posted: Mon Oct 26, 2009 2:26 am
by alex.barylski
Do you have a router? Soudns like maybe your expecting to much from the front controller and it's complicating your design?
Can you post some code, that might us a better idea as to where you might improve?
Cheers,
Alex
Re: Request Forwarding
Posted: Mon Oct 26, 2009 12:57 pm
by superdezign
The router I made just gets the first two elements of the url as the controller and action. The controllers have a forwardRequest() method that
- looks at the current controller and action,
- finds the controller file (i.e. /user/profile = Controller_User_Profile in Controller/User/Profile.php) using it's own controller file (unless it is the front cotnroller),
- checks for the action method and calls it if it exists, or the defaultAction() method if it does not.
Each of the action methods have the ability to call the forwardRequest() function in their controller. When called in any controller other than the front controller, it removes the first node of the URL, making the old action the new controller. However, doing this means that if there are more nodes in the URL than I check for, then any extra nodes will not cause an error.
I'm not sure if this is good or bad. I mean, I guess it can be good for creating SEO-friendly URLs by adding "trash" to the end of the URL.
I'd post code, but I'm on campus at the moment, so I don't have access to it.
Re: Request Forwarding
Posted: Sat Oct 31, 2009 11:13 pm
by josh
You could just have /:module/:controller/:action and put the redirect inside of the actual action.
Having so much complexity baked into the routing scheme sounds like something bad