Welcome Guest, Not a member yet? Register   Sign In
URI - Passing text
#5

[eluser]Michael Wales[/eluser]
To make your URLs appear like: domain.com/index.php/game/value you will either have to use the _remap() function or URI Routing. In this case, since this is the only controller where you will be performing this sort of action - I recommend going the _remap() route. Using _remap() will also save you some frustration as it won't interfere with your URL helper (thus, producing correct returns from the anchor() function).

When using _remap(), the requested method will be passed to the _remap() method, where you will use a conditional to determine what actually needs to happen.

Let's just say our controller is user, for these examples.


Here's a basic outline of what a remapped controller will look like:
Code:
class User extends Controller {

function _remap($method) {
  if ($method == 'view') {
    $this->view();
  } elseif ($method = 'edit') {
    $this->edit();
  }
}

function edit() {
  // Do some stuff
}

function view() {
  // Do some stuff
}


}
}

We're going to take advantage of the if..else statement. What you are attempting to do is pass a value, in the method segment of the URL, so we need to remap any method requests that aren't predefined. Using the example above, let's say view and edit are the only two methods within our controller - we know that, and we've defined it in our _remap() function.

Now, we want to make the URLs look like: index.php/user/username - which means we need to utilize the ..else statement within our _remap() function.

Code:
class User extends Controller {

function _remap($method) {
  if ($method == 'view') {
    $this->view();
  } elseif ($method = 'edit') {
    $this->edit();
  } else {
    $this->profile();
  }
}

function edit() {
  // Do some stuff
}

// If a pre-established method has not been defined
// assume they are passing a username to view a profile
function profile() {
  if ($this->uri->segment(2)) {
    $username = $this->uri->segment(2);
  } else {
     show_error('No username was passed');
  }
  // Do all of our stuff to retrieve the profile and display it to the user
}

function view() {
  // Do some stuff
}


}
}

One of the downfalls of the _remap() method is that you can not pass information easily from _remap() to the method. For example, when I remapped the ..else statement to the profile() method, I can't just pass segment(2) to the profile method - I have to request segment(2) from within the profile() method.

Hope this helps you out some - and good luck!


Messages In This Thread
URI - Passing text - by El Forum - 08-22-2007, 06:05 AM
URI - Passing text - by El Forum - 08-22-2007, 06:16 AM
URI - Passing text - by El Forum - 08-22-2007, 06:58 AM
URI - Passing text - by El Forum - 08-22-2007, 07:35 AM
URI - Passing text - by El Forum - 08-22-2007, 07:45 AM
URI - Passing text - by El Forum - 08-22-2007, 08:36 AM
URI - Passing text - by El Forum - 08-22-2007, 08:38 AM
URI - Passing text - by El Forum - 08-22-2007, 08:47 AM



Theme © iAndrew 2016 - Forum software by © MyBB