在Controller中调用自定义Artisan命令Laravel

时间:2017-09-03 11:04:55

标签: laravel command artisan

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

自定义工匠命令签名

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)

1 个答案:

答案 0 :(得分:7)

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

\Artisan::call('scan',[ 'url' => "{$customer->cms_url}" ]); // You must wrap the url in " " otherwise it will not set the full string