3

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?

Antonio Carlos Ribeiro
  • 86,191
  • 22
  • 213
  • 204
WildBill
  • 9,143
  • 15
  • 63
  • 87
  • http://laravel.com/docs/ssh – Justin E May 02 '14 at 12:35
  • So the above tells me how to access a remote server. I already have access to the webserver in question. I simply want to run a .php script on the server I am logged onto but I want to access, for example, the DB object to update tables, insert data, etc. – WildBill May 02 '14 at 12:47
  • 1
    Have you considered migrating the php script to a Laravel artisan command? Then it'd have access by default and you could just do `php artisan yourcommandhere`. – ollieread May 02 '14 at 17:08

2 Answers2

2

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);
Antonio Carlos Ribeiro
  • 86,191
  • 22
  • 213
  • 204
  • These files do not exist in my instance of laravel. This is for laravel 3 however... – WildBill May 02 '14 at 19:01
  • Sorry, edited to provide some info, you actually just have to look at public/index.php and see what it's calling, just don't startup the whole laravel application (`laravel.app`) and you should be good to go. – Antonio Carlos Ribeiro May 02 '14 at 19:13
2

I would highly recommend that you migrate your PHP script over to an artisan command. You can find more information here: http://laravel.com/docs/commands

This basically gives you access by default, as well as a lot of handy output and argument/option methods to simplify everything.

As a general rule of thumb, if you're running scripts that have to do something with Laravel, use commands.

ollieread
  • 6,018
  • 1
  • 20
  • 36
  • 1
    Since the laravel documentation is, ah, terrible, are there any examples of converting a script to artisan? – WildBill May 02 '14 at 19:03
  • To be honest with you, the structure of the class is the most important. You can then just literally copy your code into the correct method, and replace the parts with things like the model names and what not, it's really that simple. – ollieread May 02 '14 at 19:21
  • Oh wait, artisan is Laravel 4. – ollieread May 02 '14 at 19:21
  • So this is not an option in laravel 3? But I'm using artisan with my migrations, so it is working to some extent. Can you explain a touch more how to do so/call an artisan script? – WildBill May 21 '14 at 02:13
  • I'm afraid I adopted Laravel at the birth of 4, so I'm not familiar with the way 3 works. My fault, I didn't see where you specified that it was 3. – ollieread May 22 '14 at 08:40
  • See here for IMHO the best solution: https://willvincent.com/blog/easily-running-custom-scripts-bootstrapped-laravel-environment – Dan Sandberg Mar 19 '17 at 00:21