承接 elfsundae/laravel-apps 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

elfsundae/laravel-apps

Composer 安装命令:

composer require elfsundae/laravel-apps

包简介

Laravel multi-application support.

README 文档

README

Latest Version on Packagist Software License tests StyleCI SymfonyInsight Grade Quality Score Code Coverage Total Downloads

This package provides basic support for Laravel multi-application.

Installation

You can install this package using the Composer manager:

$ composer require elfsundae/laravel-apps

For earlier Laravel than v5.5, you need to register the service provider manually:

ElfSundae\Apps\AppsServiceProvider::class,

Then publish the configuration file to your config directory:

$ php artisan vendor:publish --tag=laravel-apps

Configuration

The url configuration option defines the root URL of each application:

'url' => [
    'web' => 'https://example.com',
    'admin' => 'https://example.com/admin',
    'mobile' => 'https://m.example.com',
    'api' => 'https://api.example.com',
    'assets' => 'https://assets.foo.net',
],

The providers array lists the class names of service providers for each application, you may configure this to selectively register service providers.

The config option may be used to override the default configurations for each application. Additionally, you may wish to put all of your application defaults in one place instead of editing separate configuration files, just put them in the default key:

'config' => [

    'default' => [
        'app.timezone' => 'Asia/Shanghai',
        'app.log' => env('APP_LOG', 'daily'),
        'app.log_max_files' => 50,
        'filesystems.disks.public.url' => env('APP_URL_ASSETS', env('APP_URL')).'/storage',

        'debugbar.options.auth.show_name' => false,
        'debugbar.options.route.label' => false,
    ],

    'admin' => [
        'auth.defaults' => [
            'guard' => 'admin',
            'passwords' => 'admin_users',
        ],
        'filesystems.default' => 'public',
        'session.domain' => env('SESSION_DOMAIN_ADMIN', null),
    ],

    'api' => [
        'auth.defaults.guard' => 'api',
        'filesystems.default' => 's3',
    ],

],

Usage

Obtain Application Manager Instance

You may obtain the application manager instance using the Apps facade, the apps() helper function or injecting the ElfSundae\Apps\AppManager dependency.

Retrieve Application URL Configuration

use ElfSundae\Apps\Facades\Apps;

// Get all application URLs
Apps::urls();

// Get all application identifiers
apps()->ids();

// Get URL root for the assets app
apps()->root('assets');

// Get URL domain for the api app
apps()->domain('api');

// Get URL prefix for the admin app
apps()->prefix('admin');

Determine The Current Application Identifier

The application identifier to the current request can be determined via the id method on the app manager, or using the corresponding app_id helper function:

$appId = Apps::id();

$appId = app_id();

You may also pass arguments to the id method to check if the current application identifier matches a given value. The method will return true if the identifier matches any of the given values:

if (Apps::id('admin')) {
    // Currently requesting admin app
}

if (app_id('web', 'admin')) {
    // Currently requesting either web app OR admin app
}

Selectively Register Service Providers

Instead of adding all service providers to the config/app.php file, you may want to selectively register service providers for certain sub applications to optimize performance. To do so, simply list the providers to the providers array in the config/apps.php configuration file:

'providers' => [

    'admin' => [
        Rap2hpoutre\LaravelLogViewer\LaravelLogViewerServiceProvider::class,
        Yajra\DataTables\DataTablesServiceProvider::class,
        App\Providers\AdminServiceProvider::class,
    ],

    'api' => [
        App\Providers\ApiServiceProvider::class,
    ],

],

⚠️ If your application runs on Laravel 5.5+ which support package discovery, you also need to disable discovery for the optional packages in the composer.json file:

"extra": {
    "laravel": {
        "dont-discover": [
            "rap2hpoutre/laravel-log-viewer",
            "yajra/laravel-datatables-oracle"
        ]
    }
}

Don't worry about the deferred service providers, as the deferred providers are only loaded when needed.

Define Application Routes

The routes method on the app manager helps you define route group for each application. In general, you will call it in the map method of your RouteServiceProvider:

class RouteServiceProvider extends ServiceProvider
{
    protected $namespace = 'App\Http\Controllers';

    /**
     * Define the routes for the application.
     *
     * @return void
     */
    public function map()
    {
        apps()->routes();
    }
}

The route files named with the application identifiers in the routes directory will be automatically loaded, such as routes/web.php, routes/admin.php.

By default, the routes method will assign the existing middleware group named with the application identifier or web to the route group, and the namespace applied to your controller routes will be StudlyCase of the application identifier.

For example, apps()->routes() is equivalent to:

// web: https://example.com
Route::group([
    'domain' => 'example.com',
    'middleware' => 'web',
    'namespace' => $this->namespace.'\Web',
], function ($router) {
    require base_path('routes/web.php');
});

// api: https://api.example.com
Route::group([
    'domain' => 'api.example.com',
    'middleware' => 'api',
    'namespace' => $this->namespace.'\Api',
], function ($router) {
    require base_path('routes/api.php');
});

// admin: https://example.com/admin
Route::group([
    'domain' => 'example.com',
    'prefix' => 'admin',
    'middleware' => 'web', // suppose if the "admin" middleware group does not exist
    'namespace' => $this->namespace.'\Admin',
], function ($router) {
    require base_path('routes/admin.php');
});

// ...

Of course, you are free to specify any route attributes:

apps()->routes([
    'web' => [
        'namespace' => $this->namespace,
    ],
    'admin' => [
        'middleware' => ['web', 'admin.ip'],
        'as' => 'admin.',
        'where' => [
            'id' => '[0-9]+',
        ],
    ],
]);

In addition to an array, you can pass a Closure to the routes method:

apps()->routes(function ($id, $apps) {
    return ['as' => $id.'.'];
});

Generate URL

You can use the url method or the corresponding app_url helper function to generate an absolute URL to a path for a specified application:

apps()->url('admin', 'user', [$user]); // https://example.com/admin/user/123

app_url('api', 'posts'); // https://api.example.com/posts

The asset method generates a URL with the root URL of the assets application:

apps()->asset('js/app.js'); // https://assets.foo.net/js/app.js

Custom Root URL For asset() Helper

The Laravel built-in URL::asset method or the corresponding asset, secure_asset helper functions are designed to generate URL for the application assets. In most applications, we will probably specify a cookie-free domain or use CDN for the assets, however we can not set custom root URL for these built-in assets methods, and for now there is no elegant way to extend the core UrlGenerator.

You may use URL::assetFrom, Apps::asset, or a custom helper function to generate assets URLs, but it is awfully boring to replace all asset() calls to your own assets method for the third-party packages. Maybe a better workaround is overwriting the built-in asset helper: define your asset function before including the Composer autoloader file, in your public/index.php file:

function asset($path, $secure = null)
{
    return apps()->asset($path, $secure);
}

require __DIR__.'/../vendor/autoload.php';

This package ships with an asset.php file you may include to use the root URL of the assets application for the asset() helper:

require __DIR__.'/../vendor/elfsundae/laravel-apps/asset.php';

require __DIR__.'/../vendor/autoload.php';

FYI, related PR laravel/framework#22372.

Extend Application Manager

The AppManager class is macroable, that means you can use the macro method to extend it:

Apps::macro('route', function ($name, $parameters = []) {
    return URL::route($this->id().'.'.$name, $parameters);
});

Testing

$ composer test

License

This package is open-sourced software licensed under the MIT License.

elfsundae/laravel-apps 适用场景与选型建议

elfsundae/laravel-apps 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10.51k 次下载、GitHub Stars 达 84, 最近一次更新时间为 2017 年 11 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「assets」 「application」 「multiple」 「laravel」 「domain」 「multi」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 elfsundae/laravel-apps 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 elfsundae/laravel-apps 我们能提供哪些服务?
定制开发 / 二次开发

基于 elfsundae/laravel-apps 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 10.51k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 84
  • 点击次数: 13
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 84
  • Watchers: 5
  • Forks: 11
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-11-30