- Previous article phP-Laravel routing usage (detailed).
A list,
-
Route parameters are configured in the previous route, but it is not suitable for carrying a large amount of request data.
Route::get('req/{parameter name}', function ($parameter name) {return 'get request '; });Copy the code
-
Will usually take at the end of the request link parameters, such as: http://www.dzm.com:8088/req?id=10&name=dzm,? The attached parameters.
-
Laravel offers a variety of ways to access data.
Ii. Method 1:Input
-
Get data from the Input class (use Illuminate\Support\Facades\Input;) Here are just a few commonly used ones.
// Get the specified parameter with the default value Input::get(' parameter name ', 'default value (effective when the parameter is not passed)'); // Get all user Input::all(); // Get Input::only([' parameter name ']); Input::except([' parameter name ']); // Check whether Input::has(' parameter name ') exists; .Copy the code
-
For example, get the ID field of the interface. If the id field does not exist, the default value is 0.
/ / such as: the Route: http://www.dzm.com:8088/req?id=10: get (' the req ', function () {return Input: : get (' id ', 0); }); Public function index() {// Can use Input return Input::has('id')? 'exist' : 'do not exist '; }Copy the code
-
Note:
Return $Input ->get('id', 0); return $Input ->get('id', 0); }); Route::get('req', function (Request $Request)) {return $Request ->get('id', 0); });Copy the code
3. Method 2:Request
-
Use the Request class to retrieve data (use Illuminate\Http\Request), which lists only the commonly-used ones.
Request::get(' parameter name ', 'default value (effective when the parameter is not passed)'); Request::all(); Request::only([' parameter name ']); Request::except([' parameter name ']); Request::has(' parameter name '); Request::isMethod(' Request method (get, POST, PUT...) ') // Determine the Request type (this can be determined when an interface supports multiple Request methods.) '); .Copy the code
-
Web.php uses:
// Use Request:: Do not import use Illuminate\Http\Request; Route::get('req', function () { return Request::has('id') ? 'exist' : 'do not exist '; }); // To use $request, you must import use Illuminate\Http\ request; Route::get('req', function (Request $request) { return $request->has('id') ? 'exist' : 'do not exist '; });Copy the code
-
The controller (for example, logincontroller.php) uses:
<? php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Input; Class LoginController extends Controller {public function index(Request $Request) { $request // return request ::has('id')? 'exist' : 'do not exist '; // Return Input::has('id')? 'exist' : 'do not exist '; Return $request->has('id')? 'exist' : 'do not exist '; }}Copy the code
-
Routing parameters – Pay attention to details
$request ::get('req/{tag? }', function ($tag = '') { return $tag; }); $request ::get('req/{tag? }', function (Request $request, $tag = '') { // return $request->has('id') ? 'exist' : 'do not exist '; return $tag; });Copy the code
Iv. Mode 3:request()
-
Request () is used as an auxiliary function, without importing any libraries or namespaces.
The request() function returns a Request object, so the methods available are the same.
Request ()->get(' parameter name ', 'default value (effective when the parameter is not passed)'); Request ()->all(); Request ()->only([' parameter name ']); Request ()->except([' parameter name ']); Request ()->has(' parameter name '); Request ()->isMethod(' request method (get, POST, put...) ') '); .Copy the code
-
If you need more methods available for this object, you can print all available methods for that object.
dump(get_class_methods(request())); Copy the code