laravel中Request、Session、Response、Middelware

Request请求

使用的是symfony/http-foundation组件

存放$_get,$_post,$_cookie,$_files,$_server等数据

Public function request(Request $request)

{

           取单一get值,可加默认值

           Ehco $request->input(‘name’,‘未知’);

 

           判断是否有这个数据

           if($request->has(‘name’’)){

                    ehco $request->input(‘name’, ‘未知’);

           } else {

                    echo ‘null’;

           }

           取所有参数

           $res = $request->all();

           dd($res)

 

           判断请求类型

           获取类型

           $res = $request->method();

           判断类型

           if($request->isMethod(‘POST’))

           else

 

           判断是否为ajax方法

           res = $request->ajax();

           var_dump($res);

 

           请求的路径是否满足特定的格式

           判断是否在哪个controllers下某个方法

           $res =$request->is(‘student/*’)

 

           当前URL

           echo $request->url()

}

 

 

 

 

 

 

Session

Public function request(Request $request)

{

                   HTTP request 类的session()方法

         $request->session()->put(‘key1’, ‘value’);

           $request->session()->get(‘key1’);

 

 

                   Session()辅助函数

           Session()->put(‘key1’,‘value’);

           Session()->get(‘key1’);

 

 

                  Sessionfaçade

         Session::put(‘key1’, ‘value’);

           Session::get(‘key1’,‘default’);

 

           以数组方式存值

           Session::put([‘key’=>’val’]);

 

           把数据放到session数组中

           Session::push(‘value1’,‘value’);

 

           取完数据并删除

           Session::pull(‘value’1);

 

           取出所有数据

           Session::all();

 

           判断session中KEY是否存在

           Session::has(‘key’);

 

           删除session中KEY

           Session::forget(‘key’);

 

           删除session所有KEY

           Session::flush(‘key’);

 

           临时数据(第一次访问有,第二次即空)

           Session::flash(‘key’);

         }

 

 

Response响应

常见类型

字符串

 

视图

 

Json

return Response()->json($data);

 

重定向

路由名字

return Redirect(‘session2’)->with(‘message’,‘这是传输的参数’); // session2路由名字

 

action()

return redirect()->action(‘StudentController@update’)->with(‘message’,‘这是传输的参数’);

 

路由别名route()

return redirect()->route(‘session’)->with(‘message’,‘这是传输的参数’);

 

返回上一级

return redirect()->back();


 

Middelware中间件

1.新建立

<?php

 

namespace App\Http\Middleware;

 

 

class Activity

{

         //前置中间件

   public function handle($request, \Closure $next)

    {

       if (time() < strtotime('2016-06-05')) {

           //跳转到其他页

           return redirect('active');

       }

       return $next($request);

}

 

         //后置中间件

   public function handle($request, \Closure $next)

{       

         $response = $next($request);

         echo ‘我是后置操作,请往下写逻辑哦’

    }

 

}

 

2.Kernel.php下

   /**

    * The application's route middleware.

    *

    * These middleware may be assigned to groups or used individually.

    *

    * @var array

    */

   protected $routeMiddleware = [

       'auth' => \Illuminate\Auth\Middleware\Authenticate::class,

       'auth.basic' =>\Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,

       'bindings' =>\Illuminate\Routing\Middleware\SubstituteBindings::class,

       'can' => \Illuminate\Auth\Middleware\Authorize::class,

       'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,

       'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,

       'activity' =>\App\Http\Middleware\Activity::class,

];

 

3.路由使用

Route::group(['middleware' =>['activity']], function () {

         以下为需要验证的方法

   Route::get('student/index', ['uses' => 'StudentController@index']);

   Route::any('student/create', ['uses' => 'StudentController@create']);

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值