laravel 5.5 -- router

本文详细介绍了Laravel框架中的路由配置方法,包括基本路由、资源路由、命名路由等,并讲解了如何进行路由参数设置及正则约束,同时涵盖了路由前缀、中间件添加等内容。

web/api.php中定义的路由会自动添加api前缀,如需修改该前缀,可以在RouteServiceProvider修改。
web.php 路由里的POST, PUT, DELETE方法,在提交表单时候必须加上CSRF参数。

两个 api api1api2

$route = Route::current();
$name = Route::currentRouteName();
$action = Route::currentRouteAction();

resource

* GET /test
index()  //  Display a listing of the resource.

* GET /test/create
create()  // Show the form for creating a new resource.

* POST /test
store()  // Store a newly created resource in storage.

* GET /test/{id}
show($id)  // Display the specified resource.

* GET /test/{id}/edit
edit($id)  // Show the form for editing the specified resource.

* PUT /test/{id}
update($id)  // Update the specified resource in storage.

* DELETE /test/{id} 
destroy($id)  // Remove the specified resource from storage.

基本路由

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::any($uri, $callback);        // 任意 method

Route::match(['get', 'post'], '/', function () {
    //
});
# 重定向路由
Route::redirect('/here', '/there', 301);

# 只需要返回一个视图
Route::view('/welcome', 'welcome', ['name' => 'Taylor']);

路由参数

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

Route::get('user/{name?}', function ($name = 'John') {   // 一定要给可选参数设置默认值
    return $name;
});

正则表达式约束


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]+']);

全局约束

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

    parent::boot();
}

命名路由

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

// 使用
$url = route('profile');
// 生成重定向...
return redirect()->route('profile');

// 在中间中检查当前路由
public function handle($request, Closure $next)
{
    if ($request->route()->named('profile')) {
        //
    }
    return $next($request);
}

添加中间件

Route::middleware(['first', 'second'])->group(function () {
    Route::get('/', function () {
        // 使用 `first` 和 `second` 中间件
    });

    Route::get('user/profile', function () {
        // 使用 `first` 和 `second` 中间件
    });
});

命名空间

Route::namespace('Admin')->group(function () {
    // 在 "App\Http\Controllers\Admin" 命名空间下的控制器
});

子域名路由

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

路由前缀

Route::prefix('admin')->group(function () {
    Route::get('users', function () {
        // 匹配包含 "/admin/users" 的 URL
    });
});

路由命名前缀

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

表单伪造

<input type="hidden" name="_method" value="PUT">
// 或者 {{ method_field('PUT') }}

获取当前路由信息

// Route::get('/', 'TestController@test')->name("mytest");
$route = Route::current(); //  object(Illuminate\Routing\Route)
$name = Route::currentRouteName(); // mytest 
$action = Route::currentRouteAction(); // 控制器中:App\Http\Controllers\TestController@test  路由中:null

隐式绑定

Route::get('api/users/{user}', function (App\User $user) {
    return $user->email;   // 传人的 id
});

# 自定义键名 在模型中修改
// App/User.php
public function getRouteKeyName()
{
    return 'slug';
}

显式绑定

# RouteServiceProvider
public function boot()
{
    parent::boot();

    Route::model('user', App\User::class);
}

Route::get('profile/{user}', function ($user) {
    //
});

自定义解析逻辑

public function boot()
{
    parent::boot();

    Route::bind('user', function ($value) {
        return App\User::where('name', $value)->first() ?? abort(404);
    });
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值