自定义组件服务注册配置

<?php
namespace Impecty\LaravelShop\Wap\Member\Providers;

use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use EasyWeChat\OfficialAccount\Application as OfficialAccount;

class MemberServiceProvide extends ServiceProvider
{
    // member组件需要注入的中间件
    protected $routeMiddleware = [
        'wechat.oauth' => \Overtrue\LaravelWeChat\Middleware\OAuthAuthenticate::class,
    ];

    //定义自定义artisan命令的路径
    protected $commands = [
        \Impecty\LaravelShop\Wap\Member\Console\Commands\InstallCommand::class,
    ];

    protected $middlewareGroups = [];
    //注册方法
    public function register()
    {
        //注册路由
        $this->registerRoutes();
        //加载config配置文件
        $this->mergeConfigFrom(__DIR__.'/../Config/member.php','wap.member');
        //注册路由中间件
        $this->registerRouteMiddleware();
        //注册发布配置文件方法
        $this->registerPublish();
        //重写wechat.official_account.default,使微信设置直接在wap\member配置文件中获取,而不是在.env
        $this->app->singleton("wechat.official_account.default", function ($laravelApp) {
            $app = new OfficialAccount(array_merge(config('wechat.defaults', []), config("wechat.official_account.default", [])));
            if (config('wechat.defaults.use_laravel_cache')) {
                $app['cache'] = $laravelApp['cache.store'];
            }
            $app['request'] = $laravelApp['request'];
            return $app;
        });
    }

    //执行方法
    public function boot()
    {
        $this->LoadMemberConfig();
        $this->loadMigration();
        $this->commands($this->commands);

    }

    //注册发布配置文件方法
    public function registerPublish()
    {
        if($this->app->runningInConsole()){
            $this->publishes([__DIR__.'/../Config' => config_path('wap')], 'laravel-shop-wap-member');
        }
    }

    //加载数据库迁移文件
    public function loadMigration()
    {
        if($this->app->runningInConsole()){
            $this->loadMigrationsFrom(__DIR__.'/../Database/migrations');
        }
    }

    //加载member配置文件到框架的配置文件(合并)
    public function LoadMemberConfig()
    {
        config(Arr::dot(config('wap.member.auth',[]),'auth.'));
        config(Arr::dot(config('wap.member.wechat',[]),'wechat.'));
    }

    protected function registerRouteMiddleware()
    {
        foreach ($this->middlewareGroups as $key => $middleware) {
            $this->app['router']->middlewareGroup($key, $middleware);
        }

        foreach ($this->routeMiddleware as $key => $middleware) {
            $this->app['router']->aliasMiddleware($key, $middleware);
        }
    }

    //注册路由
    private function registerRoutes()
    {
        Route::group($this->routeConfiguration(), function () {
            $this->loadRoutesFrom(__DIR__.'/../Http/routes.php');
        });
    }

    //定义路由配置
    private function routeConfiguration()
    {
        return [
            // 定义访问路由的域名
            // 'domain' => config('telescope.domain', null),
            // 是定义路由的命名空间
            'namespace' => 'Impecty\LaravelShop\Wap\Member\Http\Controllers',
            // 这是前缀
            'prefix' => 'wap/member',
            //中间件
            'middleware' => 'web',
        ];
    }
}
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!