登录  /  注册
首页 > php框架 > Laravel > 正文

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

藏色散人
发布: 2020-04-09 11:44:50
转载
2714人浏览过

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

推荐教程:《laravel/" target="_blank">laravel教程》

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

<?php
namespace 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&#39;s root namespace.
     *
     * @var string
     */
    protected $namespace = &#39;App\Http\Controllers&#39;;
    /**
     * 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(&#39;web&#39;)
             ->namespace($this->namespace)
             ->group(base_path(&#39;routes/web.php&#39;));
    }
    /**
     * Define the "api" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapApiRoutes()
    {
        Route::prefix(&#39;api&#39;)
             ->middleware(&#39;api&#39;)
             ->namespace($this->namespace)
             ->group(base_path(&#39;routes/api.php&#39;));
    }
}
登录后复制

我将与用户有关的路由抽象到了一个名为 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(&#39;api&#39;)
             ->middleware(&#39;api&#39;)
             ->namespace($this->namespace)
             ->group(base_path(&#39;routes/users.php&#39;));
    }
登录后复制

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

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

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

├── routes   
│   ├── api    
│   │   ├── public
│   |   │   ├── users.php 
│   │   ├── auth
│   |   │   ├── users.php 
│   │   ├── guest
│   |   │   ├── users.php
登录后复制

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

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

<?php
namespace 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&#39;s root namespace.
     *
     * @var string
     */
    protected $namespace = &#39;App\Http\Controllers&#39;;
    /**
     * 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(&#39;web&#39;)
            ->namespace($this->namespace)
            ->group(base_path(&#39;routes/web.php&#39;));
    }
    /**
     * Define the "api" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapAuthRoutes()
    {
        foreach (glob(base_path(&#39;routes/api/auth/*.php&#39;)) as $file) {
            Route::prefix(&#39;api&#39;)
                ->middleware([&#39;api&#39;, &#39;auth:api&#39;])
                ->group($file);
        }
    }
    protected function mapGuestRoutes()
    {
        foreach (glob(base_path(&#39;routes/api/guest/*.php&#39;)) as $file) {
            Route::prefix(&#39;api&#39;)
                ->middleware([&#39;api&#39;, &#39;guest:api&#39;])
                ->group($file);
        }
    }
    protected function mapPublicRoutes()
    {
        foreach (glob(base_path(&#39;routes/api/public/*.php&#39;)) as $file) {
            Route::prefix(&#39;api&#39;)
                ->middleware(&#39;api&#39;)
                ->group($file);
        }
    }
}
登录后复制

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

这些文件将如何注册?

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

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

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

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

以上就是一种颗粒度很小的 Laravel 路由文件划分方式(翻译)的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
相关标签:
来源:learnku网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号