如何更改Laravel 5.2中的URL命名约定?

时间:2017-07-16 07:51:02

标签: laravel-5

我有一个吉他课程网站。它具有层次结构类别 - > lesson-> exercise

目前网址显示如下:

Category: /category/1/somecat

Lesson: /lesson/1/some-title

Exercise: /exercise/1/some-title

这个命名有很多地方是硬编码的,例如视图文件夹名称,视图,路线等。

这就是我真正想要的:

Category: /guitar-lesson-category/1/somecat

Lesson: /guitar-lesson/1/some-title

Exercise: /guitar-lesson-exercise/1/some-title

如果我对吉他课程感兴趣,我觉得这对谷歌搜索会更好。

以下是routes.php文件中的一些条目:

Route::get('/category', 'CategoryController@index');
Route::get('/category/{id}/{name?}', 'CategoryController@category');

Route::get('/lesson/{id}/{name?}', 'LessonController@index');
Route::post('/lesson/hit/{id}', 'LessonController@hit');
Route::post('/lesson/progress/{id}', 'LessonController@progress');
Route::post('/lesson/favorite/{id}', 'LessonController@favorite');

Route::get('/exercise/{id}/{name?}', 'ExerciseController@index');
Route::post('/exercise/hit/{id}', 'ExerciseController@hit');
Route::post('/exercise/progress/{id}', 'ExerciseController@progress');
Route::post('/exercise/favorite/{id}', 'ExerciseController@favorite');

知道如何尽可能轻松地做到这一点吗?什么编辑以及如何欣赏的具体例子!

谢谢!

1 个答案:

答案 0 :(得分:0)

////Route Group Example
//Route::prefix('admin')->group(function () {
//    Route::get('users', function () {
//        // Matches The "/admin/users" URL
//    });
//});

//Solution For You
Route::prefix('/category')->group(function () {
    Route::get('/', 'CategoryController@index');
        // Matches The "/category" URL
    Route::get('/{id}/{name?}', 'CategoryController@category');
        // Matches The "/category/{id}/{name?}" URL
});

Route::prefix('/lesson')->group(function () {
    Route::get('/{id}/{name?}', 'LessonController@index');
        // Matches The "/lesson/{id}/{name?}" URL
    Route::post('/hit/{id}', 'LessonController@hit');
        // Matches The "/lesson/hit/{id}" URL
    Route::post('/progress/{id}', 'LessonController@progress');
        // Matches The "/lesson/progress/{id}" URL
    Route::post('/favorite/{id}', 'LessonController@favorite');
        // Matches The "/lesson/favorite/{id}" URL
});

Route::prefix('/exercise')->group(function () {
    Route::get('/{id}/{name?}', 'ExerciseController@index');
        // Matches The "/exercise/{id}/{name?}" URL
    Route::post('/hit/{id}', 'ExerciseController@hit');
        // Matches The "/exercise/hit/{id}" URL
    Route::post('/progress/{id}', 'ExerciseController@progress');
        // Matches The "/exercise/progress/{id}" URL
    Route::post('/favorite/{id}', 'ExerciseController@favorite');
        // Matches The "/exercise/favorite/{id}" URL
});
相关问题