开发者

How to handle a navigation that depends on URL parameters

This might be a bit hard for me to explain, but I will try anyway.

In my PHP appl开发者_StackOverflow中文版ication, I have a main navigation which leads to different "Trackers", where I add a parameter "?trk=1" to the end of the URL for example.

I have a secondary navigation which I need to be different for the different "Trackers" in the system. From the main tracker page, I can easily get the id of the "trk" parameter, and create the secondary navigation based on that. But, my app has many sub-pages below the "Tracker" level. For example, every Tracker has Programs, where the Programs have Projects, etc.

One of the solutions I was considering was passing the "trk" parameter through all my pages. This way, my tracker.header.php file (which is run in all levels below the Tracker level in my app) could correctly generate a custom secondary navigation for each Tracker.

I was sorta thinking I could make a class for my secondary menu. I would create this menu object in tracker.header.php and I would then have access to this object variable throughout all lower levels which would then be very easy to customize per tracker. Is it standard to hold all HTML generated in PHP in variables and then just echo the variables in the very last lines of the application?


Yes, it is fairly standard to have a single entry page (index.php) and all pages called are merely index.php with parameters either via GET or POST. Many people also use apache rewrites to hide the fact that they are doing this.

In your example, you might have a URL such as this: "./index.php?trk=1&prog=23&proj=12" and you would have to decide what to do in the event that a particular parameter was not passed.

It is also common to hold page state information in a session.


I think there's two questions in your post, but I'll do what I can to answer them:

If you need to pass along a category that's buried within other categories, there's a few ways to go about it, some easier than others. One such approach would be to append the chain of categories browsed in the URL itself. For example, ?trk=123&program=456&proj=789 could be reduced to ?trk=123|456|789 where the script splits trk every pipe character. If the end section is missing, you can assume it hasn't been set yet.

Also, it is common practice to output the page while it's being generated, usually with echo or by escaping the PHP tags for static HTML. If you're going to be sending a large page (such as one with a large amount of table data in the middle), then waiting until the page is fully generated may cause some browsers to assume the server has stopped responding. Also, in such situations it may be advisable to somehow make the giant midsection of the webpage asynchronous so users won't get impatient.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜