zoukankan      html  css  js  c++  java
  • laravel路由基础

    基础:

    所有的laravel路由都定义在 app/Http/routes.php 这个文件里,会被框架自带加载。简单的路由会接受一个URI和一个Closure类。

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

    默认的routes.php 是被RouteServiceProvider所加载进入框架的,允许自定义路由和响应

    Route::get($uri, $callback);
    Route::post($uri, $callback);
    Route::put($uri, $callback);
    Route::patch($uri, $callback);
    Route::delete($uri, $callback);
    Route::options($uri, $callback);

    html是不支持PUT DELETE 等方法的,所以需要伪造一下

    表单中加入隐式的_method方法和value值
    
    <form action="/foo/bar" method="POST">
        <input type="hidden" name="_method" value="PUT">
        <input type="hidden" name="_token" value="{{ csrf_token() }}">
    </form>
    
    也可以通过 method_field('PUT')
    输出
    <input type="hidden" name="_method" value="PUT">

    如果需要一个路由对多个HTTP请求进行相应,可以使用math方法或者any方法

    Route::match(['get', 'post'], '/', function () {
        //
    });
    
    Route::any('foo', function () {
        //
    });

    通过Route::current()访问当前的路由

    $route = Route::current();
    
    $name = $route->getName();
    
    $actionName = $route->getActionName();
    $name = Route::currentRouteName();
    
    $action = Route::currentRouteAction();

    二 路由参数

    必选参数

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

    参数约束

    对路由中的URI参数进行约束,通过where字段中加入正则表达式

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

    全局约束

    可以在RouteServiceProvider中的 boot 方法中 对给定的路由参数进行全局约束

    位于appHttpProviderRouteServiceProvider.php中

    public function boot(Router $router)
    {
        $router->pattern('id', '[0-9]+');
    
        parent::boot($router);
    }

     之后访问id会自动进行约束

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

    路由重命名

    通过在路由定义数组中使用  as  参数可以为路由重新定义方法

    Route::get('user/profile', ['as' => 'profile', function () {
        //
    }]);
    
    也可以同时定义控制器
    Route::get('user/profile', [
        'as' => 'profile', 'uses' => 'UserController@showProfile'
    ]);

    也可以通过链式调用name函数来定义

    Route::get('user/profile', 'UserController@showProfile')->name('profile');

    路由群组

    通过在群组属性数组中定义一个 as属性为群组定一个公共前缀

    Route::group(['as' => 'admin::'], function () {
        Route::get('dashboard', ['as' => 'dashboard', function () {
            // Route named "admin::dashboard"
        }]);
    });

    测试

    定义路由群组:
    
    Route::group(['as' => 'admin::'], function(){
       Route::get('groupsub', ['as' => 'sub', function(){
            echo "in route group";
       }]);
    });
    
    
    通过路由群组前缀访问路由:
    
    Route::get('/test', function(){
        return redirect() -> route('admin::sub');
    });
    
    访问http://localhost/test时会被重定向到
    http://localhost/groupsub输出
    
    in route group

    CSRF验证

    使用 csrf_token()生成token值添加到表单中laravel会自动验证
    <input type="hidden" name="_token" value="csrf_token()“ />
    
    使用 csrf_field() 自动生成字段
    <?php echo csrf_field(); ?>
    生成
    <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">

    通过在VerifyCsrfToken 中except数组中可以添加不验证的路由VerifyCsrfToken.php文件中

    class VerifyCsrfToken extends BaseVerifier
    {
        /**
         * The URIs that should be excluded from CSRF verification.
         *
         * @var array
         */
        protected $except = [
            //此处添加
        ];
    }

     

  • 相关阅读:
    Linux 虚拟机虚拟网卡问题导致无法连接问题
    使用 Load Balancer,Corosync,Pacemaker 搭建 Linux 高可用集群
    如何在 Linux 虚拟机上扩展根文件系统
    Linux 虚拟机中配置 GNOME + VNC
    在 Linux 中使用 Azure Premium 存储的基本优化指南
    如何为运行的 ARM Linux 启用 LAD2.3 版本的诊断扩展
    如何解决 Linux 虚拟机磁盘设备名不一致的问题
    Java 调用 Rest api 设置经典 Linux 虚拟机的实例启停
    CentOS: 将虚拟机迁移到 Azure (以阿里云为例)
    Linux 虚拟机的计划维护
  • 原文地址:https://www.cnblogs.com/yangxunwu1992/p/5399027.html
Copyright © 2011-2022 走看看