简体   繁体   English

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

[英]Need php template help and clean url help

I'm new to PHP and web development in general, but have been a programmer for 5 years.我一般是 PHP 和 web 开发的新手,但已经做了 5 年的程序员。 I'm trying to work on my own website from scratch using Notepad to edit my PHP files and WAMP for my server.我正在尝试使用记事本从头开始在我自己的网站上工作,以编辑我的 PHP 文件和我的服务器的 WAMP。 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.我可以在 Safari、Chrome 和 Firefox 中查看我的文件,但不能在 IE 中查看(我们都知道 IE 不是最好的),因为我正在使用一些 Z6BFF62B10D884FB77428CFE168CD78 的东西。 Anyways, I have an Includes folder that holds my files for my header, menu and footer.无论如何,我有一个包含文件夹,其中包含我的 header、菜单和页脚的文件。 I have an index.php file that includes these files and displays them fine.我有一个 index.php 文件,其中包含这些文件并可以正常显示。 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 (eg Profile/index.php, Forums/index.php, etc.).为了尝试保持干净的网址,我创建了很多文件夹并将相同的 index.php 文件放在那里(例如 Profile/index.php、Forums/index.ZE1BFD762321E409CEE4AC0B6E84196 等)。 I did this so when I went to localhost/mysite/profile/ it showed me the template I wanted to use.我这样做了,所以当我转到 localhost/mysite/profile/ 时,它向我展示了我想使用的模板。 However, there has got to be a better way to use the template and a better way to have clean urls.但是,必须有更好的方法来使用模板和更好的方法来获得干净的 url。 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?我目前没有在任何地方托管这个网站,所以我不知道我是否可以访问 htaccess 文件(老实说,我什至不确定它是什么,只是看到它提到过),但我很好奇文件夹结构(每个菜单项一个文件夹)是正常还是可以的做法? 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.我也很好奇是否有一种方法可以使用 index.php 而不必每次进行小改动时都复制和粘贴它。 Any help would be appreciated.任何帮助,将不胜感激。 Thanks.谢谢。

If you are planning on templating I suggest using an existing platform like Symfony , Zend Framework, or Smarty .如果您打算使用模板,我建议使用现有平台,例如SymfonyZend FrameworkSmarty Smarty is probably the easiest to get going with and it is (almost) purely for templating. Smarty 可能是最容易使用的,而且它(几乎)纯粹是为了模板。 Whereas Symfony and Zend Framework are big packages with lots of bells and whistles.而 Symfony 和 Zend Framework 是大包,有很多花里胡哨。

If I was going to be doing pure templating I would look at Smarty.如果我要做纯模板,我会看 Smarty。 I use Zend Framework for just about all my current PHP projects now but it has a pretty steep learning curve.我现在几乎所有当前的 PHP 项目都使用 Zend Framework,但它的学习曲线相当陡峭。 Your first couple weeks will be frustrating.你的前几周会令人沮丧。

As far as URLs go, .htaccess is probably the preferred method (at least in my book).就 URL go 而言, .htaccess可能是首选方法(至少在我的书中)。 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 . 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.您不一定必须看到host/profile/index ,它可以是host/profilehost/profile/edit ,其中 edit 是正在执行的操作。

Copying the index.php file is not really the way I would go.复制 index.php 文件并不是我想要的 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如果您希望网址干净且搜索引擎友好,您应该真正看看url 使用 .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.你是说你不确定你是否会有一个“访问 htaccess 文件”的服务器,但是如果你可以上传文件,你总是可以上传 .htaccess 文件——问题是 web 服务器不是总是使用它们,或者可能没有启用 mod_rewrite。 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).在这种情况下,您可以获取格式为http://www.example.com/index.php?u=/Profile/foo的网址(这也是我处理 Z572D4E421E5E6B9BC11D815E8 的首选方式)。

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.在您的 index.php 中,只需确保读取 mod_rewrite 传递的请求的 url 参数,并在文件夹中包含您需要的任何文件。 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.我真的建议你 go 看看一个好的 PHP 框架,例如CodeIgniter 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.即使您决定从头开始编写所有代码,您仍然会学到很多关于 url 处理、数据库、MVC 模式、模板引擎、数据库抽象层和 PHP 编码的最佳实践。

your answer is the htaccess file, is converts the 'folder structure' to $_GET value's您的答案是 htaccess 文件,将“文件夹结构”转换为 $_GET 值

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如果您在 website.com/Profile/ 中,您可以编写一个 htaccess 行,将其转换为 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] 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM