Created
November 27, 2013 23:01
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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