加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Laravel 4.2:在控制器中包含一个PHP文件(库)

发布时间:2020-12-14 19:49:31 所属栏目:大数据 来源:网络整理
导读:我正在使用Laravel 4.2进行一个项目,我需要在控制器中包含一个 PHP文件(将PDF转换为文本的库),然后返回带有文本的变量,任何想法如何? 这是我的控制器: public function transform() { include ('includes/vendor/autoload.php');} 和我的/app/start/global
我正在使用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目录中的某个位置创建新目录,例如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","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).如果按照惯例对类进行了正确命名,则命名空间可能会命名为库.为了清楚起见,我们将调用我们的命名空间自定义以避免与目录混淆.

$class = new customCustomClass();

或者,您可以在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'   => 'customCustomClass',...
)

您可以像应用任何其他类一样在应用程序的任何位置实例化该类:

$class = new CustomClass();

希望这可以帮助!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读