laravel路由Routes

路由

最基本的 Laravel 路由接受一个 URI 和一个闭包:

use Illuminate\Support\Facades\Route;

Route::get('/hellow', function () {
    return 'Hello World';
});

所有 Laravel 路由都在路由文件中定义,这些文件位于routes目录中。这些文件由应用程序的App\Providers\RouteServiceProvider. 该routes/web.php文件定义了用于您的 Web 界面的路由。这些路由被分配到web中间件组,它提供了会话状态和 CSRF 保护等功能。目录中的路由routes/api.php是无状态的,并被分配到api中间件组。

对于大多数应用程序,您将从在routes/web.php文件中定义路由开始。定义中的路线routes/web.php可以通过在浏览器中输入定义路由的网址进行访问。例如,您可以通过http://example.com/user在浏览器中导航到以下路线:

use App\Http\Controllers\UserController;

Route::get('/user', [UserController::class, 'index']);

常用路由器方法

Route::get('index','IndexController@index');
Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);
Route::redirect('/here', '/there', 301); //重定向路由
Route::view('/welcome', 'welcome', ['name' => 'hellow']); //直接返回视图
//带参数路由
Route::get('/user/{id}', function ($id) {
    return 'User '.$id;
});
Route::get('/posts/{post}/comments/{comment}', function ($postId, $commentId) {
    //
});
//路由中可直接使用依赖注入:
use Illuminate\Http\Request;
Route::get('/user/{id}', function (Request $request, $id) {
    return 'User '.$id;
});
//可选参数
Route::get('/user/{name?}', function ($name = null) {
    return $name;
});
//正则表达式约束
Route::get('/user/{id}/{name}', function ($id, $name) {
    //
})->where(['id' => '[0-9]+', 'name' => '[a-z]+']);
//使用内置辅助方法来约束
Route::get('/user/{id}/{name}', function ($id, $name) {
    //
})->whereNumber('id')->whereAlpha('name');
//重命名
Route::get('/user/profile',[UserProfileController::class, 'show'])->name('profile');
//中间件与分组
Route::middleware(['first', 'second'])->group(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
    });
});
//限速 更多资料:https://laravel.com/docs/8.x/routing#rate-limiting
protected function configureRateLimiting()
{
    RateLimiter::for('global', function (Request $request) {
        return Limit::perMinute(1000);
    });
}
//跨域资源共享(CORS):https://laravel.com/docs/8.x/routing#cors

全局约束

如果您希望路由参数始终受给定正则表达式的约束,则可以使用该pattern方法。您应该在类的boot方法中定义这些模式App\Providers\RouteServiceProvider:

public function boot()
{
    Route::pattern('id', '[0-9]+');
}

一旦定义了模式,它就会自动应用于使用该参数名称的所有路由,例如:

Route::get('/user/{id}', function ($id) {
    // Only executed if {id} is numeric...
});

路由缓存

在将应用程序部署到生产环境时,您应该利用 Laravel 的路由缓存。使用路由缓存将大大减少注册所有应用程序路由所需的时间。要生成路由缓存,请执行route:cacheArtisan 命令:

php artisan route:cache

运行此命令后,您的缓存路由文件将在每个请求中加载。请记住,如果您添加任何新路由,则需要生成新的路由缓存。因此,您应该只route:cache在项目部署期间运行该命令。

您可以使用以下route:clear命令清除路由缓存:

php artisan route:clear

路由如何注册?

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值