需要 php 模板帮助和清理 url 帮助

发布于 2024-11-18 18:44:59 字数 594 浏览 1 评论 0原文

我对 PHP 和 Web 开发总体来说是新手,但已经成为一名程序员 5 年了。我正在尝试使用记事本从头开始开发自己的网站,为我的服务器编辑 PHP 文件和 WAMP。我可以在 Safari、Chrome 和 Firefox 中正常查看我的文件,但不能在 IE 中查看(我们都知道 IE 不是最好的),因为我正在使用一些 HTML5 内容。无论如何,我有一个 Includes 文件夹,用于保存页眉、菜单和页脚的文件。我有一个 index.php 文件,其中包含这些文件并可以正常显示它们。页面的中心是我想要的内容。为了尝试保持干净的网址,我创建了很多文件夹并将相同的index.php 文件放入其中(例如Profile/index.php、Forums/index.php 等)。我这样做了,所以当我访问 localhost/mysite/profile/ 时,它向我显示了我想要使用的模板。然而,必须有更好的方法来使用模板和更好的方法来获得干净的网址。我目前没有在任何地方托管这个网站,所以我不知道我是否可以访问 htaccess 文件(甚至不确定它是什么,老实说,只是看到它提到的),但我很好奇拥有文件夹结构(每个菜单项一个文件夹)是正常还是可以的做法?我也很好奇是否有一种方法可以使用index.php,而不必每次进行小更改时都复制并粘贴它。任何帮助将不胜感激。谢谢。

I'm new to PHP and web development in general, but have been a programmer for 5 years. I'm trying to work on my own website from scratch using Notepad to edit my PHP files and WAMP for my server. I'm able to view my files fine in Safari, Chrome and Firefox, but not IE (which we all know IE isn't the greatest) because I'm using some HTML5 stuff. Anyways, I have an Includes folder that holds my files for my header, menu and footer. I have an index.php file that includes these files and displays them fine. In the center of the page is where I want the content. To try and keep clean urls, I made quite a few folders and put this same index.php file in there (e.g. Profile/index.php, Forums/index.php, etc.). I did this so when I went to localhost/mysite/profile/ it showed me the template I wanted to use. However, there has got to be a better way to use the template and a better way to have clean urls. I'm not currently hosting this site anywhere so I don't know if I'll have access to the htaccess file (not even sure what it is honestly, just seen it mentioned), but I was curious of having the folder structure (one folder for each menu item) is a normal or ok practice? I was also curious if there is a way to use the index.php without having to copy and paste it every time I make a small change. Any help would be appreciated. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天冷不及心凉 2024-11-25 18:44:59

如果您计划使用模板,我建议使用现有平台,例如 SymfonyZend Framework,Smarty 。 Smarty 可能是最容易使用的,它(几乎)纯粹用于模板。而 Symfony 和 Zend Framework 是带有大量花哨功能的大软件包。

如果我要做纯模板,我会考虑 Smarty。我现在几乎所有当前的 PHP 项目都使用 Zend Framework,但它的学习曲线相当陡峭。你的前几周将会令人沮丧。

就 URL 而言,.htaccess 可能是首选方法(至少在我的书中)。 Zend Framework 和 Symfony 都有一种默认的 URL 编写风格,类似于 http://host/controller/action,其中控制器是 ProfileForums。您不一定必须看到 host/profile/index,它可能是 host/profilehost/profile/edit,其中 edit是正在执行的操作。

If you are planning on templating I suggest using an existing platform like Symfony, Zend Framework, or Smarty. Smarty is probably the easiest to get going with and it is (almost) purely for templating. Whereas Symfony and Zend Framework are big packages with lots of bells and whistles.

If I was going to be doing pure templating I would look at Smarty. I use Zend Framework for just about all my current PHP projects now but it has a pretty steep learning curve. Your first couple weeks will be frustrating.

As far as URLs go, .htaccess is probably the preferred method (at least in my book). Zend Framework and Symfony both have kind of default URL writing style that looks like http://host/controller/action where controller would be Profile or Forums. You wouldn't necessarily have to see host/profile/index, it could be host/profile or host/profile/edit, where edit is the action being performed.

复制index.php 文件并不是我真正想要的方式。看起来很混乱,还有一些其他选择。如果您希望网址干净且搜索引擎友好,您确实应该看看 使用 进行网址重写。 htaccess

你是说你不确定你是否有一个可以“访问 htaccess 文件”的服务器,但如果你可以上传文件,你也可以上传 .htaccess 文件 - 问题是网络服务器并不总是使用它们或者可能没有启用 mod_rewrite。在这种情况下,您可以以 http://www.example.com/index.php?u=/Profile/foo 的格式获取网址(这也是我处理网址重写的首选方法)。

在你的index.php中,只需确保读取mod_rewrite传递的请求的url参数,并包含你需要的文件夹中的任何文件。实际上,您甚至不需要“物理”文件夹结构,您可能想使用类或类似的东西来实现它。 (我确信我会的)。

我真的建议你去看看一个好的 PHP 框架,例如 CodeIgniter 。即使您决定从头开始编写所有代码,您仍然会学到很多有关 url 处理、数据库、MVC 模式、模板引擎、数据库抽象层和一般 PHP 编码的最佳实践的知识。

Copying the index.php file is not really the way I would go. It seems messy and there are a few other options. If you want the urls to be clean and search engine friendly you should really have a look at url rewriting using .htaccess

You're saying that you're not sure if you will have a server with "access to the htaccess file" but if you can upload files you can always upload a .htaccess file as well -- the problem is that the web server is not always using them or might not have mod_rewrite enabled. In this case you can get your urls on the format of http://www.example.com/index.php?u=/Profile/foo (this is also my preferred way to handle url rewrites).

In your index.php just make sure to read the requested url parameter passed by mod_rewrite and include whatever files in the folder that you need. Actually, you don't even need a "physical" folder structure, you might want to implement it using classes or something like that. (I'm sure I would).

I would really recommend that you go have a look at a good PHP framework, CodeIgniter for example. Even if you decide to code everything from scratch, you would still learn a lot about best practices for url handling, databases, the MVC pattern, template engines, database abstraction layers and PHP coding in general.

以为你会在 2024-11-25 18:44:59

你的答案是 htaccess 文件,它将“文件夹结构”转换为 $_GET 值,

例如,

如果你在 website.com/Profile/ 中,你可以编写一个 htaccess 行,将其转换为 website.com/index.php ?folder=配置文件

htaccess:

RewriteEngine on
RewriteRule ^/(.*)/$ index.php?folder=$1 [NC,L,QSA] 
RewriteRule ^/(.*).html$ index.php?folder=$1 [NC,L,QSA] 

your answer is the htaccess file, is converts the 'folder structure' to $_GET value's

for example,

if you're in website.com/Profile/ you can write an htaccess line that will convert that into website.com/index.php?folder=Profile

htaccess:

RewriteEngine on
RewriteRule ^/(.*)/$ index.php?folder=$1 [NC,L,QSA] 
RewriteRule ^/(.*).html$ index.php?folder=$1 [NC,L,QSA] 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文