Home> PHP Framework> Laravel> body text

Laravel Lecture 3: Global constraints on routing parameters, routing redirection and routing view binding

齐天大圣
Release: 2020-12-10 09:09:52
Original
1903 people have browsed it

A previous article has already explained some knowledge about routing, and I will continue to add a few points today.

Global constraints of routing parameters

We already know that we can use the where method to constrain parameters, as follows:

Route::get('news/{id}', function ($id) { echo 'news:' . $id; })->where('id', '[0-9]+'); Route::get('list/{id}', function ($id) { echo 'list:' . $id; })->where('id', '[0-9]+');
Copy after login

About This ID is used by multiple routes and the constraints are the same. Then, we can constrain ids on a global scale. In this way, local routing does not need to be constrained, and the code will not be redundant.

Now, let’s edit the boot method in app/Providers/RouteServiceProvider.php and add a line of code, as follows:

public function boot() { Route::pattern('id', '[0-9]+'); parent::boot(); }
Copy after login

Once defined, these rules will be automatically applied to all on the route using this parameter name. But if I want to cancel this restriction or reset parameter rules on individual routes, how should I do it?

To reset, just reset the rules in the where method

Route::get('news/{id}', function ($id) { echo 'news:' . $id; })->where('id', '[a-z]');
Copy after login

The method to cancel the restriction is actually to reset the rules, but use .* in the rules to regularize any characters

Route::get('news/{id}', function ($id) { echo 'news:' . $id; })->where('id', '.*');
Copy after login

Route redirection

We can jump from one route to another address or another route, just use the redirect method of Route

// 跳转到php中文网 Route::redirect('index', ' // 跳转到本站另一个路由 Route::redirect('a', 'news/1');
Copy after login

The default route redirection uses 302 temporary redirection. If you want to set up a 301 permanent redirection, you need to set the third parameter or use another method, which is permanentRedirect.

Route::redirect('b', 'news/1', 301); Route::permanentRedirect('c', 'news/2');
Copy after login

Routing and view binding

Routing can also be directly bound to the view without going through the controller. Here we need to use the view method, which has three parameters: uri (required parameters), view name (required parameters), and parameters (optional parameters).

Route::view('vtest', 'view1', ['str' => 'study laravel']);
Copy after login

Now let’s create a view file named view1 and create a view1.blade.php file in the resources/views directory. The content of the file is as follows:

view1{{$str}}
Copy after login

In addition to using Route::view In addition, you can also use the global view function in the closure of get or other methods to achieve the same effect.

Route::get('vtest', function () { return view('view1', ['str' => 'study laravel2']); });
Copy after login

The above is the detailed content of Laravel Lecture 3: Global constraints on routing parameters, routing redirection and routing view binding. 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!