Home >Backend Development >PHP Tutorial >Understanding Laravels Routing System [Laravel beginner series | Part 2]
? The second article in the Laravel newbie series is here! Let’s get straight to the point!
The routing mechanism in Laravel defines how the application responds to user requests. When a user visits a URL or submits a form, Laravel's routing system decides what to do next. This might include returning a view, performing controller actions, or processing data.
Laravel’s routing is handled in the routes folder, especially in files such as web.php and api.php.
The route for web requests is defined in the routes/web.php file, and the route for API requests is defined in the routes/api.php file.
<code>Route::get('/', function () { return view('welcome'); });</code>
Suppose you are building a blogging application where users can view a list of posts, view details of individual posts, and add new posts.
Here, the /posts route gets the list of posts and passes them to the posts.index view.
<code>Route::get('/posts', function () { $posts = [ ['id' => 1, 'title' => '第一篇文章', 'content' => '这是第一篇文章的内容。'], ['id' => 2, 'title' => '第二篇文章', 'content' => '这是第二篇文章的内容。'] ]; return view('posts.index', ['posts' => $posts]); });</code>
Here, the /posts/{id} route dynamically gets the post by ID and returns the detailed view. Show Laravel's default 404 error if the post does not exist.
<code>Route::get('/posts/{id}', function ($id) { $posts = [ 1 => ['title' => '第一篇文章', 'content' => '这是第一篇文章的内容。'], 2 => ['title' => '第二篇文章', 'content' => '这是第二篇文章的内容。'] ]; if (!isset($posts[$id])) { abort(404, '帖子未找到'); } return view('posts.show', ['post' => $posts[$id]]); });</code>
/posts route handles form submissions for adding new posts.
<code>Route::post('/posts', function () { // 添加新帖子的逻辑 return '已添加新帖子!'; });</code>
Laravel provides various methods to define routes for different HTTP verbs.
<code>Route::post('/submit', function () { return '表单已提交!'; }); Route::put('/update', function () { return '数据已更新!'; }); Route::delete('/delete', function () { return '数据已删除!'; });</code>
Using controllers helps keep routing files simple and easy to maintain. Here, UserController@index handles the logic of the /users route.
<code>Route::get('/users', [UserController::class, 'index']);</code>
Named routes allow you to reference routes by name instead of URL, which is especially useful when the URL changes.
<code>Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');</code>
You can then generate the URL for this route:
<code>$url = route('dashboard');</code>
You can pass dynamic parameters in routes.
Required parameters
<code>Route::get('/user/{id}', function ($id) { return '用户ID:' . $id; });</code>
Optional parameters
<code>Route::get('/', function () { return view('welcome'); });</code>
Middleware can restrict access or perform operations before processing the request.
<code>Route::get('/posts', function () { $posts = [ ['id' => 1, 'title' => '第一篇文章', 'content' => '这是第一篇文章的内容。'], ['id' => 2, 'title' => '第二篇文章', 'content' => '这是第二篇文章的内容。'] ]; return view('posts.index', ['posts' => $posts]); });</code>
In this example, only authenticated users can access the /profile route.
Route groups allow you to apply shared properties such as middleware or prefixes. Here, all routes within the group share the admin prefix and auth middleware.
<code>Route::get('/posts/{id}', function ($id) { $posts = [ 1 => ['title' => '第一篇文章', 'content' => '这是第一篇文章的内容。'], 2 => ['title' => '第二篇文章', 'content' => '这是第二篇文章的内容。'] ]; if (!isset($posts[$id])) { abort(404, '帖子未找到'); } return view('posts.show', ['post' => $posts[$id]]); });</code>
Laravel can automatically bind routing parameters to Eloquent models. In this case, Laravel retrieves the Post model corresponding to the {post} parameter.
Laravel easily tests routes using its built-in tools. You can use php artisan route:list to view all defined routes in your application. This command displays details such as routing method, URI, and assigned middleware.
<code>Route::post('/posts', function () { // 添加新帖子的逻辑 return '已添加新帖子!'; });</code>
Laravel's routing system is a powerful and flexible tool that simplifies web development. By understanding the basics and taking advantage of advanced features such as middleware, named routing, and routing model binding, you can create applications that are concise, efficient, and easy to maintain.
Laravel has great and very well done documentation here.
PS: If you found this post helpful, please like and share it! If you have any questions for me, please leave a message! I'd love to chat with you! :)
Our next article will be out next week!
The above is the detailed content of Understanding Laravels Routing System [Laravel beginner series | Part 2]. For more information, please follow other related articles on the PHP Chinese website!