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

[eluser]Kemik[/eluser]
Hello,

Is it possible to pass text via the URI?

E.g.

http://www.domain.com/index.php/game/comps/r6v

game = controller
comps = function
r6v = text trying to pass

I've been able to pass numeric values but whenever I try text I get the variable = 0.

E.g.
Code:
$game_id = $this->uri->segment(3, 0);
        
if ($game_id == 0) {
   show_error('No Game ID passed');
}
        
echo $game_id;

It always says No Game ID passed. If I remove that show_error I still get 0.

At first I thought it was just because I had put it inside the index() function with segment(2, 0) but then when I moved it to it's own function I still get 0.
#2

[eluser]Michael Wales[/eluser]
Odd... try putting a trailing slash - does it work then?

Is the text you are trying to pass 0 (index.php/games/comps/0)?

Try removing your default override (the 0) and let it use FALSE if it fails, does it work then?
Code:
$game_id = $this->uri->segment(3);
if ($game_id === FALSE) {
  show_error('Damn');
}
echo $game_id;

Are you performing any URI routing?

Is that the only code you have in your controller? It should be working...
#3

[eluser]Kemik[/eluser]
Nope, I'm trying to pass r6v. I've seen the problem elsewhere in my application too. I just changed true to 1 in the URI. That's pretty much the only code (it's the code at the start of the controller) and I'm doing no routing. I'm using rapyd but that shouldn't make any difference as it doesn't have any uri features.

Ok. I changed 0 to FALSE and it now works. I'm guessing I shouldn't have to do that though.

Btw, is it possible to put that code in the index() function so I only have to put domain.com/index.php/game/value ?

I've tried it with that code and segement(2) but I get a 404.

The reason I ask is because it's the only function I need in that controller.
#4

[eluser]Michael Wales[/eluser]
Hmm... well, the issue definitely rests with this statement:
Code:
if ($game_id == 0) {

I honestly can't tell you why, but - if you want to keep returning 0 as the fault identifier - change the conditional to:
Code:
if (!$game_id) {

For some reason PHP is saying "Yes, $game_id is equivalent to 0, even though I have r6v stored in there." To be honest, I would leave the segment() method alone and let it return FALSE as it does by default, then use the following conditional, which will perform a strict comparison and only fail if $game_id truly equals FALSE (not 0, not a blank string, only FALSE). By using these literal, strict, comparisons you will save yourself a lot of heartache in the future:
Code:
if ($game_id === FALSE) {

I'm going to address your other question in another reply - as it is a completely different topic and will be a bit lengthy.
#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!
#6

[eluser]alpar[/eluser]
i thin that the source of that problem above is that any string like 'r6v' for example will evaluate to 0 if you force a type. Here's an example:

$x = 'r6v';
$y = '6rv';
$z = '66';

(int) $x will be 0 , (int) $y will be 6, and (int) $z will be 66

in your case

$game_id = $this->uri->segment(3, 0);

if ($game_id == 0) {
show_error('No Game ID passed');
}


($game_id == 0) means that $game_id is compared to int 0, so it is automatically converted to an integer, since it starts with a letter, that is automatically a 0. if you want something similar to the above, use strings instead, use:

if ($game_id == '0') {

this way $game_id isn't converted to an integer.

Since PHP is loosely typed you really have to be careful with types, the fact that you don't declare variables with a strict type doesn't mean, that you will have nothing to do with types in your code.
#7

[eluser]Michael Wales[/eluser]
You hit the nail on the head there alpar - that's exactly the reason. I looked at that code for 15 minutes and failed to notice the 0 was not surrounded by quotes.

Yet another reason I prefer to use booleans. In this code you are expecting to receive a string, but if something messes up, you are returning an integer - it can get confusing and lead to some issues.

The strict boolean comparisons available with === and !== are extremely valuable in these circumstances.
#8

[eluser]Kemik[/eluser]
Thanks for the info guys. I'll adapt all my current files to use the FALSE boolean instead of 0.




Theme © iAndrew 2016 - Forum software by © MyBB