MVC 框架中的 URL 路由 - PHP
我正在从头开始用 PHP 开发一个 MVC 框架;主要是为了学习经验,但这很容易最终形成一个实时项目。我以这个 教程 作为基础,我已经从那里扩展。
请求是这样发出的:
examplesite.com/controller/action/param1/param2/等等...
这是我的.htaccess文件:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?rt=$1 [L,QSA]
所以所有请求都转到index.php,它们被路由到正确的控制器和操作从那里。如果未给出控制器或操作,则两者均采用默认“索引”。
我有一个带有索引操作的索引控制器,它应该是我网站的主页。我可以通过访问 examplesite.com 来访问它(因为假定索引部分)。它有一些图像、样式表的链接和一些脚本。它们与相对于index.php 的路径链接。我认为这很好,因为所有请求都转到index.php,并且所有内容都使用 php 简单地包含在该页面中。如果我访问 examplesite.com,这就会起作用。我将看到所有图像和样式,并且脚本将运行。 但是,如果我访问 examplesite.com/index,我会被路由到该网站的正确部分,但所有链接都不起作用。浏览器是否认为我位于不同的文件夹中?
我希望能够对网站中的所有内容使用相对路径,因为否则我需要在各处使用绝对路径以确保内容能够显示出来。这可能吗?
I'm developing an MVC framework in PHP from scratch; mostly for the learning experience but this could easily end up in a live project. I went through this tutorial as a base and I've expanded from there.
Requests are made like this:
examplesite.com/controller/action/param1/param2/ and so on...
And this is my .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?rt=$1 [L,QSA]
So all requests go to index.php and they are routed to the correct controller and action from there. If no controller or action is given, then the default 'index' is assumed for both.
I have an index controller with an index action, which is supposed to be the home page of my site. I can access it by going to examplesite.com (since the index part is assumed). It has some images, a link to a stylesheet, and some scripts. They are linked with paths relative to index.php. I thought this would be fine since all request go to index.php and all content is simply included in this page using php. This works if I go to examplesite.com. I will see all of the images and styles, and scripts will run.
However, if I go to examplesite.com/index, I am routed to the correct part of the site, but all of the links don't work. Does the browser think I am in a different folder?
I would like to be able to use relative paths for all of the content in my site, because otherwise I need to use absolute paths everywhere to make sure things will show up. Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要绝对路径,因为浏览器会评估相对于当前请求 uri 的相对路径(除非您使用基本标记,请参见下文)。请注意,绝对路径并不一定意味着您需要在其中包含域名。只需从正斜杠开始就足够了。可以这么说,它是相对于服务器的绝对路径。
所以:
而不是
.. .会工作得很好。
正如我所提到的,另一种方法是使用 基本标记。但由于我现在不记得的原因,我相信建议不要使用它。 (如果还有人想插话的话……)
You need an absolute path, because browsers evaluate the relative paths relative to the current request uri (unless you use the base tag, see below). Mind you though that an absolute path doesn't necesseraly mean you need to include the domainname in it. Simply starting with a forward slash is enough. It's the absolute path relative to the server, so to speak.
So:
<img src="/images/bla.gif">
in stead of
<img src="images/bla.gif">
...will work just fine.
As I've mentiond, another approach would be to use the base tag. But for reasons I can't remember right now, I believe it is recommended to not use it. (If anybody else wants to chime in here...)
IMO 图像、javascript、样式和其他静态内容不应该成为路由系统的一部分。这意味着您为每个请求加载 PHP(性能打击),而 Apache 可以很好地服务这些文件。
我的 .htaccess 总是让现有文件跳过 PHP:
IMO images, javascript, styles, and other static content should not be part of the routing system. It means you load PHP for every request (a performance whack), when Apache could serve those files just fine.
My .htaccess always lets existing files skip PHP: