制作一个没有框架的网站
我想制作我的主页,没有框架,我是否应该在index.php上拆分我的设计,使其成为header.php/footer.php,然后将它们包含在每个页面上?
I want to make my homepage, without frames, should i just split up my design on index.php so it is header.php/footer.php, and then just include them on every page?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我建议你使用一个框架。大多数框架(如果不是全部)都有简单的模板系统,因此您不必重复代码。
I suggest you use a framework. Most frameworks (if not all) have simple template systems, so you don't have to repeat code.
在网站的每个页面中包含内容的建议解决方案的问题在于,如果您想包含其他内容(例如侧边栏),则必须更新网站的所有页面。
更好的想法是根本不使用脚本-页面连接。因此,您不必为要显示的每个页面编写一个 php 文件。相反,使用一个前端控制器文件,大多数使用网站根目录中的index.php。然后使用 Apache mod_rewrite 或其他服务器技术来灵活地设置站点的 URL。然后让index.php映射不同的URL请求来服务不同的页面,然后你可以将站点的所有页面放入数据库或其他地方。
这样,您的网站中只有一个点包含页眉和页脚的模板,因此可以轻松更改,并且您可以使用网站的根目录来服务 AJAX 请求,在该请求中您不想输出 HTML,而是输出 JSON例如。
Afaik 这是一个很好的解决方法。
The problem with the suggested solution of including stuff in every page of your site is that you have to update all the pages of your site if you want to include another thing, say a sidebar.
A better idea is not to have a script--page connection at all. So you don't write a php file per page you want to show. Instead, use one front controller file, most use index.php in the root of the website. And then use Apache mod_rewrite or other server techniques to have flexibility in the URL's of your site. Then let index.php map different URL requests to serve different pages, you can then put all the pages of your site into a database or somewhere else.
This way there's only one point in your site that includes the templates for the header and footer, so that's easily changeable, and you can use the root of the site to serve AJAX requests, in which you won't want to output HTML but JSON for instance.
Afaik this is a good way of going about it.
另一种想法是只有一个入口点,使用
GET
参数调用,例如?site=about
。您的index.php
可能如下所示:因此,您只需包含
header.php
和footer.php
一次,即可完全控制内容允许什么,不允许什么(包含的文件可能位于只有 php 有权访问的目录中)。当您的index.php
处理请求时,home.php
、about.php
不必了解header.php< /code> 和
footer.php
(您可以在以后轻松地替换它们)。如果您不喜欢
http://www.example.com/?site=about
,您可以查看mod_rewrite
和朋友。Another idea would be to have just one single point of entry which is called with a
GET
parameter, e.g?site=about
. Yourindex.php
could look like this:Thus you only have to include
header.php
andfooter.php
once and have full control about what is allowed and what is not (the included files could be in a directory that only php has access to). While yourindex.php
handles the request,home.php
,about.php
do not have to know aboutheader.php
andfooter.php
(you could easily replace them at a later point in time).If you do not like
http://www.example.com/?site=about
, you can look intomod_rewrite
and friends.您可能想为此设置一个会话。只要访问者在您的网站上,会话变量就存在:
You may want to set a session for that. A session variable exists as long as a visitor is on your website:
是的,您可以将 index.php 拆分为 header.php/footer.php,然后将它们包含在每个页面上。
请注意,您的页面不能是静态 HTML,而是 php 脚本,以便用一个脚本显示多个页面。
我还建议不要使用像这样的常见结构,
而是另一种更有用的结构:
Yes, you can split your index.php into header.php/footer.php and then just include them on every page.
Note that your pages can be not static HTML but php scripts, to show multiple pages with one script.
I'd suggest also to have not a commonplace structure like
but another structure, much more useful: