Laravel is an excellent framework for PHP and is becoming more and more popular in today's web development. Laravel provides many convenient features, one of the important features is routing. So, where are Laravel's routes defined? This article will introduce the definition and use of Laravel routing in detail.
Routes in Laravel can be defined in three ways: manually defined routing, automatic routing and RESTful routing. We will introduce these three methods separately.
Manually defining routes is a widely used way, which allows us to define URIs with specific URLs. Laravel achieves this through methods in the Route
class. The Route
class is a global helper class that contains many routing-related methods. Here we implement an example of returning "Hello world" by manually defining routes.
Route::get('/hello', function () { return 'Hello World'; });
When accessing http://your-app.com/hello
, the "Hello World" message will be displayed.
In the above example, we use the HTTP GET method to obtain the URI of /hello
and return the simple string "Hello World". In addition to the GET method, there are some other HTTP methods, such as POST, PUT, DELETE, etc., which can be used according to needs.
In Laravel, you can automatically generate routes by using the Route::controller
method. This method will use the method in the controller as the URI to respond to the access request.
Route::controller('/user', 'UserController');
The above code defines a URI /user
and uses the methods in the UserController
controller to handle access requests. In the controller, we can define a series of functions that accept URIs, and these functions will be automatically routed and registered.
For example, a controller might be defined as:
class UserController extends Controller { public function getIndex() { // 返回用户首页 } public function postUser() { // 处理用户提交的表单数据 } }
With the above definition, when accessing the /user
URI, the ## of UserController
The #getIndex method will be called; and when using the POST method to access
/user/user, the
postUser method of
UserController will be called.
Route::resource method to automatically register all RESTful routes. This method is a very convenient and commonly used way to centralize all RESTful operations in a controller.
Route::resource('/user', 'UserController');
/user, and register the names
index,
in UserController
The methods create,
store,
show,
edit,
update and
destroy.
Route::get('/user/{id}', function ($id) { return 'User ' . $id; });
id. When accessing
/user/1, "User 1" will be printed; when accessing
/user/2, "User 2" will be printed.
Route::get('/user/{id}', 'UserController@show'); class UserController extends Controller { public function show($id) { return view('user.show', ['user' => User::findOrFail($id)]); } }
id and display it in the controller's
show Use it in the method to query user information in the database. By using the
findOrFail() method, it is very convenient to handle the situation where the user does not exist. At the same time, we can also pass the queried user information to the view for display and use.
The above is the detailed content of Detailed introduction to the definition and use of Laravel routing. For more information, please follow other related articles on the PHP Chinese website!