Laravel 调度调用控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36038241/
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
Laravel Scheduling call controller
提问by Kikolce
I work with Laravel Task Scheduling, but I have a problem when I call some method from my controller.
我使用 Laravel 任务调度,但是当我从控制器调用某些方法时遇到问题。
protected function schedule(Schedule $schedule)
{
$schedule->call('UserController@deleteInactiveUsers')->everyMinute();
//$schedule->call('App\Http\Controllers\UserController@deleteInactiveUsers')->everyMinute();
}
When I call with uncommented line i get this error:
当我使用未注释的行调用时,出现此错误:
[ReflectionException]
Class RecurrenceInvoiceController does not exist
and then I insert fully qualified namespace path and then I get this error:
然后我插入完全限定的命名空间路径,然后出现此错误:
[PDOException] SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
And
和
[ErrorException] PDO::__construct(): php_network_getaddresses: getaddrinfo failed: Name or service not known
Where is the problem? Which way is correct to call method from Controller from Laravel Task Scheduling.
问题出在哪儿?从 Laravel 任务调度中从控制器调用方法的方法是正确的。
回答by Jennifer Miranda Beuses
I stumbled months ago with the same problem, until I could fix it. I use laravel 5.2 and the kernel call my drivers this way:
几个月前我遇到了同样的问题,直到我能解决它。我使用 laravel 5.2,内核以这种方式调用我的驱动程序:
$schedule->call('App\Http\Controllers\MyController@MyAction')->everyMinute();
I hope this will help someone else ;)
我希望这会帮助别人;)
回答by Ahmad Dastageer
Directly put this in schedule function in kernel.php file
直接把这个放在kernel.php文件的schedule函数里
$schedule->call('App\Http\Controllers\YourController@function')->everyMinute();
$schedule->call('App\Http\Controllers\YourController@function')->everyMinute();
This will run after every minute
这将在每分钟后运行
Note:
笔记:
In localhost, you have to run it manually but on the server, you should try this command in your cronjob this will automatically call your controller function after every minute
在本地主机中,您必须手动运行它,但在服务器上,您应该在您的 cronjob 中尝试此命令,这将在每分钟后自动调用您的控制器功能
php -d register_argc_argv=On /home/iflasity/public_html/foldername /artisan schedule:run > /dev/null 2>&1
php -d register_argc_argv=On /home/iflasity/public_html/foldername /artisan schedule:run > /dev/null 2>&1
cd /home/iflasity/public_html/foldername && php artisan schedule:run /dev/null 2>&1
cd /home/iflasity/public_html/foldername && php artisan schedule:run /dev/null 2>&1
回答by lcjury
For me, the first error looks like you need to run composer update
, then composer dump-autoload
.
对我来说,第一个错误看起来像你需要运行composer update
,然后composer dump-autoload
.
If it works you will also get the second error, the 2002 error meaning is:
如果它有效你也会得到第二个错误,2002 错误的含义是:
Can't connect to local MySQL server through socket" (see (Client Error Codes and Messages in MySQL docs).
无法通过套接字连接到本地 MySQL 服务器”(请参阅(MySQL 文档中的客户端错误代码和消息)。
You need to set your database configuration in your .env
file
您需要在.env
文件中设置数据库配置
回答by Kikolce
OK. I solved this problem. The problem was with Docker Cron container. Cron container was not linked with MySQL container. Thanks for all answers.
好的。我解决了这个问题。问题出在 Docker Cron 容器上。Cron 容器未与 MySQL 容器链接。感谢所有的答案。
回答by Kikolce
Also now I test with more simple function to insert new currency in database. Here is code:
此外,现在我使用更简单的函数进行测试,以在数据库中插入新货币。这是代码:
public function handle()
{
$currency = new Currency();
$currency->currency_name = 'EUR';
$currency->save();
$this->info('New currency is successfully generated!');
}
This function is from laravel/app/Console/Commands
. I call from Kernel > schedule()
. Not work.
该函数来自laravel/app/Console/Commands
. 我从Kernel > schedule()
. 不行。
When I insert simple log write to handle()
function like:
当我插入简单的日志写入handle()
功能时:
File::put(base_path() . '/storage/logs/test_logs.txt', 'Test log ' . Carbon::now()->format('Y-m-d H:i:s') . PHP_EOL);
this works. But when I try to create new object from Models
and save into db -> this not work.
这有效。但是当我尝试从Models
db创建新对象并将其保存到 db 时 -> 这不起作用。