Codeigniter路由器将URL段作为$ _GET查询传递 [英] Codeigniter router to pass url segment as $_GET query

查看:65
本文介绍了Codeigniter路由器将URL段作为$ _GET查询传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设置codeigniter进行路由:

How can I set codeigniter to route this:

/download/folder/file.ext

对此

/download?path=folder/file.ext

我知道如何使用.htaccess做到这一点,但是仅在CI中可以吗?我尝试过

I know how to do it using .htaccess , but is it possible in CI only? I tried

$route['download/(:any)'] = "download/index/?path=$1"; 

没有工作...
谢谢

didn't work ... thanks

推荐答案

我敦促您改变对CI Router类所做工作的看法(这对我来说,从重写方法转变为路由器的方法。)

I urge you to change your perspective of what the CI Router class does (this was the most difficult thing for me in moving from the "rewriting" approach to the "router" approach).

您的问题采用的是重写方法,以下是使用.htaccess文件处理的方法:

Your question assumes a "rewriting" approach, and here's how it could be handled with an .htaccess file:

RewriteRule ^/download/(.*)$ index.php/download/?path=$1 [L]

在这种方法下,您将有一条仅匹配 / download部分的路径,然后将使用 $ this-> input-> get('path')在控制器动作中。

Under this approach, you'll have a route to match just the "/download" part, and then you'll use $this->input->get('path') in the controller action.

路由方法保留。 htaccess文件相同,并更改$ routes配置以及控制器如何获取该信息,如果您愿意的话:

The "routing" approach keeps the .htaccess file the same, and changes your $routes configuration, as well as how the controller "gets" that information, if you will:

<?php
// config/routes.php change:
$routes['download/(:any)'] = 'download/index/$1';

// controllers/download.php:
class Download extends CI_Controller {
    public function index($folder, $file)
    {
        // if the folder is "flat" (i.e. no subfolders),
        // you can simply use $folder and $file
        var_dump($folder);
        var_dump($file);

        // otherwise, $file is the last segment ("n"),
        // and folder is segments 2 through n
        $segments = $this->uri->segment_array();
        $folder = implode('/', array_slice($segments, 2, -1);
        $file   = end($segments);

        // full path is always $folder and $file appended
        $path = $folder.'/'.$file;
        var_dump($path);
    }
}

这是一种方法,因此请根据您的特定要求进行调整。

This is the approach, so adjust as needed for your specific requirements.

干杯!

这篇关于Codeigniter路由器将URL段作为$ _GET查询传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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