Skip to content

Instantly share code, notes, and snippets.

@matula
Created November 27, 2013 23:01
Show Gist options
  • Save matula/7684596 to your computer and use it in GitHub Desktop.
Save matula/7684596 to your computer and use it in GitHub Desktop.
I was trying to have URLs like "http://example.com/myusername" and "http://example.com/myusername/about". Using Laravel, you're able to throw a wildcard in the prefix. The route needs to go at the end of the routes.php file if you have other static routes. #laravel
<?php
// Routes for usernames
Route::group(['prefix' => '{username}', 'before' => 'valid-user'], function()
{
Route::get('/', 'UserController@index');
Route::get('about', 'UserController@about');
});
// Check to make sure the user is valid
Route::filter('valid-user', function($route, $request)
{
// Get the username from the URL
$username = $request->segment(1);
// lookup username in user table, and if none exist, throw 404
if (!$username)
{
return Response::view('errors.404', [], 404);
}
// Will probably cache the username here, so it doesn't need a db query on every route
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment