Iamzozo
Iamzozo

Reputation: 2358

Laravel routes & usernames in routes

What options are available to prevent users to use existing route names as an account name.

For example: I've a route, that shows latest posts domain.com/latest. I also want to simplify user's profile page url's, like the following domain.com/a_user.

The problem occurs when a user registers with the latest username. It depends on route order, but I want to refuse those usernames. Is there any simple validation for this, or I should store all base routes in an array and check against it?

Upvotes: 2

Views: 237

Answers (1)

Bogdan
Bogdan

Reputation: 44586

You can use not_in in conjunction with an array of route paths which you can get using Route::getRoutes(). So in your case you could have something like this:

$routes = [];

// You need to iterate over the RouteCollection you receive here
// to be able to get the paths and add them to the routes list
foreach (Route::getRoutes() as $route)
{
    $routes[] = $route->getPath();
}

$validator = Validator::make(
    ['username' => $username],
    ['username' => 'not_in:' . implode(',', $routes)]
);

Upvotes: 4

Related Questions