模拟文件结构与PHP [英] Simulate file structure with PHP

查看:238
本文介绍了模拟文件结构与PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个共享的Apache Web服务器上的PHP。我可以编辑.htaccess文件。

I'm running PHP on a shared Apache web server. I can edit the .htaccess file.

我试图模拟一个文件,文件结构,是不是真正存在。例如,我想的网址: www.Stackoverflow.com/jimwiggly 实际显示 www.StackOverflow.com/index.php?name = jimwiggly 我有一半通过编辑我的.htaccess文件按在这篇文章中说明:<一href="http://stackoverflow.com/questions/1987743/php-serve-pages-without-php-files-in-file-structure">http://stackoverflow.com/questions/1987743/php-serve-pages-without-php-files-in-file-structure:

I'm trying to simulate a file file structure that is not actually there. For example, I would like for the URL: www.Stackoverflow.com/jimwiggly to actually display www.StackOverflow.com/index.php?name=jimwiggly I got halfway there by editing my .htaccess file as per the instructions in this post: http://stackoverflow.com/questions/1987743/php-serve-pages-without-php-files-in-file-structure:

RewriteEngine on
RewriteRule ^jimwiggly$ index.php?name=jimwiggly

这很好地工作,只要在地址栏仍显示 www.Stackoverflow.com/jimwiggly 和正确的页面加载,但是,我所有的相对链接保持不变。我能回到过去,并插入&LT;?PHP的echo $ _ GET ['名称'];?&GT; 的每一个环节之前,但似乎有可能是一个更好的办法比起那个来说。此外,我怀疑我的整个方法可能会关闭,我应该会对此有所不同?

This works nicely insofar as the URL bar still displays www.Stackoverflow.com/jimwiggly and the correct page loads, however, all of my relative links remain unchanged. I could go back in and insert <?php echo $_GET['name'];?> before each link, but it seems like there might be a better way than that. Additionally, I suspect my whole approach might be off, should I be going about this differently?

推荐答案

我觉得要做到这一点的最好办法是采用MVC样式的URL操作的URI,而不是PARAMS。

I think the best way to do this is to adopt the MVC style url manipulation with the URI and not the params.

在喜欢你htaccess的使用:

In your htaccess use like:

<IfModule mod_rewrite.c>
    RewriteEngine On
    #Rewrite the URI if there is no file or folder
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

然后在你的PHP脚本,你想开发一个小班读取URI,并把它分割成段,如

Then in your PHP Script you want to develop a small class to read the URI and split it into segments such as

class URI
{
   var $uri;
   var $segments = array();

   function __construct()
   {
      $this->uri = $_SERVER['REQUEST_URI'];
      $this->segments = explode('/',$this->uri);
   }

   function getSegment($id,$default = false)
   {
      $id = (int)($id - 1); //if you type 1 then it needs to be 0 as arrays are zerobased
      return isset($this->segments[$id]) ? $this->segments[$id] : $default;
   }
}

使用像

http://mysite.com/posts/ 22 /罗伯特 - 皮特 - 显示-MVC式-URI访问

$Uri = new URI();

echo $Uri->getSegment(1); //Would return 'posts'
echo $Uri->getSegment(2); //Would return '22';
echo $Uri->getSegment(3); //Would return 'robert-pitt-shows-mvc-style-uri-access'
echo $Uri->getSegment(4); //Would return a boolean of false
echo $Uri->getSegment(5,'fallback if not set'); //Would return 'fallback if not set'

现在在MVC中通常有像 http://site.com/controller/method/param但在非MVC风格的应用程序,你可以做 http://site.com/action/sub-动作/参数

Now in MVC There usually like http://site.com/controller/method/param but in a non MVC Style application you can do http://site.com/action/sub-action/param

希望这有助于你期待与您的应用程序移动。

Hope this helps you move forward with your application.

这篇关于模拟文件结构与PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆