Laravel 5 - 如何从 Artisan 命令运行控制器方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36518225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:34:40  来源:igfitidea点击:

Laravel 5 - how to run a Controller method from an Artisan Command?

phplaravelcontrollercommandartisan

提问by iSS

I need some code from my Controller to run every ten minutes. Easy enough with Schedulerand Commands. But. I've created a Command, registered it with Laravel Scheduler(in Kernel.php) and now I am unable to instantiate the Controller. I know it's a wrong way to approach this problem, but I just needed a quick test. Is there a way, mind you a hacky way, to accomplish this? Thank you.

我需要我的控制器中的一些代码每十分钟运行一次。很容易,与SchedulerCommands。但。我已经创建了一个Command,用Laravel Scheduler(in Kernel.php)注册了它,现在我无法实例化Controller. 我知道这是解决此问题的错误方法,但我只需要进行快速测试。有没有办法,请注意一个hacky的方式,来完成这个?谢谢你。

Update #1:

更新 #1:

The Command:

Command

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Http\Controllers\StatsController;


class UpdateProfiles extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'update-profiles';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Updates profiles in database.';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        StatsController::updateStats('<theProfileName>');
    }
}

updateStats()method in StatsController.php

updateStats()方法在 StatsController.php

public static function updateStats($theProfileName) { 
   // the body
}

This returns a FatalErrorException:

这将返回一个FatalErrorException

[Symfony\Component\Debug\Exception\FatalErrorException] 
syntax error, unexpected 'if' (T_IF)

Update #2:

更新#2:

Turns out that I've had an typo in the updateStats()method, but the answer by @alexey-mezenin works like a charm! It is also enough to import the Controllerinto the Command:

原来我在该updateStats()方法中有一个错字,但@alexey-mezenin 的回答很有魅力!将 导入Controller到 中也足够了Command

use App\Http\Controllers\StatsController;

And then initialize it as you'd do normally:

然后像往常一样初始化它:

public function handle() {
   $statControl        = new StatsController;
   $statControl->updateStats('<theProfileName>');
}

采纳答案by Alexey Mezenin

Try to use use Full\Path\To\Your\Controller;in your command code and use method statically:

尝试use Full\Path\To\Your\Controller;在您的命令代码中使用并静态使用方法:

public static function someStaticMethod()
{
    return 'Hello';
}

In your command code:

在您的命令代码中:

echo myClass::someStaticMethod();