【问题标题】:Calling a custom Artisan command in a Controller | Laravel在控制器中调用自定义 Artisan 命令 |拉拉维尔
【发布时间】:2018-02-11 20:24:48
【问题描述】:

我正在尝试在控制器中执行自定义 Artisan 命令,但它会引发 CommandNotFound 异常。在实现中我运行命令,而不是执行整个命令'php artisan scan {url}',我只是运行扫描{url},因为这个答案:Running Artisan Command。我不明白我做错了什么?

自定义 Artisan 命令签名

protected $signature = 'scan {url}
                            {--r= : y or n | to follow or not to follow the robot.txt} 
                            {--fm= : set follow mode}
                            {--u= : username} 
                            {--p= : password} 
                            {--s : SQL module} 
                            {--x : XSS module}';

实施

switch ($select) {

   case 'full':

     \Artisan::call('scan ' . $customer->cms_url); //url == http://localhost:8888
     return $request;

     break;
}

错误

1/1) CommandNotFoundException
There are no commands defined in the "scan http" namespace.
in Application.php (line 548)
at Application->findNamespace('scan http')
in Application.php (line 582)
at Application->find('scan http://localhost:8888')
in Application.php (line 205)

【问题讨论】:

    标签: laravel command laravel-artisan


    【解决方案1】:

    首先在app/console/Kernel.php 中注册您的 Artisan 命令:在命令数组中添加您的命令类。之后你可以像这样调用你的命令

    \Artisan::call('scan', ['url' => "{$customer->cms_url}"]);
    

    您必须将 url 包裹在 " " 中,否则它不会设置完整的字符串

    【讨论】:

    • 感谢您的回复。这是正确的解决方案。
    猜你喜欢
    • 2016-07-30
    • 1970-01-01
    • 2018-11-12
    • 1970-01-01
    • 2015-12-26
    • 2018-05-23
    • 2023-01-04
    • 2019-05-26
    • 1970-01-01
    相关资源
    最近更新 更多