codeigniter url segments containing leading slash

2019-08-10 11:19发布

the url to access the method is like this: http://localhost/site/cont/method I want to access this method using GET method like this: http://localhost/new-tera/paper/lookup_doi/segment but my segment part is already containing /like this:

http://localhost/lookup_doi/segment/containing/slashes note that the whole segment/containing/slashes is one value.

I am getting this value in my method like this:

public function method ($variable)
{
  echo $variable;
}

//output: segment

and not : segment/containing/slashes

4条回答
萌系小妹纸
2楼-- · 2019-08-10 11:57

CodeIgniter passes the rest of them as additional arguments. You can either specify the additional arguments (if the number is fixed) or use:

implode('/', func_get_args())

to get the entire string.

查看更多
ゆ 、 Hurt°
3楼-- · 2019-08-10 11:59

.htaccess mast be

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]

so ok...

$route = empty($_GET['route']) ? '' : $_GET['route'];
$exp = explode('/', $route);

results:

$exp[0] = new-tera
$exp[1] = paper
$exp[2] = lookup_doi
$exp[3] = segment

and so we mast be have routing! run example(with my projects):

 if($exp[0] == '')
        {
          $file = $_SERVER['DOCUMENT_ROOT'] .'/controllers/controller_index.php'; 
        }
        else
        {

          $file = $_SERVER['DOCUMENT_ROOT'] .'/controllers/controller_'.$exp[0].'.php'; 
        }    
        if(!file_exists($file))
        {
            engine :: away(); 
        }


        include_once $file;


        $class = (empty($exp[0]) or !class_exists($exp[0])) ? 'class_index' : $exp[0];


        $controller = new $class;

        $method = (empty($exp[1]) or !method_exists($controller, $exp[1])) ? 'index' : $exp[1];



        $controller -> $method();
查看更多
神经病院院长
4楼-- · 2019-08-10 11:59

You can base64 encode it first and decode it after. Really, you can use a variety of methods to change the / to something else (i.e. a -) and change it back.

echo site_url('controller/method/' . base64_encode($variable));

public function method ($variable) { $variable = base64_decode($variable); }

查看更多
甜甜的少女心
5楼-- · 2019-08-10 12:00

you can add slashes by adding "%2F" in your query string hope this will work

segment = 'somethingwith%2F'
http://localhost/new-tera/paper/lookup_doi/segment
查看更多
登录 后发表回答