Blogger Information
Blog 37
fans 0
comment 0
visits 20790
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
综合实战第五课:laravel基础4-PHP培训九期线上班
渡劫小能手
Original
557 people have browsed it

Laravel的HTTP请求

use Illuminate\Http\Request; 引入Request类

all

获取所有输入数据数组,但是如果指定了下标而没有此数据,laravel就会报错

  1. $res = $request->all();
  2. //var_dump($res['prod_id']);没有prod_id,就会laravel报错

input

获取单个输入值,如果不带参数就是获取全部输入值
第二个参数,就是第一个参数不存在的时候,默认输出第二个参数
如果指定的下标不存在,就会返回null

  1. $res = $request->input('pro_id');
  2. $res = $request->input('pro_id''http://www.baidu.com');
  3. header('Location:'.$res);

query

和input没有区别

通过动态属性获取

获取不存在的返回null,没有默认值

  1. $res = $request->pro_id;

Lavarel 框架生成的全部 cookies 都是加密的,并且已经用授权码签名,这意味着如果它们被客户端改变就会失效

  1. setcookie('name','laosong');
  1. $res = $_COOKIE['name'];
  1. return response('Hello World')->cookie('name', 'wangyulong', 10);
  1. $res = $request->cookie('name');

session

设置session

  1. session(['name'=>'liushanghai']);

获取session

可以设置第二个参数,如果没有第一个参数,输出第二个默认值

  1. $res = session('name');
  2. $res = session('addr','合肥');

清除session

清除值,键addr还在

  1. session(['addr'=>null]);

删除单个session

  1. $request->session()->forget('addr');

删除全部session

  1. $request->session()->flush();

中间件

中间件提供了一种方便的机制过滤进入应用程序的 HTTP 请求。例如,Laravel 包含一个中间件,验证您的应用程序的用户身份验证。如果用户未被认证,中间件会将用户重定向到登录界面。然而,如果用户通过身份验证,中间件将进一步允许请求到应用程序中<br />在路由后面加入一个中间件auth,如果有auth并且没有登录,就会302跳转到login<br />跳转可以用js的 setTimeoutwindow.location.href = '/home/lists' ;

  1. Route::get('/product-list','Product@lists');->middleware('auth');
  2. Route::get('/home/lists','Home@lists');->name('login');
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:中间件类似之前的前置方法, 有点像请求拦截器, 也像是分发器的一种变形, 很有用
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!