laravel 5.5 -- router

来源:互联网 发布:淘宝直播招聘主播吗 编辑:程序博客网 时间:2024/05/23 00:41

路由

resource

* GET /testindex()  //  Display a listing of the resource.* GET /test/createcreate()  // Show the form for creating a new resource.* POST /teststore()  // Store a newly created resource in storage.* GET /test/{id}show($id)  // Display the specified resource.* GET /test/{id}/editedit($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('api/users/{user}', function (App\User $user) {    return $user->email;   // 传人的 id});# 自定义键名 在模型中修改public function getRouteKeyName(){    return 'slug';}

显式绑定

# RouteServiceProviderpublic 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);    });}

基本路由

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

全局约束

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

表单伪造

<form action="/foo/bar" method="POST">    <input type="hidden" name="_method" value="PUT">    // 或者 {{ method_field('PUT') }}    <input type="hidden" name="_token" value="{{ csrf_token() }}"></form>

获取当前路由信息

// 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
原创粉丝点击