我正在使用Laravel 4.2做一个项目,我需要在控制器中包含一个PHP文件(一个将PDF转换为文本的库),然后返回一个带有文本的变量,知道怎么做吗?
这是我的控制器:
public function transform() {
include ('includes/vendor/autoload.php');
}
我的/app/start/global.php文件:
ClassLoader::addDirectories(array(
app_path().'/commands',
app_path().'/controllers',
app_path().'/models',
app_path().'/database/seeds',
app_path().'/includes',
));
这就是错误:
include(includes/vendor/autoload.php): failed to open stream: No such file or directory
您可以在应用程序目录中的某个位置创建一个新目录,例如app/libraries
然后在composer.json文件中,您可以在自动加载类映射中包含app/libraries
:
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"require": {
"laravel/framework": "4.2.*",
},
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/libraries", <------------------ YOUR CUSTOM DIRECTORY
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
]
},
"scripts": {
"post-install-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
"post-update-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
"post-create-project-cmd": [
"php artisan key:generate"
]
},
"config": {
"preferred-install": "dist"
},
"minimum-stability": "stable",
}
请确保在修改您的composer.json.后运行composer dump-autoload
假设您的类名名为CustomClass.php
,它位于app/libraries
目录中(因此完整路径为app/libraries/CustomClass.php
)。如果您已经为类设置了适当的名称空间,按照惯例,您的名称空间可能会命名为libraries
。为了清楚起见,我们将调用我们的命名空间custom
,以避免与目录混淆。
$class = new 'custom'CustomClass();
或者,您可以在app/config/app.php
文件中给它一个别名:
/*
|--------------------------------------------------------------------------
| Class Aliases
|--------------------------------------------------------------------------
|
| This array of class aliases will be registered when this application
| is started. However, feel free to register as many as you wish as
| the aliases are "lazy" loaded so they don't hinder performance.
|
*/
'aliases' => array(
...
'CustomClass' => 'custom'CustomClass',
...
)
您可以从应用程序中的任何位置实例化该类,就像使用任何其他类一样:
$class = new CustomClass();
希望这能有所帮助!
我认为你是对的,兄弟,但是,我找到了另一种方法,也许不是正确的方法,但它有效。
就像这样,我创建了一个名为Includes的新文件夹,并将我的文件放在那里,然后在/app/start/global.php中添加了这一行:
require app_path().'/includes/vendor/autoload.php';
现在正在工作:D