toplan / phpsms

:iphone::rocket:Probably the most intelligent, elegant sms send library in php
MIT License
642 stars 129 forks source link

lumen使用问题 #79

Closed ilovintit closed 7 years ago

ilovintit commented 7 years ago

我使用lumen做为API核心处理系统,现在想用这个类库进行短信发送,但是我发现一个问题,就是PhpSmsServiceProvider类里面返回的Sms类是没有初始化的,然后整个文档都是说用门面调用,但是lumen是默认禁用门面的,我尝试用app("PhpSms")的形式加载Sms类,但是因为没有初始化Sms,所以基本上不可用。希望后面能适配一下lumen的使用。

toplan commented 7 years ago

@ilovintit 你好,试试app("PhpSms")->make()

ilovintit commented 7 years ago

很好,经测试可以了。昨晚我折腾了一个晚上,发现在lumen里面使用还有几个问题,一个是lumen里面没有config_path函数,我参考了ide-helper的代码修改了ServiceProvider类,同时,因为lumen默认是不启用config目录下面的配置文件的,如果需要启动配置文件,需要执行$this->app->configure('phpsms');,我上网查资料发现有几种方法可以解决这个问题,一个是用$apploadComponent来加载ServiceProvider类,一个就是在ServiceProvider类里面主动调用configure方法,但是这个方法在laravel的框架下面是不存在的,我参考了laravel-wechat的代码写了修改了ServiceProvider。最后我修改后能正常运行的ServiceProvider如下,做为一个参考。

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Laravel\Lumen\Application as LumenApplication;
use Toplan\PhpSms\Sms;

class SmsServiceProvider extends ServiceProvider
{
    protected $defer = true;

    /**
     * bootstrap
     */
    public function boot()
    {
        //publish config files
        $configPath = base_path('vendor/toplan/phpsms/src/config/phpsms.php');
        if (function_exists('config_path')) {
            $publishPath = config_path('phpsms.php');
        } else {
            $publishPath = base_path('config/phpsms.php');
        }
        $this->publishes([$configPath => $publishPath], 'config');
    }

    /**
     * register service provider
     */
    public function register()
    {
        if ($this->app instanceof LumenApplication) {
            $this->app->configure('phpsms');
        }
        //merge configs
        $this->mergeConfigFrom(
            base_path('vendor/toplan/phpsms/src/config/phpsms.php'), 'phpsms'
        );

        Sms::scheme(config('phpsms.scheme', []));
        Sms::config(config('phpsms.agents', []));

        $this->app->singleton(['Toplan\\PhpSms\\Sms' => 'sms'], function () {
            return new Sms(false);
        });
    }

    public function provides()
    {
        return ['sms', 'Toplan\\PhpSms\\Sms'];
    }
}
toplan commented 7 years ago

很棒!感谢你的分享,我下来整理进去

skys215 commented 7 years ago

我之前的做法是,在app/Http下建一个文件叫Application.php 内容是

<?php namespace App;

class Application extends \Laravel\Lumen\Application
{

    public function __construct($basePath = null)
    {
        parent::__construct($basePath);
        $this->configure('sms'); //你要加载的,在config目录下的php文件名字
    }

}

然后修改bootstrap/app.php中,新建Application对象处为:

...
$app = new App\Application( //App\Application对应上面创建的namespace App下的Application类
    realpath(__DIR__.'/../')  //指向项目根目录
);
...

就可以加载config/sms.php了(对应在Application.php里引用的配置名)

toplan commented 7 years ago

感谢,终于发布新版本1.7.0,做了很多优化。

skys215 commented 7 years ago

@toplan 那个依赖问题也解决了吗?

toplan commented 7 years ago

@skys215 是的

skys215 commented 7 years ago

@toplan laravel-sms的新版预计什么时候发布呢?谢谢