Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
256 views
in Technique[技术] by (71.8m points)

php - Artisan::call() outside the Laravel framework

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 the recommended Cron entry from Laravel's docs, ie :

* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1

Therefore, I have to call the Artisan command from a .php file, outside of the Laravel framework.

Here is what my public/cron.php file looks like so far:

<?php

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

use IlluminateSupportFacadesArtisan;

Artisan::call('refresh');

refresh being my command for regenerating thumbnails inside my app.

When accessing cron.php through my browser (testing on local XAMPP), the following error occurs:

Fatal error: Uncaught RuntimeException: A facade root has not been set. in 
C:xampphtdocssitevendorlaravelframeworksrcIlluminateSupportFacadesFacade.php:210

Stack trace: 
#0 C:xampphtdocssitepubliccron.php(7): IlluminateSupportFacadesFacade::__callStatic('call', Array) 
#1 {main} thrown in C:xampphtdocssitevendorlaravelframeworksrcIlluminateSupportFacadesFacade.php on line 210

I have also tried to boot the app, but it doesn't make any differences

$app = require_once __DIR__.'/../bootstrap/app.php';
$app->boot();

To avoid using the Artisan Facade, I tried calling the underlying Kernel Class directly:

use IlluminateContractsConsoleKernel;

$kernel = new Kernel;
$kernel->call('refresh');

But this returns:

Uncaught Error: Cannot instantiate interface IlluminateContractsConsoleKernel

EDIT: Here is a screenshot of OVH cron interface. The cron task is customized by OVH and only allows to point to the fullpath uri of a file - which file would execute my artisan command-. My question is, what should I put in this file, and should it be a PHP file, or a CMD?

OVH cron interface

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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(IlluminateContractsConsoleKernel::class);

$status = $kernel->handle(
    $input = new SymfonyComponentConsoleInputArrayInput(['command' => 'refresh']),
    new SymfonyComponentConsoleOutputConsoleOutput
);

$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 SymfonyComponentConsoleInputArrayInput([
    'command' => 'refresh',
    'arg_foo' => 'foo',
    '--option_bar' => 42
]);

$status = $kernel->handle(
    $input,
    new SymfonyComponentConsoleOutputConsoleOutput
);

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:

OVH Shared Hosting Cronjob - Step 1

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...