0

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]
daulat
  • 969
  • 11
  • 24

2 Answers2

1

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());
Yaseen Ahmad
  • 1,807
  • 5
  • 25
  • 43
0

Problem in your routes is with setting wildcard placeholder on first place. It always need to be at last place.

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/$1';

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

Docs.

Tpojka
  • 6,996
  • 2
  • 29
  • 39