I often see websites that have pretty URLs for categories, subcategories and articles. How do I replicate this function with Laravel?
I need to get the following routes working:
/kb/{id (numeric)}
/kb/{category}
/kb/{category}/{article} (redirects to the route immediately below, adding the id)
/kb/{category}/{article}/{id (numeric)}
/kb/{category}/{subcategory}
/kb/{category}/{subcategory}/{article} (redirects to the route immediately below, adding the id)
/kb/{category}/{subcategory}/{article}/{id (numeric)}
/kb/download/{id (numeric)}
I was also going to try and include pretty URLs for pagination, but I realised it might complicate it further to have categories and subcategories have a /page/1
part after - but, if you know how to make that work, please tell!
Currently, my code is as follows:
Route::group(['prefix' => '/kb', 'as' => 'kb::'], function () {
Route::get('/', function() {
echo "<h1>Index</h1>";
});
Route::get('{id}', function($id = null) {
echo "<h1>ID: $id</h1>";
})->where('id', '0-9+');
Route::get('download/{id}', function($id = null) {
echo "<h1>Download ID: $id</h1>";
})->where('id', '0-9+');
Route::get('{category}', function($category = null) {
echo "<h1>Category: $category</h1>";
});
Route::get('{category}/{article}', function($category = null, $article = null) {
echo "<h1>Category: $category</h1>";
echo "<h1>Article: $article</h1>";
});
Route::get('{category}/{article}/{id}', function($category = null, $article = null, $id = null) {
echo "<h1>Category: $category</h1>";
echo "<h1>Article: $article</h1>";
echo "<h1>ID: $id</h1>";
})->where('id', '0-9+');
Route::get('{category}/{subcategory}', function($category = null, $subcategory = null) {
echo "<h1>Category: $category</h1>";
echo "<h1>SubCategory: $subcategory</h1>";
});
Route::get('{category}/{subcategory}/{article}', function($category = null, $subcategory = null, $article = null) {
echo "<h1>Category: $category</h1>";
echo "<h1>SubCategory: $subcategory</h1>";
echo "<h1>Article: $article</h1>";
});
Route::get('{category}/{subcategory}/{article}/{id}', function($category = null, $subcategory = null, $article = null, $id = null) {
echo "<h1>Category: $category</h1>";
echo "<h1>SubCategory: $subcategory</h1>";
echo "<h1>Article: $article</h1>";
echo "<h1>ID: $id</h1>";
})->where('id', '0-9+');
});
Unfortunately, this has a habit of mixing up articles and subcategories, and is a total mess. But I see websites with this structure, and even more complicated structures, and they work - what's the secret?!
It's also important to remember that two categories might have subcategories with the same name, that aren't the same subcategory, further complicating things.
EDIT: I've also just taken a look at maybe making each article be prefixed with article
, like /kb/cat/article/evil/666
. Still doesn't seem to work right though, irritatingly.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire