Welcome Guest, Not a member yet? Register   Sign In
How to skip/index/ in uri-segment
#1

[eluser]mojitoo[/eluser]
Hey!

I want the the ability to share links like this: www.example.com/products/d32rcdsfskl, but like it is now I have to write www.example.com/products/index/d32rcdsfskl, how can I skip the index in the middle?

Code:
function index() {

  echo $this->uri->segment(2);

  if($this->uri->segment(2)) {

I want the segment to be 2 in the index controller but it has to be 3 because of the annoying index in the middle. Please help me to delete that one.
#2

[eluser]vrencianz[/eluser]
As I know you cannot skip it using CI only. There is a simple reason for that: that the controller can potentially contain other functions (products(), lists(), ...) and there is no way identifying the right one to be called when a request arrives.

If the controller contains only this function (index) you can try url_rewrite, but I don't recommend it.
#3

[eluser]CroNiX[/eluser]
@vrencianz, sure you can. If you want to change what the 2nd url is (by default, a specific method in the class), you need to use _remap() as covered in the user guide for the controller. http://ellislab.com/codeigniter/user-gui...#remapping

First, I'd use it (the "method", aka segment(2), passed to the _remap() method) to check if it is an existing method in the class. If it is execute that method, if not, process it how you wish. The example in the user guide does this, except where it does a show_404() you'd want to process it as a link (in OP's example)
#4

[eluser]mojitoo[/eluser]
Thank you CroNIX, it worked like a charm! Smile
#5

[eluser]vrencianz[/eluser]
I am glad that the problem is solved.

My problem with this solution is that if that other controller functions will inhibit parameters to the initial index().

For example:

Code:
class Test extends CI_Controller
{
/*
function index()
{
  echo $this->uri->segment(2);
  echo $this->uri->segment(3);
}
*/
function shoes()
{
  echo $this->uri->segment(2);
  echo $this->uri->segment(3);
}

function lists()
{
  echo $this->uri->segment(2);
  echo $this->uri->segment(3);
}

function _remap($method)
{
  echo 'remap<br/>';

  if($method === 'shoes')
  {
   $this->shoes();
  }
  else if($method === 'lists')
  {
   $this->lists();
  }
  else
  {
   echo 'processing: '.$method;
  }
}
}

1) let suppose that the initial class worked for <b>test/index/shoes</b>
2) with this remap <b>test/shoes</b> won't work anymore the same ($this->shoes() is called instead)

So, the developer must take care to avoid function names that match with all possible parameters to the initial index(). Because of, theoretically, any url is possible (user input from db, like id-s, names) this is not achievable.

I hope you understand my concerns. Smile

Cheers!
#6

[eluser]CroNiX[/eluser]
Take a look at the sample code (last example) in the docs that I posted a link for. It first checks to see if the method exists and if it does it sends the request to that method (along with any parameters that were passed in segments).

They also address your specific concern by prefixing all of their methods with "process_", as to avoid that issue, so if the method passed in from the url is "login", it will see if a method named "process_login" exists.
#7

[eluser]vrencianz[/eluser]
The second, and tricky, example can look complicated for most of users but explains entirely your solution.

Thx!




Theme © iAndrew 2016 - Forum software by © MyBB