devs
devs

Reputation: 541

Laravel - Filter to Controller

I'm trying to use Filters to check if the user is a guest... if so, use a method in a Controller.... let me know if I'm doing this right... I keep getting the error call_user_func_array() expects parameter 1 to be a valid callback, no array or string given

So routes:

Route::get('/', ['before' => 'checkUserFilter'], function()
{
    return View::make('home');
});

Filter:

Route::filter('checkUserFilter', function() 
{
    if (Auth::guest()) 
    {
        Route::get('/', 'User@createUser');
    }

});

Controller

public function createUser($username)
{

    $username = 'test-'.str_random(4);
    $newUser = new User;
    if ($username == null) {
        $newUser->username = 'user-'.str_random(4);
    } else  {
        $newUser->username = $username;
    }

    $newUser->password = str_random(8); // ignore this line.....
    $newUser->save();

    Auth::login($newUser); 
}

Upvotes: 0

Views: 1043

Answers (1)

Antonio Carlos Ribeiro
Antonio Carlos Ribeiro

Reputation: 87719

You must redirect, in this case you can use action(), and you also need to create a route for that action, out of your filter, so, this could be your code:

Route::filter('checkUserFilter', function() 
{
    if (Auth::guest()) 
    {
        return Redirect::action('User@createUser');
    }

});

Route::get('/createUser', 'User@createUser');

EDIT:

Why you ned to create that /createUser route?

Because Redirect will basically look for a route that points that action and redirect to the URI related to the route. So, Redirect will send your browser to http://app.com/createUser.

If you don't create this route Redirect will raise a NotFoundHttpException.

Upvotes: 2

Related Questions