一种颗粒度很小的 Laravel 路由文件划分方式(翻译)

一种颗粒度很小的 Laravel 路由文件划分方式(翻译),第1张

概述一种颗粒度很小的 Laravel 路由文件划分方式(翻译) 我估计我们所有人都遇到过这样的情况,即我们有一个写满路由的超大文件。不骗你,这让我很长一段时间几近抓狂,我不得不想个办法解决这个问题。 因此,这就是我最终用来构造路由文件的方法。

推荐教程:《laravel教程》

最初,我想到了利用路由组方法可以接收文件,这就是 laravel 在 RouteServiceProvIDer 处拆分路由的方式。

<?PHPnamespace App\ProvIDers;use Illuminate\Foundation\Support\ProvIDers\RouteServiceProvIDer as ServiceProvIDer;use Illuminate\Support\Facades\Route;class RouteServiceProvIDer extends ServiceProvIDer{    /**     * This namespace is applIEd to your controller routes.     *     * In addition, it is set as the URL generator's root namespace.     *     * @var string     */    protected $namespace = 'App\http\Controllers';    /**     * define your route model bindings, pattern filters, etc.     *     * @return voID     */    public function boot()    {        //        parent::boot();    }    /**     * define the routes for the application.     *     * @return voID     */    public function map()    {        $this->mapAPIRoutes();        $this->mapWebroutes();        //    }    /**     * define the "web" routes for the application.     *     * These routes all receive session state, CSRF protection, etc.     *     * @return voID     */    protected function mapWebroutes()    {        Route::mIDdleware('web')             ->namespace($this->namespace)             ->group(base_path('routes/web.PHP'));    }    /**     * define the "API" routes for the application.     *     * These routes are typically stateless.     *     * @return voID     */    protected function mapAPIRoutes()    {        Route::prefix('API')             ->mIDdleware('API')             ->namespace($this->namespace)             ->group(base_path('routes/API.PHP'));    }}

我将与用户有关的路由抽象到了一个名为 users.PHP 的文件中,并将 mapAPIRoutes 复制为 mapUsersRoutes 并指向到我的 users.PHP 文件。

 /**     * define the routes for the application.     *     * @return voID     */    public function map()    {        $this->mapAPIRoutes();        $this->mapWebroutes();        $this->mapUsersRoutes();        //    }/**     * define the "API" routes for the application.     *     * These routes are typically stateless.     *     * @return voID     */    protected function mapUsersRoutes()    {        Route::prefix('API')             ->mIDdleware('API')             ->namespace($this->namespace)             ->group(base_path('routes/users.PHP'));    }

我知道您在想什么,显然,这并不是最好的解决方案,因为每当我们需要创建新文件时,都必须像之前一样注册它。 因此,我不得不改进这个最初的想法。

我想到了将整个应用程序中的公共部分拆分成单独的路由文件,并且我想到我们的所有路由都不能超出已认证、访客和公共路由的范围。

我将路由文件夹的结构优化成下面这样:

├── routes   │   ├── API    │   │   ├── public│   |   │   ├── users.PHP │   │   ├── auth│   |   │   ├── users.PHP │   │   ├── guest│   |   │   ├── users.PHP

乍一看,您可能会认为 “嗯,它并没有太大变化,我们还是需要去映射这些文件”。 但是,实际上我们可以利用 PHP 原生提供的名为 glob 的函数,这是一种开箱即用的解决方案,因为我们没有与 laravel 的解决方案耦合。

glob 接收一个正则,并且可以在与我们的正则匹配的路径下找到文件名。 因此,我们的路由是在特定文件夹下组织的,我们现在可以在这些文件夹下找到所有文件,并将它们注册到其中间件。

<?PHPnamespace App\ProvIDers;use Illuminate\Foundation\Support\ProvIDers\RouteServiceProvIDer as ServiceProvIDer;use Illuminate\Support\Facades\Route;class RouteServiceProvIDer extends ServiceProvIDer{    /**     * This namespace is applIEd to your controller routes.     *     * In addition, it is set as the URL generator's root namespace.     *     * @var string     */    protected $namespace = 'App\http\Controllers';    /**     * define the routes for the application.     *     * @return voID     */    public function map()    {        $this->mapAuthRoutes();        $this->mapGuestRoutes();        $this->mapPublicRoutes();//        $this->mapWebroutes();        //    }    /**     * define the "web" routes for the application.     *     * These routes all receive session state, CSRF protection, etc.     *     * @return voID     */    protected function mapWebroutes()    {        Route::mIDdleware('web')            ->namespace($this->namespace)            ->group(base_path('routes/web.PHP'));    }    /**     * define the "API" routes for the application.     *     * These routes are typically stateless.     *     * @return voID     */    protected function mapAuthRoutes()    {        foreach (glob(base_path('routes/API/auth/*.PHP')) as $file) {            Route::prefix('API')                ->mIDdleware(['API', 'auth:API'])                ->group($file);        }    }    protected function mapGuestRoutes()    {        foreach (glob(base_path('routes/API/guest/*.PHP')) as $file) {            Route::prefix('API')                ->mIDdleware(['API', 'guest:API'])                ->group($file);        }    }    protected function mapPublicRoutes()    {        foreach (glob(base_path('routes/API/public/*.PHP')) as $file) {            Route::prefix('API')                ->mIDdleware('API')                ->group($file);        }    }}

现在,无论何时我们创建一个新文件,foreach 都将找到它,因为它是使用正则匹配(该文件位于对应的路径下,并且具有 PHP 扩展名,因此它与我们的正则匹配)。简直太骚了!但是请稍等片刻。

这些文件将如何注册?

如果您研究过 laravel 的生命周期,您就知道服务提供者是 laravel 请求的生命周期的一部分,我们可以利用此功能动态注册我们的路线。

就是这样!我希望您喜欢它。

原文地址:https://dev.to/secmohammed/how-to-separa...

译文地址:https://learnku.com/laravel/t/42989

总结

以上是内存溢出为你收集整理的一种颗粒度很小的 Laravel 路由文件划分方式(翻译)全部内容,希望文章能够帮你解决一种颗粒度很小的 Laravel 路由文件划分方式(翻译)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1231290.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存