Laravel Framework Request Response and Session Operation Examples

  • 2021-12-09 08:35:54
  • OfStack

This article illustrates the Laravel framework Request, Response, and Session operations. Share it for your reference, as follows:

Request operation


public function request(Request $request){
  //1. Value 
  //echo $request->input('name');
  //echo $request->input('sex',' Unknown ');
  /*if($request->has('name')){
    echo $request->input('name');
  }else{
    echo ' No parameter ';
  }*/
  // Get all the parameters 
  /*$res = $request->all();
  dd($res);*/
  //2. Determine the request type 
  /*echo $request->method();
  if($request->isMethod('post')){
    echo 'Yes';
  }else{
    echo 'No';
  }*/
  // Judge whether it is ajax Request 
  /*$res = $request->ajax();
  var_dump($res);*/
  // Interpret the requested path 
  /*$res = $request->is('User/*');
  var_dump($res);*/
  // Gets the current url
  //echo $request->url();
}

Response operation


public function response(){
  // Response json
  /*$data = [
    'errCode'=>0,
    'errMsg' =>'success',
    'data'  => 'yxh',
  ];
  return response()->json($data);*/
  // Redirect 
  //return redirect('Hello');
  //return redirect('Hello')->with('message','yxh');
  //return redirect()->action('UserController@Hello')->with('message','yxh');
  //return redirect()->route('Hello')->with('message','yxh');
  // Back up 1 Pages 
  //return redirect()->back();
}

Session operation


// Settings session Value in 
public function session1(Request $request){
  //1.HTTP request session();
  /*$request->session()->put('key1','value1');
  echo $request->session()->get('key1');*/
  //2.session()
  /*session()->put('key2','value2');
  echo session()->get('key2'); */
  //3.session
  // Store data to session
  //Session::put('key3','value3');
  // Get session Data in 
  //echo Session::get('key3');
  // Get the default value if it does not exist 
  //echo Session::get('key4','default');
  // Store data as an array 
  //Session::put(['key4'=>'value4']);
  // Put the data into Session In the array of 
  /*Session::push('user','yxh');
  Session::push('user','imooc');*/
  // Fetch the data of the array 
  /*$res = Session::get('user','default');
  var_dump($res);*/
  // Delete the data after taking it out 
  /*$res = Session::pull('user','default');
  var_dump($res);*/
  // Take out session Ownership in 
  /*$res = Session::all();
  var_dump($res);*/
  // Judge session Is there a value in 
  /*if(Session::has('key1')){
    $res = Session::all();
    dd($res);
  }else{
    echo ' Nonexistent ';
  }*/
  // Delete session The value specified in the 
  //Session::forget('key1');
  // Empty all session
  //Session::flush();
  // Setting temporary data , Access only 1 Second, first 2 The second access was deleted 
  //Session::flash('key-flash','flash');
}

More readers interested in Laravel can check the topics of this site: "Introduction and Advanced Tutorial of Laravel Framework", "Summary of Excellent Development Framework of php", "Introduction Tutorial of php Object-Oriented Programming", "Introduction Tutorial of php+mysql Database Operation" and "Summary of Common Database Operation Skills of php"

I hope this article is helpful to PHP programming based on Laravel framework.


Related articles: