0

I am trying to pass the username via the url.

site.tld/{username}/account

So i have this entry here in my routes

Route::group(array('prefix' => '{username}'), function($username)
{
    $user = User::whereUsername($username)->first();
    if(!is_null($user))
    {
        Route::get('portfolio', 'PortfolioController@getIndex');
        Route::get('profile', 'ProfileController@getIndex');
        ....
    }
}

i get the following error.

Object of class Illuminate\Routing\Router could not be converted to string

What am i doing wrong?

1 Answer 1

1

Route::group() does not work the same way as Route::method(), the closure is executed during the route listing procedure and what is passed to it is the router, not your parameter:

Route::group(array('prefix' => '{username}'), function($router) { ... });

So you are basically doing:

$user = User::whereUsername($router)->first();

That's why it says

Object of class Illuminate\Routing\Router could not be converted to string

But you may user a filter:

Route::filter('age', function($route, $request)
{
    if (! User::whereUsername($route->parameter('username'))->first())
    {
        App::abort(404);
    }
});

Route::group(array('prefix' => '{username}', 'before' => 'age'), function($username)
{
    Route::get('portfolio', 'PortfolioController@getIndex');
    Route::get('profile', 'ProfileController@getIndex');
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.