close

路由命名 (不常使用)

呼叫路由更方便, 不用輸入網址, 使用 route('RouteName') 即可呼叫, 此函式可以在 Controller 中使用。

1. 給閉包 function 的 Route 命名

Route::get('user', ['as' => 'profile', function() {
    echo route('profile');    // 顯示當前的網址, ex. http://localhost/user
    return '命名路由';
}]);

2. 給 Controller 的 Route 命名

Route::get('user/profile', [
    'as' => 'profile', 'uses' => 'UserController@showProfile'
]);

3.簡便的命名( 較常使用 )

Route::get('user/profile', 'UserController@showProfile')->name('profile');

 

路由分組

如果有很多的 Route 前綴都有 admin 或是 namespace 都有 Admin, 就把他 Group 起來, 可以簡化重複的程式碼。

1.Group前

Route::get('admin/login', 'Admin\IndexController@login');
Route::get('admin/index', 'Admin\IndexController@index');

2.Group後

Route::Group(['prefix' => 'admin', 'namespace' => 'Admin'], function() {
    Route::get('login', 'IndexController@login');
    Route::get('index', 'IndexController@index');
});

 

Artisan 顯示所有路由

除了能顯示所有路由, 也能檢查檔案是否有缺少!

php artisan route:list

 

Resource Route ( 資源路由 )

在使用下面方法前, 必須創建 ArticleController! 當使用下面方法後, 再使用 php artisan route:list 指令, 會發現多了相當多的 Route!

Route::resource('article', 'ArticleController');

 

 

 

 

 

 

arrow
arrow
    文章標籤
    route 路由 HTTP laravel
    全站熱搜

    Mayuge 發表在 痞客邦 留言(0) 人氣()