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'); 
});

 

 

使用api 分组路由,在app目录下新增api/v1目录

修改app/Providers/RouteServiceProvider.php

复制代码
 protected $namespace = 'App\Http\Controllers';
 protected $apinamespace = 'App\api\v1';

    /**
     * The path to the "home" route for your application.
     *
     * @var string
     */
    public const HOME = '/home';

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */
    public function boot()
    {
        //

        parent::boot();
    }

    /**
     * Define the routes for the application.
     *
     * @return void
     */
    public function map()
    {
        $this->mapApiRoutes();

        $this->mapWebRoutes();

        //
    }

    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * @return void
     */
    protected function mapWebRoutes()
    {
        Route::middleware('web')
             ->namespace($this->namespace)
             ->group(base_path('routes/web.php'));
    }

    /**
     * Define the "api" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapApiRoutes()
    {
        Route::prefix('api')
             ->middleware('api')
             ->namespace($this->apinamespace)
             ->group(base_path('routes/api.php'));
    }
复制代码

 

posted @   fsl  阅读(489)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗
点击右上角即可分享
微信分享提示