1、Request
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use App\Student;
use Illuminate\Http\Request;
class StudentController extends Controller
{
public function index(Request $request){
//1、取值
echo $request->input('name','unknown');
if ($request->has('name')) {
echo $request->input('name');
}else{
echo "string";
}
$res = $request->all();
dd($res);
//2、判断请求类型:GET、POST
if ($request->isMethod('GET')) {
echo "Yes";
}else{
echo "No";
}
$res = $request->ajax();
var_dump($res);
//3、判断路由是否符合一定的格式
$res = $request->is('test2/*');
$res = $request->url();
var_dump($res);
}
}
2、Session
Route::group(['middleware'=>['web']],function(){
Route::any('session',['uses'=>'StudentController@sessionTest']);
});
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;
use App\Student;
use Illuminate\Http\Request;
class StudentController extends Controller
{
public function sessionTest(Request $request){
//1、HTTP request session()
$request->session()->put('key1','value1');
echo $request->session()->get('key1','default');
//2、辅助函数
session()->put('key2','value2');
echo session()->get('key2','default');
//3、Session类
Session::put('key3','value3');
echo Session::get('key3','default');
//以数组形式存储数据
Session::put(['key4'=>'value4']);
echo Session::get('key4','default');
//把数据放到Session数组中
Session::push('student','Leroi');
Session::push('student','Li');
$res = Session::get('student','default');
//取出数据并删除
$res = Session::pull('student','default');
//取出所有的值
$res = Session::all();
var_dump($res);
//判断session中某个key是否存在
if (Session::has('key8')) {
$res = Session::all();
dd($res);
}else{
dd('no');
}
//删除session中指定的key值
$res = Session::forget('key1');
var_dump($res);
//删除session所有数据
Session::flush();
//赞存数据,只能用一次
Session::flash('key-flash','val-flash');
echo Session::get('key-flash');
echo Session::get('key-flash');
}
}
3、Response
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;
use Illuminate\Contracts\Routing\ResponseFactory;
use App\Student;
use Illuminate\Http\Request;
class StudentController extends Controller
{
public function responseTest()
{
//响应json
//$data = [
// 'errCode'=>0,
// 'errMsg'=>'success',
// 'data'=>'Leroi',
//];
//return response()->json($data);
//重定向//并带上数据
//return redirect('sessionTest')->with('message','我是快闪数据');
//action()
//return redirect()->action('StudentController@sessionTest')->with('message','我是快闪数据');
//route......
//返回上一页
return redirect()->back();
}
4、中间件
https://laravel-china.org/docs/laravel/5.5/middleware/1294