PHP:在文件结构中提供没有 .php 文件的页面 [英] PHP: Serve pages without .php files in file structure
问题描述
我正在为客户构建内部 CMS.不是为每个页面创建一个新的 php 文件,我想知道是否有一种方法可以根据 URL 加载页面,而不是该位置的物理 php 文件.
I am working on building an internal CMS for clients. Instead of creating a new php file for each page, I am wondering if there is a way to load up a page based on the URL but not a physical php file in that location.
因此,如果我访问 www.mysite.com/new-page,我希望这只是对我的模板、内容等的引用,而不是实际的 .php 文件.
So, if I visit www.mysite.com/new-page I would like this to just be a reference to my template, content, etc. rather than an actual .php file.
抱歉,如果我没有正确解释这一点,但我很难解释.
Sorry if I have not explained this correctly, but I am having a hard time explaining it.
谢谢.
推荐答案
听起来您需要 Front Controller模式.
基本上每个 URL 都会被重定向到一个 PHP 页面,该页面决定如何处理它.您可以使用 Apache mod_rewrite 使用此 .htaccess:>
Basically every URL gets redirected to one PHP page that determines what to do with it. You can use Apache mod_rewrite for this with this .htaccess:
RewriteEngine on
RewriteBase /
RewriteRule !\.(js|ico|txt|gif|jpg|png|css)$ index.php
这会将除静态内容文件之外的所有内容重定向到 index.php.根据需要进行调整.
That redirects everything except static content files to index.php. Adjust as required.
如果您只想影响 URL/new-page,请尝试以下操作:
If you just want to affect the URL /new-page then try something like:
RewriteEngine on
RewriteBase /
RewriteRule ^new-page/ myhandler.php
任何以new-page"开头的 URL 都将被发送到 myhandler.php.
Any URLs starting with "new-page" will be sent to myhandler.php.
这篇关于PHP:在文件结构中提供没有 .php 文件的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!