Home  >  Article  >  PHP Framework  >  Laravel Lecture 3: Global constraints on routing parameters, routing redirection and routing view binding

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

齐天大圣
齐天大圣Original
2020-12-09 18:23:531996browse

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

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();
 }

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]');

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', '.*');

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');

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');

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']);

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}}

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']);
});

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!

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