无限参数获取最后一个 [英] unlimited parameters get last one
问题描述
我使用codeigniter。如何获取网址的最后一部分,例如:
www.example.com/uk-en/category/subcategory
参数可以是无限的,也可以只有一个。在我的Controller Home和方法Index的里面,我只想要url的最后一部分,例如subcategory
- 如果url是www.example.com / uk-en / category我想要category
- 如果url是www.example.com/uk-en我想要uk-en / ol>
编辑
如果url是www.example.com/首页/ index / uk-en / category然后它工作
但是我想要没有类名Home和方法index
Like this 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('不允许直接脚本访问);
$ route ['(:any)'] ='Home / index';
$ route ['default_controller'] ='家';
$ route ['404_override'] ='error_404 / index';
$ route ['translate_uri_dashes'] = FALSE;
?>
.htaccess
RewriteEngine on
RewriteCond%{REQUEST_FILENAME}!-f
RewriteRule ^ index.php [QSA,L]
解决方案在您的路径中使用
$ route ['(。*)'] =首页/索引;
这将在索引函数中打印控制器中的所有路径
print_r($ this-> uri-> segment_array());
I'm using codeigniter. How can I get last part of my URL like:
www.example.com/uk-en/category/subcategory
Parameters may be unlimited or there may be only one. Inside of my Controller Home and method Index, I just want last part of url e.g "subcategory"
- if url is www.example.com/uk-en/category I want "category"
- if url is www.example.com/uk-en I want "uk-en"
Edit
if url is www.example.com/Home/index/uk-en/category then it's working
but What i want without class name "Home" and method "index"Like this 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]
解决方案Use this in your routs
$route['(.*)'] = "Home/index";
This to print all routs in your controller in your index function
print_r($this->uri->segment_array());
这篇关于无限参数获取最后一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!