Welcome Guest, Not a member yet? Register   Sign In
Divide URI segment into 2 parts through routes.php
#1

[eluser]danishravian[/eluser]
Hi,

Is it possible to break the one URI segment into two parts.

For example if my URL is:

Code:
www.example.com/aaa-xxx

I want it to route to:

aaa controller and xxx as parameter. xxx is variable and it can contain one or more dashes too. Link could be like:
Code:
www.example.com/aaa-x-z-y-a

I tried this in routes.php

Code:
$route['aaa-(:any)'] = "aaa/$1";

It goes to aaa controller, but it is not passing any variable.
Please let me know how can I do this?

Thanks
#2

[eluser]louisl[/eluser]
You can to do that in the controller, grab $this->uri->segment(1) then break it up as you need, also take a look at the CodeIgniter's _remap() function.
#3

[eluser]danishravian[/eluser]
I am familiar with remap, but problem with remap is that it will go to default controller. But i want to send it to some other controller.
#4

[eluser]louisl[/eluser]
Please explain what you are trying to achieve, what are you expecting to happen with the variables changing in the urls?
#5

[eluser]danishravian[/eluser]
aaa will be my controller...
and second part is a variable, which I will search in database and on the basis of that variable, I will display the results ..
#6

[eluser]louisl[/eluser]
Is there a reason you cant do www.example.com/aaa/x-y-z ?

Do you have other controllers or is the intention to have one controller for the whole site? So urls could be www.example.com/x-y-z, www.example.com/a-b-c etc?

#7

[eluser]danishravian[/eluser]
There are other controllers as well and i have to use dash for seo purpose and its client's requirement
#8

[eluser]CroNiX[/eluser]
Code:
//pass it to a method in your controller, not just the controller (unless using _remap())
$route['aaa-(:any)'] = "aaa/process/$1";

class aaa extends CI_Controller
{
  //receive parameter 1 passed from the route into the process() method
  //(with a default of empty string so no error going to http://yoursite.com/aaa))
  function process($parameter1 = '')
  {
    if($parameter1 == '')
    {
        $parameter1 = 'Not Set';
    }
    echo 'parameter1: ' . $parameter1;
  }
}
#9

[eluser]louisl[/eluser]
Nice solution CroNIX, I was going to suggest this:-
Code:
//$route['aaa-(:any)'] = "aaa/$1";
//$route['aaa-'] = "aaa";

class aaa extends CI_Controller {
  
  function _remap($method) {
  
   echo $method;
      
  }

}
#10

[eluser]danishravian[/eluser]
there is some modification in the structure.

actually whenever url will be something like aaa-x-y-z, then I am sending it to a controller and passing aaa and x-y-z as two parameters.

Code:
class bbb extends CI_Controller {
  
  function _remap($method) {
   $method; // should contain aaa
   $uri2 = $this->uri->segment(2); // should contain x-y-z
      
  }

}
But in this case $mothod contains aaa-x-y-z

and I am using this in route.php file
Code:
$route['aaa-(:any)'] = "bbb/aaa/$1";

So the problem here is, uri segment 2 is empty and uri segment 1 contains both the parameters. But uri 1 should contain aaa and uri 2 should have x-y-z. How can I do this?




Theme © iAndrew 2016 - Forum software by © MyBB