示例6-1 $request->all()
<!-- GET route form view at /get-route -->
<form method="post" action="/post-route?utm=12345">
{{ csrf_field() }}
<input type="text" name="firstName">
<input type="submit">
</form>
Route::post('/post-route', function(Request $request){
var_dump($request->all());
});
//输出
/**
*[
* '_token' => 'CSRF token here'.
* 'firstName' => 'value',
* 'utm' => 12345
*]
*/
示例6-2 $request->except()
Route::post('/post-route', function(Request $request){
var_dump($request->except('_token'));
});
//输出
/**
*[
* 'firstName' => 'value',
* 'utm' => 12345
*]
*/
示例6-3 $request->only()
Route::post('/post-route', function(Request $request){
var_dump($request->only(['firstName','utm']));
});
//输出
/**
*[
* 'firstName' => 'value',
* 'utm' => 12345
*]
*/
示例6-4 $request->has()
//通过/post->route传递路由
if($request->has('utm')){
//进行分析
}
示例6-5 $request->input()
Route::post('/post-route', function(){
$userName = $request->input('name', '(anonymous)');
});
示例6-6 通过点标记法在用户数据中访问数组值
<!-- GET route form view at /get-route -->
<form method="post" action="/post-route">
{{ csrf_field() }}
<input type="text" name="employees[0][firstName]">
<input type="text" name="employees[0][lastName]">
<input type="text" name="employees[1][firstName]">
<input type="text" name="employees[1][lastName]">
<input type="submit">
</form>
//通过 /post-route 传递路由
Route::post('/post-route', function(Request $request){
$employeeZeroFirstName = $request->input('employees.0.firstName');
$allLastNames = $request->input('employees.*.lastName');
$employeeOne = $request->input('employees.1');
});
示例6-7 使用$request->input()从json获取数据
POST /post-route HTTP/1.1
Content-Type: application/json
{
"firstName": "Joe",
"lastName": "schmoe",
"spouse":{
"firstName": "jill",
"lastName": "Schmoe"
}
}
//post-route
Route::post('post-route', function(Request $request){
$firstName = $request->input('firstName');
$spouseFirstname = $request->input('spouse.firstName');
});
示例6-8 从路由参数获取URL详细信息
//routes/web.php
Route::get('users/{id}', function($id){
//如果用户访问 /users/15/, $id 等同于 15
});
示例6-9 上传文件的表单
<form method="post" enctype="multipart/form-data">
{{ csrf_field() }}
<input type="text" name="name">
<input type="file" name="profile_picture">
<input type="submit">
</form>
示例6-10 提交示例6-9中的表单后的输出
Route::post('form', function(Request $request){
var_dump($request-all());
});
//输出
//[
// "_token" => "token here"
// "name" => "asdf"
// "profile_picture" => UploadedFile()
//]
Route::post('form', function(Request $request){
if( $request0<hasFile('profile_picture')){
var_dump($request0<file('profile_picture'));
}
});
//输出
// UploadedFile(details)
if($request->hasFile('profile_picture')){
$path = $request->profile_picture->store('profile', 's3');
auth()->user()->profile_picture = $path;
auth()->user()->save();
}
示例6-12 控制器验证
//Routes/web.php
Route::get('recipes/create', 'RecipesController@create');
Route::post('recipes', 'RecipesController@store');
// app/Http/Controllers.RecipesController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class RecipesController extends Controller{
public function create(){
return view('recipes.create');
}
public function store(Request $request){
$this->validate($request, [
'title' => 'required | unique:recipes |max:125',
'body' => 'required'
]);
}
}
示例6-13 手动验证
Route::get('recipes/create', function(){
return view('recipes.create');
});
Route::post('recipes', function(Illuminate\Http\Request $request){
$validator = Validator::make($request->all(),[
'title' => 'required|unique:recipes|max:125',
'body' => 'required'
]);
if($validator->fails()){
return redirect('recipes/create')
->withErrors($validator)
->withInput();
}
});
示例6-14 输出验证错误
@if ($errors->any())
<ul id="errors">
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
@endif
示例6-15 表单请求示例
<?php
namespace App\Http\Requests;
use App\BlogPost;
use App\Http\Requests\Request;
class CreateCommentRequest extends Request{
public function rules(){
return [
'body' => 'required|max:1000'
];
}
public function authorize(){
$blogPostId = $this->route('blogPost');
return auth()->check() && BlogPost::where('id', $blogPostId)
->where('user_id', auth()->user()->id)->exists();
}
}
示例6-16 使用表单请求
Route::post('comments', function(App\Http\Requests\CreateCommentRequest $request){
//Store comment
});
示例6-17 将整个表单传递给Eloquent模型
Route::post('posts', function(Request $request){
$newPost = Post::create($request->all());
});
示例6-18 从恶意质量分配中保护Eloquent模型
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model{
//在author_id字段上禁用质量分配
protected $guarded = ['author_id'];
}
示例6-19 测试无效输入并被拒绝
public function test_input_missing_a_title_is_rejected(){
$this->post('posts', ['body' => 'This is the body of my post']);
$this->assertRedirectedTo('posts/create');
$this->assertSessionHasErrors();
$this->assertHasOldInput();
}
示例6-20 测试有效输入并对输入进行处理
public function test_valid_input_should_create_a_post_in_the_database(){
$this->post('posts', ['title' => 'Post Title', 'body' => ' This is the body']);
$this->seeInDatabase(['title' => 'Post Title']);
}