如何在laravel 5中添加Libraries文件夹?

时间:2016-08-19 02:56:16

标签: php laravel-5

我正在尝试在laravel 5.0中添加库文件夹。我尝试按照以下步骤link 但是我收到了一个错误。

Class 'Libraries\Myclass' not found

这是我的composer.json

{
"autoload": {
        "classmap": [
            "database"            
        ],
        "psr-4": {
            "App\\": "app/"
        }
    },
    "autoload-dev": {
        "classmap": [
            "tests/TestCase.php",
            "app/Libraries"
        ]
    },
    "scripts": {
        "post-root-package-install": [
            "php -r \"copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "php artisan key:generate"
        ],
        "post-install-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postInstall",
            "php artisan optimize"
        ],
        "post-update-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postUpdate",
            "php artisan optimize"
        ]
    },
    "config": {
        "preferred-install": "dist"
    }
}

请参阅 autoload-dev 。我添加了我的库(app / Libraries)文件夹然后运行此命令 composer dump-autoload 这是我在Libraries / Myclass.php

中的类代码
namespace Libraries\Myclass;

class Myclass{
    public function hello(){
        echo "hello world";
    }
}

我不知道我的代码是否足够好。如果我错了,请纠正我。我正试图在laravel 5中探索更多。

3 个答案:

答案 0 :(得分:1)

    {
     "autoload": {
        "classmap": [
            "database"            
        ],
        "files": [
            "app/Libraries"
        ],
        "psr-4": {
            "App\\": "app/"
        }
    },
    "autoload-dev": {
        "classmap": [
            "tests/TestCase.php",
        ]
    },
    "scripts": {
        "post-root-package-install": [
            "php -r \"copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "php artisan key:generate"
        ],
        "post-install-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postInstall",
            "php artisan optimize"
        ],
        "post-update-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postUpdate",
            "php artisan optimize"
        ]
    },
    "config": {
        "preferred-install": "dist"
    }
}

运行composer dump-autoload命令并使用您的库。

答案 1 :(得分:0)

我处于类似情况,并且认为它是一种干净的方式(在我看来)。我正在使用Laravel 5.4。我的班级被命名为“Ldap”,这就是我的做法。

  1. 在app下创建了一个Libraries目录,并添加了我的类文件:

    app / Libraries / Ldap.php(具有名称空间Libraries \ Ldap)

  2. composer.json文件中的
  3. "autoload": {
        ...
    
        "classmap": [
            "database",
            "app/Libraries" <------------ Added libraries directory
        ], 
    
        ...
     }
    
  4. 去了终端并运行了这个命令:composer dump-autoload

  5. 打开aap / config / app.php文件并在“别名”下添加以下代码:

    'aliases' => [
        ...
    
        'Ldap' => \Libraries\Ldap\Ldap::class <--- Added this line
    ],
    
  6. 现在在我的控制器中,我只是实例化我的课程:

    // Ldap class object
    $ldapObj = new Ldap();
    
  7. 它就像一个魅力! 快乐的拉扯!

答案 2 :(得分:0)

对于 laravel 8,以下方法有效:

composer.json中:

"autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/",
            "Lib\\": "lib/" <------- Add this
        }
    },

然后运行 ​​composer dump-autoload