Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Missing Arguments Controller Function Errors
  • As I continue making sites with fuel, I continually run into an issue of properly checking for errors if an argument on a controller function is not passed in the URL.  Example:

    public function action_getname($name) {}


    If I don't send that function "namehere", it gives me warning saying I "missing argument 1" and I have no way of redirecting or setting my own error via that controller function.  I'm wondering how the correct method of  validating and checking is done for this type of error.

    What I've been doing as of late is removing the arguments in the functions, and adding Uri::segement() to get the variable, then do error checking on that.  This feels really hackish and I know there is a better way.
  • public function action_getname($name = null)
    {
        if ($name === null)
        {
            // display your error message here and redirect somewhere..
        }
    }

    You can also define a route for it, so if the URI doesn't match the route, the request will not be routed to that controller. For me a less-optimal solution as you do logic checks in two places...
  • So the way you posted works, but I guess the real problem I was having is related to routing.  When I don't pass an argument on a routed function I get an error even with setting a variable default to null.  So I was wondering what are some other options I have, maybe adding inline routes?
  • I wonder what the error was then, and where it came from. Routes and method parameters have nothing to do with each other.

    Or was the problem that without the name segment, your method wasn't routed to?
  • This could be because I am routing it wrong, or that I am over looking something.
    My error message is:

    ErrorException [ Warning ]: trim() expects parameter 1 to be string, array given

    COREPATH/classes/uri.php @ line 243


    My code:
    public function action_test($name = null)

    My Route:
    'test/(:any)' => 'auth/test/$1',

    I've played around with different route settings and nothing seemed to fix it.

    If I do a simple route
    'test' => 'auth/test',

    Then the parameter never gets passed and I receive the same error, until I remove the parameter from the url and the controller loads normally but with no parameters.
  • HarroHarro
    Accepted Answer
    What version of Fuel are you using?

    With a route like "test/(:any)", the parameter MUST be present, "http://example.com/test" will not match this route, so your Auth::test method will not be called.
  • I'm using 1.4, and I know that the parameter is required but in the case that someone forgets to add one how do I handle that?  Do I have to setup other routes to catch such an event, what is my best method to fix this issue.  I can't rely on my users to always supply parameters.

    Edit: I think I have figured a way to fix this and still have error checking in my controller function using named parameter regex routes.
    Thanks for your help Harro.
  • If you want a route that routes both "test" and "test/param", the slash in the route has to be optional too:

    'test(/:any)' => 'auth/test/$1', // slash inside the brackets!

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion