laravel 路由篇

所有 Laravel 路由都定义在路由文件中,这些文件位于 routes 目录下。

routes/web.php 文件定义路由开始的。 可以通过在浏览器中输入定义的路由 URL 来访问 routes/web.php 的路由。

routes/api.php 文件中定义的路由通过 RouteServiceProvider 被嵌套到一个路由组里面。在这个路由组中,会自动添加 URL 前缀 /api 到此文件中的每个路由,这样你就无需再手动添加了。

路由动词

Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);

基本路由

use Illuminate\Support\Facades\Route;

Route::get('/greeting', function () {
    return 'Hello World';
});//闭包函数

Route::get('/pay-order','PayController@payOrder');//直接调用控制器
 

Route::match(['get', 'post'], '/', function () {
//
});


Route::any('/', function () {
//
}); 

路由参数

Route::get('/user/{id}', function ($id) {
  return 'User '.$id;
});


//多个参数
Route::get('/posts/{post}/comments/{comment}', function ($postId, $commentId) { // });

//可选参数,有时,你可能需要指定一个路由参数,但你希望这个参数是可选的。你可以在参数后面加上 ? 标记来实现,但前提是要确保路由的相应变量有默认值

Route::get('/user/{name?}', function ($name = null) {
return $name;
});

Route::get('/user/{name?}', function ($name = 'John') {
return $name;
});

正则约束

Route::get('/user/{name}', function ($name) {
    //
})->where('name', '[A-Za-z]+');

Route::get('/user/{id}', function ($id) {
    //
})->where('id', '[0-9]+');

Route::get('/user/{id}/{name}', function ($id, $name) {
    //
})->where(['id' => '[0-9]+', 'name' => '[a-z]+']);

路由重命名

Route::get('/user/profile', function () {
    //
})->name('profile');

路由助手

users/{uiserd}/comments/{commentId}
方法1
route('users.comments.show',[1,2]);//http://myapp.com/users/1/comments/2
方法2
route('users.comments.show',['userid'=>1,'commentId'=>2]);//http://myapp.com/users/1/comments/2
方法3
route(
'users.comments.show',['userid'=>1,'commentId'=>2,'opt'=>'abc']);//http://myapp.com/users/1/comments/2?opt=abc

路由分组

Route::groupgroup(['prefix'=>'login'],function ($route) { 

$
route->post('/user-login','LoginController@userLogin');// 用户登录
$route->post('/refresh-token','LoginController@refreshToken');// 刷新token 
$route->post('/forget-password-get-code','LoginController@forgetPasswordGetCode');// 修改登录密码
$route->post('/reset-password','LoginController@resetPassword');// 重置密码
});

子域路由

Route::domain('{account}.example.com')->group(function () {
    Route::get('user/{id}', function ($account, $id) {
        //
    });
});

中间件

Route::middleware(['first', 'second'])->group(function () {
    Route::get('/', function () {
        // Uses first & second middleware...
    });

    Route::get('/user/profile', function () {
        // Uses first & second middleware...
    });
});

Route::group(['middleware'=>'auth'],function(){
Route::get('/', function () {
        // Uses first & second middleware...
    });

    Route::get('/user/profile', function () {
        // Uses first & second middleware...
    });
})

 

路径前缀

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

Route::group(['prefix'=>'admin'],function(){
    Route::get('/users', function () {
        // Matches The "/admin/users" URL
    });
    

}) 

 

名称前缀

Route::name('admin.')->group(function () {
    Route::get('/users', function () {
        // Route assigned name "admin.users"...
    })->name('users');
});

 

命名空间前缀

Route::get('/','ControllerA@index');

Route::group(['namespace'=>'API'], function(){
   Route::get('api/','ControllerB@index'); 
});

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章