访问laravel环境和类时如何在命令行上运行php脚本 [英] How to run php script on the command line while accessing laravel environment and classes
问题描述
我在Web服务器上有一个laravel 3环境,但是我想在命令行上运行php脚本.我想访问与laravel环境中的任何php脚本(例如,控制器,模型或视图文件)访问的类和方法相同的类和方法.
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.
我该怎么做?
推荐答案
要在您自己的脚本中使用Laravel应用程序,它需要在启动之前从应用程序目录中加载两件事:
To use the Laravel application in your own script, it needs to load two things from your application directory before starting:
这可能不是完全正确的方法,但是您应该可以通过以下操作来引导它:
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
Composer自动加载脚本,用于自动加载所有类:
Laravel 4
The Composer autoload script, to autoload all of your classes:
require __DIR__.'/../bootstrap/autoload.php';
如果您需要IoC容器中的东西,您将:
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);
这篇关于访问laravel环境和类时如何在命令行上运行php脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!