zoukankan      html  css  js  c++  java
  • laravel5.5路由

    1. routes/web.php

    routes/web.php 文件用于定义 web 界面的路由。这里面的路由都会被分配给 web 中间件组,它提供了会话状态和 CSRF 保护等功能。

    指向 web 路由文件中定义的 POST、PUT 或 DELETE 路由的任何 HTML 表单都应该包含一个 CSRF 令牌字段,否则,这个请求将会被拒绝。

    2. routes/api.php

    定义在 routes/api.php 中的路由都是无状态的,并且被分配了 api 中间件组。

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

    3. 重定向路由

    Route::redirect('/here', '/there', 301);
    

    4. 路由参数

    定义多个参数,参数不能包含 - ,但是可以使用下划线 _ 代替

    //可以定义多个路由参数
    Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) {
        //
    });
    

    可选参数

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

    5. 约束

    正则表达式约束

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

    全局约束

    //就使用 pattern 方法在 RouteServiceProvider 的 boot 方法中定义这些模式
    public function boot()
    {
        Route::pattern('id', '[0-9]+');
    
        parent::boot();
    }
    

    6. 命名路由

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

    为路由指定了名称后,就可以使用全局辅助函数 route 来生成链接或者重定向到该路由:

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

    如果是有定义参数的命名路由,可以把参数作为 route 函数的第二个参数传入,指定的参数将会自动插入到 URL 中对应的位置:

    Route::get('user/{id}/profile', function ($id) {
        //
    })->name('profile');
    
    $url = route('profile', ['id' => 1]);
    

    如果你想判断当前请求是否指向了某个路由,你可以调用路由实例上的 named 方法。例如,你可以在路由中间件中检查当前路由名称:

    public function handle($request, Closure $next)
    {
        if ($request->route()->named('profile')) {
            //
        }
    
        return $next($request);
    }
    

    7. 路由组

    使用Route::group()方法

    Route::group(function(){
        Route::get('/', function () {
            //...
        });
    })
    

    在group之前调用下面的方法配合group实现更过功能

    1. 中间件
    Route::middleware(['first', 'second'])->group(function () {
        Route::get('/', function () {
            // 使用 first 和 second 中间件
        });
    });
    
    1. 命名空间
    Route::namespace('Admin')->group(function () {
        // 在 "AppHttpControllersAdmin" 命名空间下的控制器
    });
    
    1. 路由前缀
    Route::prefix('admin')->group(function () {
        Route::get('users', function () {
            // 匹配包含 "/admin/users" 的 URL
        });
    });
    
    1. 子域名路由
    Route::domain('{account}.myapp.com')->group(function () {
        Route::get('user/{id}', function ($account, $id) {
            //
        });
    });
    

    8 路由模型绑定

    9 表单方法伪造

    HTML 表单不支持 PUT、PATCH 或 DELETE 行为。所以当你要从 HTML 表单中调用定义了 PUT、PATCH 或 DELETE 路由时,你将需要在表单中增加隐藏的 _method 输入标签。使用 _method 字段的值作为 HTTP 的请求方法:

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

    你也可以使用辅助函数 method_field 来生成隐藏的 _method 输入标签:

    {{ method_field('PUT') }}
    

    10 访问当前路由

    $route = Route::current();
    
    $name = Route::currentRouteName();
    
    $action = Route::currentRouteAction();
    
  • 相关阅读:
    如何将CentOS的默认启动界面修改为图形界面or字符界面
    如何将CentOS的默认启动界面修改为图形界面or字符界面
    virtualbox下CentOS7安装增强功能
    蓝牙4.0
    HC-SR04超声波测距
    STM32F407 通用同步异步收发器(串口)
    STM32F4 TIM(外设定时器)
    STM32F4 系统定时器
    STM32F4 异常与中断
    LED和按键实验
  • 原文地址:https://www.cnblogs.com/redirect/p/8435788.html
Copyright © 2011-2022 走看看