daulat daulat - 7 months ago 22
PHP Question

unlimited parameters get last one

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"


  1. if url is www.example.com/uk-en/category I want "category"

  2. 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]

Answer

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());