Home >PHP Framework >Laravel >Detailed introduction to the definition and use of Laravel routing

Detailed introduction to the definition and use of Laravel routing

PHPz
PHPzOriginal
2023-04-03 20:29:01843browse

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.

Definition of routing

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

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.

Automatic routing

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.

RESTful routing

RESTful routing is a routing method that operates on resources. In Laravel, we can use the

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');
The above code will register a URI named

/user, and register the names index, in UserController The methods create, store, show, edit, update and destroy.

Use of routing

In Laravel, we can use routing parameters to define URLs and obtain variable information in the URI. We can define a routing parameter by wrapping the parameter name in curly brackets in the URI. Below is an example with routing parameters.

Route::get('/user/{id}', function ($id) {
    return 'User ' . $id;
});
In the above example, we defined a routing parameter named

id. When accessing /user/1, "User 1" will be printed; when accessing /user/2, "User 2" will be printed.

In the controller, we can also receive variable information in the URI through routing parameters. The following code shows the method:

Route::get('/user/{id}', 'UserController@show');

class UserController extends Controller
{
    public function show($id)
    {
        return view('user.show', ['user' => User::findOrFail($id)]);
    }
}
In the above example, we define a route parameter called

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.

Summary

Routing is a very important and commonly used feature in Laravel, which can easily define the relationship between URIs and controllers. With routing parameters, RESTful routing, etc., we can easily access specified resources, and the operation has become very convenient. Mastering the definition and use of Laravel routing can greatly improve our web development efficiency and code quality.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn