webman-tech / laravel-console

Laravel illuminate/console for webman
4 stars 0 forks source link

如何引入Laravel Iseed,用了这个发现找不到命令? #7

Closed dmskys closed 6 months ago

dmskys commented 6 months ago

如何引入Laravel Iseed,用了这个发现找不到命令? https://packagist.org/packages/orangehill/iseed

krissss commented 6 months ago

使用 命令扫描自定义命令

dmskys commented 6 months ago

新建自定义命令,还是无法调用。

<?php

namespace app\command;

use Illuminate\Support\Facades\Artisan;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class DbIseed extends Command
{
    protected static $defaultName = 'db:iseed';
    protected static $defaultDescription = 'db iseed';

    /**
     * @return void
     */
    protected function configure()
    {
        $this->addArgument('table', InputArgument::OPTIONAL, 'Name description');
    }

    /**
     * @param InputInterface $input
     * @param OutputInterface $output
     * @return int
     */
    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $table = $input->getArgument('table');
        $output->writeln('Hello db:iseed ' . $table);
        Artisan::call("iseed ", $table);
        return self::SUCCESS;
    }

}

提示错误如下:

$ php webman db:iseed admin_menu
Hello db:iseed admin_menu

In Facade.php line 335:

  A facade root has not been set.  

db:iseed [<table>]
krissss commented 6 months ago
  1. webman 下无法使用 laravel 的 facade
  2. 自定义命令可以直接引入第三方的命令,比如这个的 Orangehill\Iseed\IseedCommand 不需要再复制写一遍了
  3. 需要使用 php artisan iseed 来执行命令

其他:看了下这个包下的命令IseedCommand,有些强依赖于 laravel 的,如果要使用可能需要改造,粗看下这几处应该会有问题:

  1. app('iseed'),webman 下没有这个 app 函数
  2. config('iseed::config.path'),webman 下获取不到对应路径

解决办法:

  1. 继承 IseedCommand,覆盖其中不对的地方
  2. 全局提供 app 函数,使得 app('iseed') 返回 Iseed 实例,修改全局的 config 函数,使得 config('iseed::config.path') 返回 seed 的路径(应该是/database/seeders
dmskys commented 6 months ago

那估计是用不了啦,我是新手不会改造。哪里有可以将laravel的改造成webman代码的教程?