如何在 Laravel 的控制器级别从所有请求对象中移除参数?
要获取 HTML 表单中的所有字段值,您可以使用 Request 类。类 Illuminate\Http\Request; 必须包含在您的控制器中。
示例 1
此示例显示了学生注册表单,其中包含姓名、电子邮件、年龄和地址等字段。
<html> <head> <title>Student Form</title> </head> <body> @if (count($errors) > 0) <div class = "alert alert-danger"> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <?php echo Form::open(array('url'=>'/student')); ?> <table border = '1'> <tr> <td align = 'center' colspan = '2'>Student Registration</td> </tr> <tr> <td>Name</td> <td><?php echo Form::text('name'); ?></td> </tr> <tr> <td>Email</td> <td><?php echo Form::text('email'); ?></td> </tr> <tr> <td>Age</td> <td><?php echo Form::text('age'); ?></td> </tr> <tr> <td>Address</td> <td><?php echo Form::text('address'); ?></td> </tr> <tr> <td align = 'center' colspan = '2'><?php echo Form::submit('submit'); ?></td> </tr> </table> </body> </html>
StudentController 类如下所示:
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Student; //use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\DB; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Storage; use URL; use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Validator; use Illuminate\Validation\Rule; class StudentController extends Controller{ public function index(Request $request) { return view('student'); } public function validateform(Request $request) { print_r($request->all()); } }
在 routes/web.php 中,路由定义如下:
use App\Http\Controllers\StudentController; Route::get('student', [StudentController::class, 'index']); Route::post('student',[StudentController::class, 'validateform']);
输出
在控制器中,我们打印用户输入的所有字段。输出如下所示:
在表单中输入详细信息,然后单击提交,您应该会看到以下详细信息:
Array ( [_token] => nUXVJ5adsFLADKC9RCbAJcdfYCRquy8GUkUCXtR8 [name] => Rasika Desai [email] => rasika@gmail.com [age] => 20 [address] => Pune )
该数组包含输入字段的所有详细信息
示例 2
现在让我们尝试从请求对象中移除一个字段。为此,您可以使用 unset() 方法。它是一个内置的 PHP 函数。让我们尝试移除 age 字段并再次打印数组。
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Student; use Illuminate\Support\Facades\DB; class StudentController extends Controller { public function index(Request $request) { return view('student'); } public function validateform(Request $request) { print_r($request->all()); unset($request['age']); print_r($request->all()); } }
输出
使用 unset($request['age']); 从请求对象中移除 age 字段,如下面的输出所示:
Array( [_token] => nUXVJ5adsFLADKC9RCbAJcdfYCRquy8GUkUCXtR8 [name] => Rasika Desai [email] => rasika@gmail.com [age] => 20 [address] => Pune ) Array( [_token] => nUXVJ5adsFLADKC9RCbAJcdfYCRquy8GUkUCXtR8 [name] => Rasika Desai [email] => rasika@gmail.com [address] => Pune )
示例 3
您可以使用 except() 方法。在此方法中,您需要传递要移除的键值。
此方法返回一个数组,其中包含除作为 except() 方法参数传递的字段之外的所有字段。
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Student; class StudentController extends Controller { public function index(Request $request) { return view('student'); } public function validateform(Request $request) { print_r($request->all()); $data = $request->except(['age']); print_r($data); } }
输出
以上代码的输出为:
Array ( [_token] => nUXVJ5adsFLADKC9RCbAJcdfYCRquy8GUkUCXtR8 [name] => Rasika Desai [email] => rasika@gmail.com [age] => 20 [address] => Pune ) Array ( [_token] => nUXVJ5adsFLADKC9RCbAJcdfYCRquy8GUkUCXtR8 [name] => Rasika Desai [email] => rasika@gmail.com [address] => Pune )
示例 4
您还可以使用 $request->only() 方法。此方法获取要保留的键,其余键将被丢弃。
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Student; class StudentController extends Controller { public function index(Request $request) { return view('student'); } public function validateform(Request $request) { print_r($request->all()); $data = $request->only(['name', 'email']); print_r($data); } }
输出
以上代码的输出如下所示:
Array ( [_token] => nUXVJ5adsFLADKC9RCbAJcdfYCRquy8GUkUCXtR8 [name] => Rasika Desai [email] => rasika@gmail.com [age] => 20 [address] => Pune ) Array ( [name] => Rasika Desai [email] => rasika@gmail.com )
广告