如何处理依赖于 URL 参数的导航
这对我来说可能有点难以解释,但我无论如何都会尝试。
在我的 PHP 应用程序中,我有一个主导航,它会导致不同的“跟踪器”,例如,我在 URL 末尾添加参数“?trk=1”。
我有一个辅助导航,对于系统中不同的“跟踪器”,我需要使用不同的辅助导航。从跟踪器主页面,我可以轻松获取“trk”参数的 id,并基于该参数创建辅助导航。但是,我的应用程序在“跟踪器”级别以下有许多子页面。例如,每个跟踪器都有程序,其中程序有项目等。
我正在考虑的解决方案之一是在我的所有页面中传递“trk”参数。这样,我的 tracker.header.php 文件(在我的应用程序中 Tracker 级别以下的所有级别中运行)可以正确为每个 Tracker 生成自定义辅助导航。
我有点想我可以为我的二级菜单开设一门课程。我将在 tracker.header.php 中创建此菜单对象,然后我可以在所有较低级别访问此对象变量,然后可以很容易地自定义每个跟踪器。 将 PHP 中生成的所有 HTML 保存在变量中,然后仅在应用程序的最后几行中回显变量,这是标准做法吗?
This might be a bit hard for me to explain, but I will try anyway.
In my PHP application, 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,拥有一个入口页面 (index.php) 是相当标准的,并且调用的所有页面都只是带有通过
GET
或POST
参数的 index.php。许多人还使用 apache 重写来隐藏他们正在这样做的事实。在您的示例中,您可能有一个如下所示的 URL:
"./index.php?trk=1&prog=23&proj=12"
并且您必须决定在未传递特定参数的事件。在
会话
中保存页面状态信息也很常见。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
orPOST
. 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
.我认为您的帖子中有两个问题,但我会尽力回答它们:
如果您需要传递隐藏在其他类别中的类别,有几种方法可以实现,其中一些比其他更容易。一种这样的方法是附加在 URL 本身中浏览的类别链。例如,
?trk=123&program=456&proj=789
可以简化为?trk=123|456|789
,其中脚本将trk< /code> 每个管道字符。如果末尾部分丢失,您可以假设它尚未设置。
此外,通常的做法是在生成页面时输出页面,通常使用
echo
或转义静态 HTML 的 PHP 标记。如果您要发送一个大页面(例如中间有大量表数据的页面),那么等待页面完全生成可能会导致某些浏览器认为服务器已停止响应。此外,在这种情况下,建议以某种方式使网页的巨大中间部分异步,这样用户就不会失去耐心。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 splitstrk
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.