Distinguish between requesting HTML Partial and Full Layout for same URL / Route
At the moment I am writing the routing code for my MVC framework which is capable of rendering HTML partials (views). These partials can be loaded server-side or client-side (using Mootools HTML request). The layout of every page is then build from multiple partials. As I want it to be ReST compliant, each HTML partial (view) maps to an URL in a defined URL space, as follows (indentation shows the intended document structure):
...
/
/navigation
/content
/profile
/profile/1
/profile/1/message/
/profile/1/message/1
/profile/1/message/2
...
Now the problem is that I want people to be able to visit "/profile" and then not being show开发者_开发知识库n the HTML partial, but instead the full layout. I was thinking about the following:
1) Creating a separate namespace / prefix in the URL for partials, for example:
- /profile for the full layout
- /partial/profile for the partial
2) Sending a custom HTTP header to request for a partial or no custom HTTP header to request for the full layout.
The first option would be more ReST compliant (cache friendly), but I am still unsure (and that is the real problem) about other options that might still be unknown to me.
Before I start implementing one of the above solutions, I've got the following questions:
- What alternatives do I have to distinguish between requesting partials and full layouts?
- What are the best practices to keep the client-side state / context of each partial?
When you request client-side, the server will receive the extra header HTTP_X_REQUESTED_WITH with the value xmlhttprequest.
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) &&
strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
....
}
This is a reliable header to test for, rather than add a custom type.
Option 1 is definitely a better solution that option 2. In a RESTful system we create new resources all the time to make up for the lack of methods.
Creating a custom header is a really bad idea.
精彩评论