一、Controller介绍
1) Controller之Request
laravel中的请求使用的是symfony/http-foundation组件
请求里面存放了$_GET $_POST $_COOKIE $_FILES $_SERVER等数据
eg1:
use IlluminateHttpRequest;
class ExampleController extends Controller {
public function request1(Request $request) {
#1: 取值
echo $request->input('name'); //获取参数
echo $request->input('sex', '未获取到数据,这是默认值'); //如果没有这个参数,将采用默认值
echo $request->has('sex') ? $request->input('sex') : '无参数' ; //判断是否存在这个参数,存在输出,否则默认值
$request->all(); //获取所有的参数
#2: 判断请求类型
var_dump($request->method()); //请求类型
var_dump($request->isMethod('GET')); //是否是get请求
var_dump($request->ajax()); //是否是ajax请求
var_dump($request->is('student/*')); //判断访问路劲是否是这个
var_dump($request->url()); //获取当前的url
}
}
eg2:
namespace AppHttpControllers;
use IlluminateHttpRequest;
class UploadController extends Controller {
public function save(Request $request) {
echo $request->input('file');
}
}
Route::any('upload/save', 'UploadController@save');
<form action="upload/save" method="post">
{{ csrf_field() }}
<input type="text" name="file" value="123">
<input type="submit" value="提交">
</form>
eg3:
namespace AppHttpControllers;
use IlluminateHttpRequest;
class RbacController extends BaseController
{
public function roleset(Request $request)
{
echo json_encode($request->input('rolename'));
}
}
$.ajax({
type: 'POST',
url: '/roleset',
data: { rolename : $('#rolename').val()},
dataType: 'json',
headers: {
'X-CSRF-TOKEN': "{{ csrf_token() }}"
},
success: function(data){
}
});
Route::any('/roleset', 'RbacController@roleset');
2) Controller之Session
laravel中session三种使用方式:
a: HTTP request类的session()方法
b: session()辅助函数
c: Session facade
Controller之Response
Controller之Middleware