Welcome Guest, Not a member yet? Register   Sign In
Displaying a 404 if wrong number of segments passed to controller function?
#1

[eluser]Unknown[/eluser]
Hello,

I have a question about handling unexpected URL segments and showing errors.

For example: given a URL that resembles /controller_class/function/variable, I was expecting CodeIgniter to display a 404 if the function does not except a parameter. Or, more generally, I would expect that if the number of URL segments is greater than the number of parameters for the given function, that the URL is not valid and should display an error to the user.

Since no error is given, is there a way to check for such a condition that doesn't involve checking the parameters on a per-function basis? That could get repetitive. I haven't worked with PHP5 enough to know about the Reflection APIs but my understanding is that there's a way to check on the number of parameters of a Class' method...

... or perhaps I've just overlooked something obvious. Any insight would be appreciated. Thanks!
#2

[eluser]toopay[/eluser]
Configure a Hook?
#3

[eluser]theprodigy[/eluser]
using a _remap function can help in this situation.
Code:
public function _remap($method, $params = array())
    {
        $the_method = new ReflectionMethod($this, $method);
        $num = $the_method->getNumberOfParameters();
//        echo '<pre>'.print_r($the_method,true).'</pre>';
//        echo '<pre>'.print_r($num,true).'</pre>';
        if(count($params) > $num)
        {
            //they passed in too many
            //show your custom error, show_404(), whatever.
        }
        else
        {
            //they did not pass in too many
            call_user_func_array(array($this, $method), $params);
        }
    }
#4

[eluser]John_Betong_002[/eluser]
>>> ... or perhaps I’ve just overlooked something obvious. Any insight would be appreciated. Thanks!

http://ellislab.com/codeigniter/user-gui...uting.html

First try routing all your anticipated URLs.

Followed by a catchall:
Code:
// default route
  // anticipated routes

  // finally the Daddy of them all
  $route["(:any)"] = "c_error/index/$1";
&nbsp;
#5

[eluser]Unknown[/eluser]
[quote author="theprodigy" date="1306474024"]using a _remap function can help in this situation.
Code:
public function _remap($method, $params = array())
    {
        $the_method = new ReflectionMethod($this, $method);
        $num = $the_method->getNumberOfParameters();
//        echo '<pre>'.print_r($the_method,true).'</pre>';
//        echo '<pre>'.print_r($num,true).'</pre>';
        if(count($params) > $num)
        {
            //they passed in too many
            //show your custom error, show_404(), whatever.
        }
        else
        {
            //they did not pass in too many
            call_user_func_array(array($this, $method), $params);
        }
    }
[/quote]


Just what I was looking for. Awesome.




Theme © iAndrew 2016 - Forum software by © MyBB