Artisan::call() outside the Laravel framework

无人久伴 提交于 2019-11-30 21:38:15

What you want to do is run a specific Artisan command from within a script.

You can achieve this by copying artisan.php and forcing the input to what you want:

#!/usr/bin/env php
<?php

require __DIR__.'/bootstrap/autoload.php';

$app = require_once __DIR__.'/bootstrap/app.php';

$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);

$status = $kernel->handle(
    $input = new Symfony\Component\Console\Input\ArrayInput(['command' => 'refresh']),
    new Symfony\Component\Console\Output\ConsoleOutput
);

$kernel->terminate($input, $status);

exit($status);

If you compare this script to artisan.php, you'll see that I've just forced the input passed to $kernel->handle() method. It does not read the input from the CLI anymore, it takes these arguments, as an array. See Symfony Console Component documentation for more details.

If you need to pass arguments to your script, just set the input accordingly:

$input = new Symfony\Component\Console\Input\ArrayInput([
    'command' => 'refresh',
    'arg_foo' => 'foo',
    '--option_bar' => 42
]);

$status = $kernel->handle(
    $input,
    new Symfony\Component\Console\Output\ConsoleOutput
);

Now, you can put this script where you want, it does not need to be accessible through web via a browser (it should not, by the way).

If you put it at the root of your hosting at OVH, I mean NOT in www, you just have to fill the form very simply:

If you want your script to be accessible through the web (which is not recommanded for obvious security reasons, but still), place it in your www directory, change the paths to bootstrap/autoload.php and bootstrap/app.php and give your script a name that is not easy to guess.

In the form in the OVH manager, don't forget to add www/ at the beginning of the path of the script.

There is no need to specify php script_name, since the manager handles it for you, when you choose PHP version. Just type the path of the script PHP will execute.

If you just want set a cron job. please edit crontab and use "your/app/path/php artisan cron:job" to excute your command directly.

Just try simple:

shell_exec('php artisan refresh');

If it doesn't work, try to add appropriate paths to both php and artisan.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!