【问题标题】:Creating a new Command using Artisan, without shell access使用 Artisan 创建新命令,无需 shell 访问
【发布时间】:2015-03-07 20:21:35
【问题描述】:

我需要在 Laravel 网站上设置一些 cron 工作。似乎首先我必须在 shell 中运行以下命令才能开始:

php artisan command:make CustomCommand

但是,由于我没有 shell 访问权限,我唯一的其他选择是使用 Artisan::call 并通过 HTTP 进行访问。语法是这样的:

\Artisan::call( 'command:make', 
    array(
        'arg-name' => 'CustomCommand',
        '--option' => ''
    )
);

我面临的问题是,我似乎找不到 command:make 命令的 arg-name 值。

如果有人提到make 命令的参数名称或建议不需要外壳访问的替代解决方案,我真的很感激。

【问题讨论】:

    标签: laravel laravel-4 laravel-artisan


    【解决方案1】:

    您可以通过创建一个代表您的命令的类来手动添加它。 cli 命令生成下一个文件:

        <?php
    
    use Illuminate\Console\Command;
    use Symfony\Component\Console\Input\InputOption;
    use Symfony\Component\Console\Input\InputArgument;
    
    class Test extends Command {
    
        /**
         * The console command name.
         *
         * @var string
         */
        protected $name = 'command:name';
    
        /**
         * The console command description.
         *
         * @var string
         */
        protected $description = 'Command description.';
    
        /**
         * Create a new command instance.
         *
         * @return void
         */
        public function __construct()
        {
            parent::__construct();
        }
    
        /**
         * Execute the console command.
         *
         * @return mixed
         */
        public function fire()
        {
            //
        }
    
        /**
         * Get the console command arguments.
         *
         * @return array
         */
        protected function getArguments()
        {
            return array(
                array('example', InputArgument::REQUIRED, 'An example argument.'),
            );
        }
    
        /**
         * Get the console command options.
         *
         * @return array
         */
        protected function getOptions()
        {
            return array(
                array('example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null),
            );
        }
    
    }
    

    把它放在你的commands 目录中(对于L4,它是app/commands)。接下来只需将自定义命令的绑定添加到您的 app/start/artisan.php 文件中:

    Artisan::add(new Test); 就是这样。当您不需要触摸服务器的 crontab 时,这是理想的解决方案。如果您可以从 CP 访问它,这将是最简单的解决方案。如果您没有这种能力,现在可以设置 crontab 来运行您的自定义命令。希望这会有所帮助。

    【讨论】:

    • 干杯!尚未测试,但我很确定它会起作用。谢谢!
    • 太棒了,试一试,然后回复你的成功:)
    • 顺便说一句,文件名应该是什么? MyCommand.phpmyCommand.php?
    • 或者这有关系吗?
    • 我觉得应该和类名一样。
    猜你喜欢
    • 1970-01-01
    • 2014-04-01
    • 1970-01-01
    • 2015-04-14
    • 2015-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    相关资源
    最近更新 更多