Artisan::call() outside the Laravel framework

后端 未结 3 1459
渐次进展
渐次进展 2020-12-10 17:10

I want to create a cron job for Laravel 5.2

My shared host (on OVH), only allows me to point to the full path of a file, and I am not able to use th

相关标签:
3条回答
  • 2020-12-10 17:49

    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.

    0 讨论(0)
  • 2020-12-10 18:04

    Just try simple:

    shell_exec('php artisan refresh');
    

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

    0 讨论(0)
  • 2020-12-10 18:04

    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.

    0 讨论(0)
提交回复
热议问题