php 在 Laravel 5 中运行工匠命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37236206/
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-08-26 01:07:11  来源:igfitidea点击:

Run artisan command in laravel 5

phplaravellaravel-5laravel-5.2artisan

提问by paranoid

I have controller like this

我有这样的控制器

 public function store(Request $request)
{
   Artisan::call("php artisan infyom:scaffold {$request['name']} --fieldsFile=public/Product.json");
}

Show me error

显示错误

There are no commands defined in the "php artisan infyom" namespace.

“php artisan infyom”命名空间中没有定义命令。

When I run this command in CMD it work correctly

当我在 CMD 中运行此命令时,它可以正常工作

回答by Alexey Mezenin

You need to remove php artisanpart and put parameters into an array to make it work:

您需要删除php artisan部分并将参数放入数组中才能使其工作:

public function store(Request $request)
{
   Artisan::call("infyom:scaffold", ['name' => $request['name'], '--fieldsFile' => 'public/Product.json']);
}

https://laravel.com/docs/5.2/artisan#calling-commands-via-code

https://laravel.com/docs/5.2/artisan#calling-commands-via-code

回答by Nole

If you have simple job to do you can do it from route file. For example you want to clear cache. In terminal it would be php artisan cache:clearIn route file that would be:

如果你有简单的工作要做,你可以从路由文件中完成。例如你想清除缓存。在终端中它将是php artisan cache:clear在路由文件中将是:

Route::get('clear_cache', function () {

    \Artisan::call('cache:clear');

    dd("Cache is cleared");

});

To run this command from browser just go to your's project route and to clear_cache. Example:

要从浏览器运行此命令,只需转到您的项目路径和 clear_cache。例子:

http://project_route/clear_cache