Welcome Guest, Not a member yet? Register   Sign In
Mapping the URL to method parameters
#1

[eluser]Unknown[/eluser]
Hi All,

I just discovered CodeIgniter today, so you can imagine my giddiness. I am having trouble with one feature in particular and I would appreciate if someone could point me in the right direction.

I have noticed that after specifying a correct controller/function, I can append anything and everything to the end of the URL. I am hoping there is a solution that will return a show_404 (or similar) if the number of arguments in the URL does not match the number of arguments on the method. This may not be possible with PHP5, but if it is, it seems like a viable solution to me.

Thanks in advance,
Matt
#2

[eluser]Yash[/eluser]
404 error occurs automatically when function not found in controller. but if segments are missing in function argument then it is a warning and error by default.

controller/function/seg1/seg2

error for first 2 segments i.e. controller and function

warning for left 2 (or more) arguments.
#3

[eluser]Colin Williams[/eluser]
'browse' below is a controller method

Code:
function browse($seg_1 = NULL, $seg_2 = NULL)
{
  $required_arg_count = 2; // Does not include controller/function
  if (count($this->uri->segment_array()) - 2 < $required_arg_count)
  {
    show_404();
    return; // Just in case...
  }

  // Now go on with the function

}
#4

[eluser]Leonard Yulianus[/eluser]
[quote author="Colin Williams" date="1227107056"]'browse' below is a controller method

Code:
function browse($seg_1 = NULL, $seg_2 = NULL)
{
  $required_arg_count = 2; // Does not include controller/function
  if (count($this->uri->segment_array()) - 2 < $required_arg_count)
  {
    show_404();
    return; // Just in case...
  }

  // Now go on with the function

}
[/quote]

haven't tested it but i think this solution gonna works...

maybe you could just change the < into !=, to suit your condition better
#5

[eluser]Chris Newton[/eluser]
Cool idea collin.
#6

[eluser]sophistry[/eluser]
_remap() would be a nice generalized way to do this. see http://ellislab.com/forums/viewthread/87496/ for two uses for _remap (one sent by Colin, one by me) :-)




Theme © iAndrew 2016 - Forum software by © MyBB