user5369230
user5369230

Reputation: 21

Validate route parameters in controller

I have a method like this in my UserController:

public function verifyUsername()
{
    $rules = array(
        'username' => 'required|max:30|unique:users,username'
    );

    $validator = Validator::make(Input::all(), $rules);

    if ($validator->fails())
    {
        return Response::json(array(
            'success' => 'false',
            'errors' => $validator->messages()
        ), 400);
    }
    else
    {
        return Response::json(array(
            'success' => 'true'
        ));
    }
}

I want to have a URL route like this:

/users/verify/username/{username}

How can I pass the username variable from the route to the method in the controller?

Upvotes: 2

Views: 2898

Answers (1)

whoacowboy
whoacowboy

Reputation: 7447

You pass in a array with uses set to the ControllerName@methodName

You might want to look at Named Routes for more information.

routes.php

Route::get('users/verify/username/{username}', 
           array('uses' => 'UserController@verifyUsername'));

Controller

public function verifyUsername($username)
{
    //it would be nice if this works, but I don't think it will
    $validator = Validator::make(Input::get('username'), $rules);

    // something like this should work
    $data = ['username' => $username];
    $validator = Validator::make($data, $rules);
}

Upvotes: 1

Related Questions