Home > PHP Framework > Laravel > Laravel Lecture 2: Definition of Route and Binding to Controller

Laravel Lecture 2: Definition of Route and Binding to Controller

齐天大圣
Release: 2020-12-09 11:44:04
Original
1939 people have browsed it

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');
});
Copy after login

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";
});
Copy after login

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';
});
Copy after login

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;
});
Copy after login

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+');
Copy after login

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+']);
Copy after login

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";
    }
}
Copy after login

Now modify the routing file web.php

Route::get(&#39;news/{id}&#39;, &#39;IndexController@news&#39;)->where(&#39;id&#39;, &#39;\d+&#39;);
Copy after login

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(&#39;home&#39;, &#39;Home\IndexController@index&#39;);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template