无限参数获取最后一个

unlimited parameters get last one

我正在使用 codeigniter。我怎样才能得到我的 URL 的最后一部分,例如:

www.example.com/uk-en/category/subcategory

参数可以没有限制也可以只有一个。在我的控制器主页和方法索引中,我只想要 url 的最后一部分,例如 "subcategory"

  1. 如果 url 是 www.example.com/uk-en/category 我想要 "category"
  2. 如果url是www.example。com/uk-en我想要"uk-en"

编辑

如果 url 是 www.example.com/Home/index/uk-en/category 那么它正在工作
但是我想要的是没有 class 名称 "Home" 和方法 "index"

像这样www.example.com/uk-en/category

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller{
    public function index($params=[]){
      $params=func_get_args();
     $last=end($params);
     echo $last;
    }
}
?>

routes.php

   <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    $route['(:any)'] = 'Home/index';
    $route['default_controller'] = 'Home';
    $route['404_override'] = 'error_404/index';
    $route['translate_uri_dashes'] = FALSE;
    ?>  

.htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

在你的路线中使用这个

$route['(.*)'] = "Home/index";

这将在您的索引函数中打印控制器中的所有路由

print_r($this->uri->segment_array());

您的路线中的问题在于将通配符占位符设置在第一位。它总是需要放在最后一个地方。

Routes will run in the order they are defined. Higher routes will always take precedence over lower ones.

$route['default_controller'] = 'Home';
$route['404_override'] = 'error_404/index';
$route['translate_uri_dashes'] = FALSE;

//some example routes
$route['specific/route'] = 'controller/method';
$route['specific/(:num)'] = 'page/show/';

//always put your wildcard route at the end of routes.php file
$route['(:any)'] = 'home/index';

Docs.