将第三方库添加到 Laravel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44925824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:16:02  来源:igfitidea点击:

Adding Third party library to Laravel

phplaravelpayment-gatewaypayment

提问by Sameera Herath

I have an RSA algorithm Library giving to me by a payment gateway and When I do a

我有一个支付网关给我的 RSA 算法库,当我做一个

include (app_path().'/PaymentGateway/Crypt/RSA.php');

this and try to make an object as $rsa = new Crypt_RSA();this it gives me and error saying

这个并尝试制作一个对象,因为$rsa = new Crypt_RSA();它给了我和错误说

Class 'App\Http\Controllers\Crypt_RSA' not found

I tried including it in web.phpand making an object it worked the problem occur when I try to include it in a Controller.

我尝试将它包含在其中web.php并制作一个可以工作的对象,当我尝试将它包含在控制器中时会出现问题。

回答by John Major

This is what I did. Oh and a little back ground I use to have this in Laravel 4, PHP 5, jpgraph 2.

这就是我所做的。哦,还有一点背景知识,我曾经在 Laravel 4、PHP 5、jpgraph 2 中使用过它。

I am using jpgraph 4.1on Laravel 5.5using PHP 7.

我使用jpgraph 4.1Laravel 5.5使用PHP 7

  1. Created a folder under app called jpgraph
  2. Placed the srcfolder that is in the tarball of jpgraph in that folder
  3. Created file call Graph1.php, is my code using jpgraph, with the class Custom_GraphsJMin the jpgraphfolder.
  4. In composer.jsonadded "app/jpgraph/Graph1.php"to the "classmap"

    "autoload": {
      "classmap": [
        "database/seeds",
        "database/factories",
        "app/jpgraph/Graph1.php"
      ],
      "psr-4": {
        "App\": "app/"
      }
    },
    
  5. In the application folder:

    composer dump-autoload

  6. Checked the autoload_classmap.phpand I have

    'Custom_GraphsJM' => $baseDir . '/app/jpgraph/Graph1.php',

  7. In my Model at the top I have

    use Custom_GraphsJM;

  8. To create a class

    $Two_Graphs_Temp = new Custom_GraphsJM();

  1. 在应用程序下创建一个名为 jpgraph
  2. srcjpgraph tarball 中的文件夹放在该文件夹中
  3. 创建的文件调用Graph1.php,是我的代码使用jpgraph的,与类Custom_GraphsJMjpgraph的文件夹。
  4. composer.json添加"app/jpgraph/Graph1.php""classmap"

    "autoload": {
      "classmap": [
        "database/seeds",
        "database/factories",
        "app/jpgraph/Graph1.php"
      ],
      "psr-4": {
        "App\": "app/"
      }
    },
    
  5. 在应用程序文件夹中:

    composer dump-autoload

  6. 检查了autoload_classmap.php,我有

    'Custom_GraphsJM' => $baseDir . '/app/jpgraph/Graph1.php',

  7. 在我顶部的模型中,我有

    use Custom_GraphsJM;

  8. 创建一个类

    $Two_Graphs_Temp = new Custom_GraphsJM();

回答by Fenno

You can tell Composer to autoload any (non-PSR) class by adding the base folder to:

您可以通过将基本文件夹添加到以下位置来告诉 Composer 自动加载任何(非 PSR)类:

"autoload": {
"classmap": [
    "app/commands",
    "app/database/migrations",
    "app/database/seeds",
    "app/tests/TestCase.php"
],
....

And you can also autoload autoloaders by adding them to the files section:

您还可以通过将自动加载器添加到 files 部分来自动加载它们:

"autoload": {
"files": [
    "temboo/src/Temboo_Loader.php"
],

...

...

After adding those entries, execute:

添加这些条目后,执行:

composer dumpautoload

And check the file vendor/composer/autoload_classmap.php, the available classes must be all listed in it, if one file is not there it will not be autoloaded.

并检查vendor/composer/autoload_classmap.php文件,可用的类必须全部列在其中,如果没有一个文件,则不会自动加载。

回答by mighTY

On default, everything included in the app folder of your laravel project is autoloaded, that is described in the composer.jsonof your project:

默认情况下,laravel 项目的 app 文件夹中包含的所有内容都会自动加载,这在项目的composer.json 中进行了描述:

...        
"autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "App\": "app/"
        }
    },
...

The only thing you will need to do is simply usethe namespace:

您唯一需要做的就是使用命名空间:

use App/Path/To/Third/Party/plugin/Class;

If, however, the plugin is placed outside of the scope of App, then simply add it to the psr-4 autoloader:

但是,如果插件被放置在 App 的范围之外,那么只需将它添加到 psr-4 自动加载器:

    "psr-4": {
        "ProjectRootNs\": "projects/myproject/"
    }