dongzhi1904 2019-02-11 19:56
浏览 56
已采纳

Composer需要在不离开laravel命令行的情况下将服务注入Laravel App

So I am making a Laravel artisan command. In that command I run 'composer require' for a package. I am trying to find a way to use that package I required without leaving the current command I am in.

I thought of temporarily adding The Namespace to Laravel the way it does when you auto load files.

So my first question is how do I use this code below inside of an Artisan Command? Since app in not automatically accessible inside an artisan command.

include_once __DIR__ .'/../vendor/....';

$this->app->make('My\Namespace');

My Second Question. Assuming I get the above code to work properly inside an Artisan command, is that to unstable? Should I just make a second artisan command for after the composer require?

Any advice would be appreciated.

  • 写回答

1条回答 默认 最新

  • duanmao1319 2019-02-11 20:10
    关注

    To answer your first question, composer has its own API to do things manually what it does under the hood.

    You can required a package, then add the new Class fo usage in classmaps API using its vendor path and dump the autoloads in your command.

    Secondly, I will suggest to make 2 commands each responsible to do just one task. First to install and handle installation errors, the second to use it.

    Last but importantly, this is an unconventional way of requiring a package. Because mostly packages are required once and then may be updated/removed in the project. So you can install the package manually once and then run the command to eliminate the worry to install package using command.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!