关于菜单和菜单的简单 PHP 问题内容最佳实践
好的,这是我第一次构建一个完整的网站并使用 php。现在我遇到一个问题:
假设我的网站只有一个带有菜单的标题和一个包含内容的区域。当然,我想要一个 header.php 和几个内容文件,例如 content1.php content2.php 和 content3.php 。这样,您只需更改 1 个文件中的菜单即可,如您所知。
那么,最好如何构建网站:
A. 在每个内容文件中放入类似的内容:
<?php include 'header.php'; ?>
here the content of the content page 1
B. 制作一个索引文件,其中包含以下内容:
<?php include 'header.php'; ?>
<?php include 'content1.php'; ?>
那么当在菜单中单击 content2.php 的链接时,如何做到这一点?标题也仍在该页面上吗?
C、还有别的事吗?也许有一个关于如何制作此类页面的好教程?
OK, so for the first time I am building a full website and working with php. Now I encounter a problem:
Let'ss say my site is simply has a header with a menu and a area with content. Of course, I would like to have a header.php and several content files like content1.php content2.php and content3.php for example. This way you only have to change the menu in 1 file as you would understand.
How then is it best to build the site:
A. put something like this in every content file:
<?php include 'header.php'; ?>
here the content of the content page 1
B. make an index file with something like:
<?php include 'header.php'; ?>
<?php include 'content1.php'; ?>
how then is it done that when in the menu the link to content2.php is clicked the header is still on that page too?
C. something else? Maybe a good tutorial on how to make these kind of pages?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您希望最大化重用代码/元素的好处,那么您的第二个选择是正确的:
操作方法如下(一个简单的示例):
使用查询字符串 -
mod_rewrite 通过
可以让这个变得漂亮。然后根据请求提供主要内容部分。index.php
脚本路由所有类似的请求 (content1,2,3)例如链接:
并检测要提供的内容:
不仅有一个地方可以更改标题,而且现在有一个地方可以更改整个布局。
当然,这只是一个简单的示例,有许多 PHP 框架可以为您完成这一切(使用 MVC)。
If you're looking to maximize the benefit of reusing code/elements, then you're on the right track with your second option:
Here's how (a simplistic example):
Route all of the similar requests (content1,2,3) through your
index.php
script using the query string -mod_rewrite
can make this pretty. Then serve the main content section based on the request.For example a link:
And detecting the content to serve:
Not only is there a single place to change your header, but now there's a single place to change your entire layout.
Of course this is just a simplistic example, there are many PHP frameworks that do all this for you (using MVC).
简单而不是包含内容文件的index.php,您可以这样做:
header.php:
content2.php:
content2.php:
Simple instead of a index.php including the content files, you do:
header.php:
content2.php:
content2.php:
简短的回答,不。对于小型网站来说,最快的方法是使用@mark b 描述的标头方法。它不仅速度快,而且还允许您依赖 URL 路径,这对 SEO 有利。
也许答案很长。框架本质上已被 dom 所取代,其中将内容分配给 div 并进行更新。它速度快,比旧框架更容易控制,并且可以异步完成(通过 AJAX)。但是,它也比旧的 target= 功能需要处理更多的工作。客户喜欢它,但编码确实需要时间。事实上,如果您正在处理大量页面和大量内容,那么这会花费大量时间。
另一种选择是使用 Zend Framework 等模板系统将视图构建到统一的模板中进行显示。但是,它会增加相当大的框架开销。
最后,由于您可以使用 PHP,因此您可以从数据库中提取所有内容,并在一个“页面”上运行整个网站。 URL 字符串将包含一个参数,告诉它从数据库中提取哪些内容并在通用“getter”页面中替换。一个真正的 CMS 系统。
祝你好运。
Short answer, no. The quickest way for a small website would be to use the header method that @mark b describes. Not only is it fast, but it also allows you to rely on URL paths which can be beneficial to SEO.
Long answer, maybe. Frames have essentially been replaced by use of the dom, where one assigns content to a div and does an update. It's fast, much easier to control than the old frames were, and can be done asynchronously (via AJAX) However, it's also a lot more work to deal with than the old target= functionality. Clients love it, but it does take time to code it. Significant time, in fact, if you're dealing with a ton of pages and lots of content.
Another option would be to use a templating system like Zend Framework to build your views into a unified template for display. But, it would add the overhead of a framework which can be sizable.
Finally, since you have PHP at your disposal, you could just pull all the content from the DB and essentially run the entire site on one "page". The URL string would contain a parameter that tells it what content to pull from the DB and replace in your generic "getter" page. A true CMS system of sorts.
Good luck.
选项 A 减少了文件数量,但随着您的网站设计变得越来越复杂,您可能需要查看 Smarty< /a>,因为这将页面的“视图”与“数据”完全分开。另外,它非常强大并且是作为一个框架而建立的。
Option A reduces your number of files, though as you're getting more and more complex in your site design, you may want to look at Smarty, as that completely separates the "view" from the "data" of the page. Plus it's very robust and established as a framework.
您可以使用 phps __autoload()。这听起来有点脏,但是类包装了您的内容,然后在每个文件的末尾有一个访问器。
每个文件看起来都是这样的:
You could use phps __autoload(). It sounds kind of dirty, but but class wrappers around your content, then a single accessor at the end of each file.
Each file would look something like: