简体   繁体   English

如何在drupal 7的模块中包含自动加载

[英]How to include autoload in module in drupal 7

I want to use aws-sdk for php client in a custom module of drupal.我想在 drupal 的自定义模块中将aws-sdk用于 php 客户端。 I need to include vendor/autoload.php in module.我需要在模块中包含vendor/autoload.php But when I include it, it gives me error.但是当我包含它时,它给了我错误。 I have tried to include many ways but did not get success.我试图包括很多方法,但没有成功。 I added it as:我将其添加为:

require __DIR__.'/vendor/autoload.php'; at the top of the file of .module file..module文件的文件顶部。 Then the website gets crashed.然后网站就崩溃了。 Please could you tell how I should use require __DIR__.'/vendor/autoload.php';请你告诉我应该如何使用require __DIR__.'/vendor/autoload.php';

I'm not sure about how drupal handles autoloading of external php modules.我不确定 drupal 如何处理外部 php 模块的自动加载。 But with the experience of working with frameworks like laravel, cakephp and with composer, I'm sure that the target file which is index.php which routes to a number of different controllers in root directory already includes vendor/autoload.php.但是根据使用 laravel、cakephp 和 composer 等框架的经验,我确信目标文件 index.php 路由到根目录中的许多不同控制器已经包含 vendor/autoload.php。 If it does not include, add the require statement which points to autoload.php relative to index.php or absolute path.如果不包含,则添加指向 autoload.php 相对于 index.php 或绝对路径的 require 语句。 Then using namespaces in php, you can use external php modules which are then autoloaded.然后在 php 中使用namespaces ,您可以使用外部 php 模块,然后自动加载。

require __DIR__.'/vendor/autoload.php'; // Incase vendor directory is in same level as index.php file
require dirname(__DIR__).'/vendor/autoload.php'; // Incase vendor directory is in parent level as index.php file

It's hard to know without the actual error message.如果没有实际的错误消息,很难知道。 If you're getting a white screen you need to enable display of full errors ( admin/config/development/logging ) so you can see what's actually going on and if the classes you're relying on aren't being included in the first place, or there's some sort of conflict (remember also to clear the cache after changing anything significant).如果出现白屏,则需要启用完整错误的显示( admin/config/development/logging ),以便您可以查看实际发生的情况,以及您所依赖的类是否未包含在第一个中地方,或者存在某种冲突(请记住在更改任何重要内容后还要清除缓存)。

Here's how I've gone about adding 3rd party packages in custom D7 modules:以下是我在自定义 D7 模块中添加 3rd 方包的方法:

  • generate a composer.json file that installs it in vendor/生成一个 composer.json 文件,将其安装在vendor/
  • require vendor/autoload.php from [modulename].module需要来自[modulename].module vendor/autoload.php

In detail:详细:

  • change to module directory切换到模块目录

  • run composer init运行composer init

  • follow prompts (it'll ask "Would you like to define your dependencies interactively" - choose yes then you can search by name)按照提示进行操作(它会询问“您是否想以交互方式定义您的依赖项”-选择是然后您可以按名称搜索)

  • use drupal-module for type使用drupal-module作为类型

  • you can use "proprietary" for license if you don't want anyone else to use your code, this the composer.json validate cleanly ( composer validate )如果您不希望其他任何人使用您的代码,您可以使用“专有”作为许可证,这是 composer.json 干净地验证( composer validate

  • When finished you should have a vendor directory that includes an autoload.php file and /composer directory.完成后,您应该有一个 vendor 目录,其中包含一个autoload.php文件和/composer目录。 "Require" this from your .module file, eg:从您的.module文件中“要求”这个,例如:

require 'vendor/autoload.php';

  • drush cc all

You should now be able to use your code.您现在应该可以使用您的代码了。


Note there's a Drupal module called xautoload - (in my opinion) overkill for this.请注意,有一个名为xautoload的 Drupal 模块 -(在我看来)对此有些过分。

I had wondered if a simple files[] = line for the autoload.php in [modulename].info would work - it doesn't, regardless of order.我想知道[modulename].info autoload.php一个简单的files[] =行是否会起作用 - 无论顺序如何,它都不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM