简体   繁体   English

如何制作codeigniter样式URI段

[英]how to make codeigniter style URI segments

I was wondering how one could make codeigniter style url segments on a project. 我想知道如何在项目上制作codeigniter样式的url段。

Aside from an htaccess rule to capture the segments, how can this be done in PHP? 除了捕获片段的htaccess规则之外,如何在PHP中完成此操作?

After snooping around the codeigniter source code, i could not find any reference to an htaccess file that captures the usage. 窥探codeigniter源代码后,我找不到对捕获用法的htaccess文件的任何引用。

Any idea on how this can be done? 关于如何做到这一点的任何想法?

Assuming you are passing ALL requests to index.php via Apache mod_rewrite or Nginx: 假设您正在通过Apache mod_rewrite或Nginx将所有请求传递给index.php:

// Get the current URL path (only) and convert encoded characters back to unicode
$path = rawurldecode(trim(parse_url(getenv('REQUEST_URI'), PHP_URL_PATH), '/')));

// array merge with defaults
$segments = explode('/', $path) + array('home', 'index');

//First two segments are controller/method
$controller = array_shift($segments);
$method = array_shift($segments);

// @todo serious security checking here!

// Finally, load and call
$controller = new $controller;
$controller->$method($segments);

Your controller would look like this 您的控制器看起来像这样

class Home
{
    public function index($params)
    {
        print_r($params);
    }
}

What you do is set up one single url param in htaccess, and then use a string splitting method to retrieve a model, controller, and view from that string which will then call a model class, a controller class, and then render the data into a view. 您要做的是在htaccess中设置一个网址参数,然后使用字符串拆分方法从该字符串中检索模型,控制器和视图,然后调用模型类,控制器类,然后将数据呈现到一个看法。 the transformation would be something like the following: 转换将类似于以下内容:

mysite.com/index.php?url=/tasks/all => mysite.com/tasks/all which calls the task model, which then calls the function called " all() " inside of the tasks controller. mysite.com/index.php?url=/tasks/all => mysite.com/tasks/all会调用任务模型,然后再在任务控制器内部调用名为“ all() ”的函数。

As soon as this site goes back online, do look at the htaccess portion of the tutorial -- he did a good job of showing how its done http://www.henriquebarroso.com/how-to-create-a-simple-mvc-framework-in-php/ 该站点重新上线后,请务必查看教程的htaccess部分-他在展示其完成工作方面做得很好, http://www.henriquebarroso.com/how-to-create-a-simple- mvc-framework-in-php /

You still need something to "forward" all virtual requests to a physical file. 您仍然需要一些东西来将所有虚拟请求“转发”到物理文件。

The idea is that any URI that doesn't match a physical file or folder on disk is rewritten (usually through mod_rewrite) to your index.php file (it's usually your index file so a direct call to the index works too), and it appends the URI to the path or as a query string parameter: 这个想法是,将任何与磁盘上的物理文件或文件夹都不匹配的URI重写(通常通过mod_rewrite)到index.php文件(通常是您的索引文件,因此直接调用索引也可以),并且将URI附加到路径或作为查询字符串参数:

RewriteCond %{REQUEST_FILENAME} !-f  # Not a real file
RewriteCond %{REQUEST_FILENAME} !-d  # Not a real folder
RewriteRule ^(.*)$ index.php/$1 [L]  # Rewrite to index.php, with a leading slash, followed by the URI

Alternatively, you can use a standard error document handler (still in .htaccess or apache config, but no need for mod_rewrite!): 另外,您可以使用标准的错误文档处理程序(仍在.htaccess或apache配置中,但不需要mod_rewrite!):

<IfModule !mod_rewrite.c>
    ErrorDocument 404 /index.php
</IfModule>

Now that control is passed to uniformly to an index.php file, you need a router mechanism to match the route to the correct controller. 现在,将控制统一传递到index.php文件,您需要一个路由器机制来将路由匹配到正确的控制器。 Ideally, you'd have a list of static and dynamic routes. 理想情况下,您将具有静态和动态路由列表。 Static routes are direct matches to the URI, and dynamic would be regular expression matches. 静态路由是与URI的直接匹配,而动态路由是正则表达式的匹配。 Check your static routes first, as it's as simple as a single hash lookup, while you'll have to loop through all the dynamic routes. 首先,请检查您的静态路由,因为它就像单个哈希查找一样简单,同时您必须遍历所有动态路由。

For performance, it's nice to put your more common dynamic routes at the beginning of the list, and the obscure ones at the end. 为了提高性能,最好将更常见的动态路线放在列表的开头,而晦涩的路线则放在列表的结尾。

Using the .htaccess file to employ mod_rewrite to rewrite the base of the url is essential to this. 使用.htaccess文件使用mod_rewrite重写url的基础对此至关重要。 Otherwise, the browser will treat each segment as a supposed folder or file (depending on whether or not you have a trailing /) 否则,浏览器会将每个段视为一个假定的文件夹或文件(取决于您是否有尾随/)

once you have that, however, you can simply use the method described in this post: 但是,一旦有了这些,就可以简单地使用本文中描述的方法:

how do i parse url php 我如何解析URL PHP

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

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