我正在使用Laravel 4.2进行一个项目,我需要在控制器中包含一个PHP文件(一个将PDF转换为Text的库),然后返回带有文本的变量,任何想法如何?
这是我的控制器:
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
答案 0 :(得分:16)
您可以在app目录中的某个位置创建新目录,例如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();
希望这有帮助!
答案 1 :(得分:5)
我认为你是对的兄弟,但是,我找到了另一种方式,也许不是正确的方式,但它确实有效。
就是这样,我创建了一个名为Includes的新文件夹并将我的文件放在那里,然后在/app/start/global.php中添加了这一行:
require app_path().'/includes/vendor/autoload.php';
现在正在努力:D