We know that routers in the network are used to select paths and forward packets, so what are the routes in the framework used for? The route here is the path used to receive http requests, and you can access the functions of a specific program by setting the path. Routing is one of the core functions of the framework, and basically all mainstream frameworks have routing functions. Moreover, we can set up routing to convert some long, deep paths into short, shallow paths (a feature that is very beneficial to SEO).
Default routing file
All laravel routes are defined in routing files in the routes directory of the root directory. The framework automatically loads this directory by default. file under. Let’s take a look at the routes/web.php file
use Illuminate\Support\Facades\Route; Route::get('/', function () { return view('welcome'); });
Basic routing
Now, we create a new one in the web.php file Simple routing, the code is as follows:
Route::get('index', function () { return "study laravel"; });
Now, when we enter localhost:8000/index in the browser, we can see the sentence "study laravel".
Common routing methods
In addition to the above get method, the commonly used routing methods are: post, put, delete, any, match .
The any method here means that no matter what the request method is, it can be get/post/put and other request methods, and it will handle them all. Match can be used to specify multiple requests. The following demonstrates the usage of match:
Route::match(['get', 'post'], "list", function () { return 'list'; });
Parameter passing
Often, we are routing Parameters will also be added. For example, /news/1 is very common. Lavarel's routing method supports the passing of parameters, such as:
Route::get('news/{id}', function ($id) { return 'news:' . $id; });
Although the above route can pass parameters, it still cannot meet my needs. I hope that the {id} parameter must be a number, so it can be like this:
Route::get('news/{id}', function ($id) { return 'news:' . $id; })->where('id', '\d+');
We can also pass multiple parameters, and the parameters in the closure will automatically correspond one to one, such as:
Route::get('/name/{name}/age/{age}', function($name, $age) { echo "name:$name age:$age"; })->where(['name' => '\w+', 'age' => '\d+']);
Route and controller binding
First, we create a controller file app\Http\Controllers\IndexController.php, the code is as follows:
<?php namespace App\Http\Controllers; class IndexController extends Controller { public function news ($id) { echo "news:$id"; } }
Now modify the routing file web.php
Route::get('news/{id}', 'IndexController@news')->where('id', '\d+');
Now you can access the news method in the IndexController controller through /news/1.
If app/Http/Controllers/Home/IndexController.php is created, how to set the routing of this controller? The method is as follows:
Route::get('home', 'Home\IndexController@index');
The above is the detailed content of Laravel Lecture 2: Definition of Route and Binding to Controller. For more information, please follow other related articles on the PHP Chinese website!