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

旧城冷巷雨未停 提交于 2020-01-04 04:45:09

问题


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?


回答1:


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.




回答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);


来源:https://stackoverflow.com/questions/23428289/how-to-run-php-script-on-the-command-line-while-accessing-laravel-environment-an

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