How to run php script on the command line while accessing laravel environment and classes

WildBill picture WildBill · May 2, 2014 · Viewed 8k times · Source

I have a laravel 3 environment on a web server but I want to run a php script on the command line. I'd like to access the same classes and methods that any php script within the laravel environment (for example a controller, model or view file) accesses.

How can I do that?

Answer

Antonio Carlos Ribeiro picture Antonio Carlos Ribeiro · May 2, 2014

To use the Laravel application in your own script, it needs to load two things from your application directory before starting:

Laravel 3

This might not be exactly the way, but you should be able to boot it by doing:

define('LARAVEL_START', microtime(true));

require 'paths.php';

require path('sys').'core.php';

Laravel 4

The Composer autoload script, to autoload all of your classes:

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

And if you need things from the IoC container, you'll:

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

Then you will be able to do things like:

$post = Post::find(1);