zoukankan      html  css  js  c++  java
  • Laravel 6 路由常见用法

    Laravel 路由的几种常见模式

    1.基础路由get、post

    Route::get('myget',function(){
          return 'this is get';
    });
    

    Laravel 中post有个csrf保护在使用postman进行测试的时候需要进入Http的中间件Middleware在VerifyCsrfToken中的except中添加路由名即可

    Route::post('mypost',function(){
          return 'this is post';
    });
    

    2.多请求路由match、any

    Route::match(['get','post],'match',function(){
          return 'this is get and post';
    });
    
    Route::any('any',function(){
          return 'this is request from any http verb';
    });
    

    3.CSRF保护

    这里需要在这加一个csrf的令牌这样请求才会被接收

    Route::get('form',function(){
          return '<form method="post" action="any">.csrf_field().'<button type="submit">提交</buttton></form>';
    });
    

    4.视图路由

    访问路由名routeview就可以访问到welcome页面,这里的中括号是演示了当你访问路由名为website的时候把'jellysheep'这个值传到welcome界面里的{{$website}}显示

    Route::view('routeview','welcome',['website'=>'jellysheep']);
    

    5.路由参数

    单个参数

    Route::get('user/{id}',function($id){
          return 'user='.$id;
     });
    

    多个参数

    Route::get('user/{id}/name/{name}',function($id,$name){
          return 'user='.$id.'name='.$name;
     });
    

    可选参数 在参数后加一个问号并赋予一个初始值

    Route::get('user/{id?}/name/{name?}',function($id='1',$name='jelly'){
          return 'user='.$id.'name='.$name;
     });
    

    单个参数的正则约束

    Route::get('user/{name}',function($name){
          return 'user='.$name;
     })->where('id','[A-Za-z]+');
    

    多个参数的正则约束

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

    全局范围内进行约束:需要在app的Provides里的RouteServiceProvider中的boot方法定义一下约束方式. 那么在所有路由中包含了这个参数名,那么都会对这个参数进行约束

    Route::pattern('id','[0-9]+');
    

    6.命名路由

    主要是为了生成url和重定向提供了一个非常方便的一个方式

    Route::any('user/profile',function(){
          return 'myurl:'.route(name:'profile'); 
    })->name('profile');
    //重定向路由
    Route::any('redirect',function(){
          return redirect()->route(route:'profile'); 
    });
    

    这里还可以使用as关键字

    7.路由群组

    有时候我们多个路由有相同的部分,那么我们就可以使用路由群组统一设置一个前缀

    Route::group(['perfix'=>'jelly'],function(){
          Route::get('myget',function(){
                return 'this is get';
          });
    
          Route::post('mypost',function(){
                return 'this is post';
          });
    });
    

    8.路由关联控制器

    用户的url请求会被路由器转发给相应的路由和控制器,因此我们不会在路由里进行路由的处理,因此我们要关联控制器在Http目录下的controllers里

    namespace AppHttpControllers;
    class Mycontroller extends Controller{
          public function info(){
                return 'this is myinfo';
          }
    }
    
    Route::any('info','MyController@info');
    

    这里的参数传输在路由名后面加上"/{id}"传输的参数跟顺序有关

    9.总结

    php

  • 相关阅读:
    sql语句 之聚合函数
    UML类图几种关系的总结
    关于Object.defineProperty的get和set
    devDependencies和dependencies的区别
    函数声明和函数表达式的区别
    移动端滑屏滚动事件的问题(横向滑动阻止垂直页面滚动)
    使用Cordova和JQM在ios上需要注意的问题
    阻止事件冒泡,阻止默认事件,event.stopPropagation()和event.preventDefault(),return fal的区别
    有关闭包的总结
    CSS命名规范——BEM思想(非常赞的规范)
  • 原文地址:https://www.cnblogs.com/jellysheep/p/15020905.html
Copyright © 2011-2022 走看看