在laravel路线中使用什么概念

时间:2018-08-11 07:08:28

标签: php laravel

我有一个源代码,我试图引用它。它具有Laravel不常见的奇怪代码。

它使用如下所示的路由:

<?php

/**
 * Global Routes
 * Routes that are used between both frontend and backend.
 */

// Switch between the included languages
Route::get('lang/{lang}', 'LanguageController@swap');

/* ----------------------------------------------------------------------- */

/*
* Frontend Routes
* Namespaces indicate folder structure
*/
Route::group(['namespace' => 'Frontend', 'as' => 'frontend.'], function () {
    includeRouteFiles(__DIR__ . '/Frontend/');
});

/* ----------------------------------------------------------------------- */

/*
* Backend Routes
* Namespaces indicate folder structure
*/
Route::group(['namespace' => 'Backend', 'prefix' => 'admin', 'as' => 'admin.', 'middleware' => 'admin'], function () {
    /*
     * These routes need view-backend permission
     * (good if you want to allow more than one group in the backend,
     * then limit the backend features by different roles or permissions)
     *
     * Note: Administrator has all permissions so you do not have to specify the administrator role everywhere.
     */
    includeRouteFiles(__DIR__ . '/Backend/');
});

includeRouteFiles帮助器功能

if (!function_exists('includeRouteFiles')) {

    /**
     * Loops through a folder and requires all PHP files
     * Searches sub-directories as well.
     *
     * @param $folder
     */
    function includeRouteFiles($folder)
    {
        try {
            $rdi = new recursiveDirectoryIterator($folder);
            $it = new recursiveIteratorIterator($rdi);

            while ($it->valid()) {
                if (!$it->isDot() && $it->isFile() && $it->isReadable() && $it->current()->getExtension() === 'php') {
                    require $it->key();
                }

                $it->next();
            }
        } catch (Exception $e) {
            echo $e->getMessage();
        }
    }
}

它在路由中具有后端,前端和面包屑文件。

路线文件的照片:

routes files

  

有人可以解释一下这是什么概念吗?

1 个答案:

答案 0 :(得分:1)

这实际上只是基本路由文件,它将寻找其他文件(在这种情况下),这些文件将在与路由相同的目录中的FrontendBackend文件夹中递归查找其他任何php文件的文件,都需要在例如

如果您的文件位于routes/Frontend/post.php

<?php

Route::resource('posts', 'PostsController);

它将将此文件加载到Route::group前端的路由文件中。

这将使您可以将路由文件分成较小的部分,而不必在基本文件中引用它们,并且还将为这些路由添加一些默认值,即对于Frontend部分应用namespace和路由名称前缀,而Backend将应用namespace,uri prefixname前缀以及middleware