Home > PHP Framework > Laravel > body text

Laravel Lecture 4: Route Naming and Route Grouping

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

Route naming

Route naming is for the program to easily obtain routing information. After obtaining this information, you can redirect or perform other operations. Routing commands are implemented through the name method. Next, let's name a route:

Route::get('about', function () {})->name('index.about');
Copy after login

After naming a route, you can get the url of the route through the assistant function route.

$url = route('index.about');
echo $url;  // http://localhost:8000/about
Copy after login

If we don’t want the front domain name part, but just want the back path, then we can do this

$url = route('index.about', [], false);
echo $url;  // /about
Copy after login

If we define a route with parameters, we can use the parameters as the route function The second parameter is passed in

Route::get('news/{id}', 'IndexController@news')->name('home.news');

...
return redirect()->route('home.news', ['id' => 23]);
Copy after login

The second parameter array element in the route function can automatically correspond to the routing parameters. If the second parameter array element in the route function is more than the parameters set by the route, it will Automatic splicing to generate URL

echo route('home.news', ['id' => 23, 'page' => 3, 'page_num' => 20]);
// http://localhost:8000/news/23?page=3&page_num=20
Copy after login

Route grouping

The role of routing grouping is that when the system has set many routes, but some routes are found to have If you have some common points, such as the same namespace prefix, or the same path prefix, you can put those routes that have common points into one route, which facilitates management and reduces the amount of code, and the code looks good. More elegant.

Route prefix

We put routes with the same prefix in a group. The following are two routes with the same prefix:

Route::prefix('index')->get('a', function () {
    return 'a'; // 匹配/index/a
});

Route::prefix('index')->get('b', function () {
    return 'b'; // 匹配/index/b
});
Copy after login

Now, we put them into a group

Route::prefix('index')->group(function ()
{
    Route::get('a', function () {
        return 'a'; // 匹配/index/a
    });
    Route::get('b', function () {
        return 'b'; // 匹配/index/b
    });
});
Copy after login

In addition to using the prefix method, you can also use the first parameter of the group method settings, as follows:

Route::group(['prefix' => 'index'], function ()
{
    Route::get('a', function () {
        return 'a'; // 匹配/index/a
    });
    Route::get('b', function () {
        return 'b'; // 匹配/index/b
    });
});
Copy after login

Namespace

Namespace is also a frequently used routing group.

Route::group(['namespace' => 'Home'], function ()
{
    Route::get('home/index', 'IndexController@index');
});
Copy after login

If the above code does not set a namespace, the above route will be

Route::get('home/index', 'Home\IndexController@index');
Copy after login

subdomain name

used Set which domain name can be accessed. If it is not this domain name, it cannot be accessed even if the path is correct.

Route::domain('php.cn')->group(...)
Copy after login

Name prefix

The name method can add a given prefix to the route in each routing group

Route::name('abc.')->group( function ()
{
    Route::get('home', function () {

    })->name('home');
});

……
$url = route('abc.home');
Copy after login

The above is the detailed content of Laravel Lecture 4: Route Naming and Route Grouping. 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 [email protected]
Popular Tutorials
More>
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!