I want to use aws-sdk for php client in a custom module of drupal. I need to include vendor/autoload.php in module. 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. Then the website gets crashed. Please could you tell how I should use require __DIR__.'/vendor/autoload.php';
I'm not sure about how drupal handles autoloading of external php modules. 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. If it does not include, add the require statement which points to autoload.php relative to index.php or absolute path. Then using namespaces in php, you can use external php modules which are then autoloaded.
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).
Here's how I've gone about adding 3rd party packages in custom D7 modules:
generate a composer.json file that installs it in vendor/
require vendor/autoload.php from [modulename].module
In detail:
change to module directory
run 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
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)
When finished you should have a vendor directory that includes an autoload.php file and /composer directory. "Require" this from your .module file, e.g.:
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.
I had wondered if a simple files[] = line for the autoload.php in [modulename].info would work - it doesn't, regardless of order.
Related
I'm using PhpStorm and Deployer so I included deployer.phar in PHP Include paths to have auto-completion in deploy.php file. After including the whole deployer.phar PhpStorm complains about duplicate class definition - one coming from my app vendor directory, and second from deployer.phar vendor directory.
Is it somehow possible to configure PhpStorm Include paths so only some parts of PHAR file (ex. only the src directory) are included? This doesn't work:
/path/to/deployer.phar/src
nor this:
phar:///path/to/deployer.phar/src
I want to avoid installing deployer/deployer as composer dev package.
EDIT
It seems that even adding single PHAR file in Include paths is kind of a hack.
It's not possible currently. As a workaround you can unpack deployer.phar somewhere and add extracted deployer.phar/src to Include paths. Not convenient, but would work.
I have created a file called db-constant.php in conifg directory in laravel , this file contains my constants,then i added this line require_once 'db-constants.php'; in app.php ,the problem is where i type php artisan cache:clear or any command this message will appear
PHP Notice: Constant FLD_EMAIL already defined in /var/www/html/test/config/db-constants.php on line 1
how can i fix this ?
If you use Laravel, you don't have to use require_once or it's counterparts. composer's autoloading is used for everything, and configuration files are loaded automatically. What you have to do is read the docs about configuration, place the file in config/ directory and return an array with configuration values.
Now, what it seems (to me at) is that for some odd reason you want to use constants instead of Laravel's mechanism for accessing configuration values. In that case, you:
1.) DON'T place anything in config/ directory
2.) DON'T manually require_once anything
3.) DO use Composer's autoloading capabilities
How to load a file with constants in Laravel
1.) create app/MyConstants directory.
2.) create your file, let's call it constants.php. Path is:
app/MyConstants/constants.php
3.) Add your define code
4.) Open composer.json
5.) Find "autoload"
6.) Look for "files" under "autoload". If it doesn't exist, create it.
7.) Add the list of files you want to include automatically
Your "autoload" in composer.json should look like this:
"autoload": {
"files": ["app/MyConstants/constants.php"]
}
In terminal, type composer du or composer dump-autoload. It will re-create autoloader and automatically include the file with your constants.
Remove bootstrap/cache so you can use artisan again.
Now you can:
1.) Have a place where you can define constants
2.) Avoid manually requiring files
Personally, I don't see a reason for this, but then again - I'm just a human, I've no clue about anything in the grand scheme :)
You need to read this docs first https://laravel.com/docs/5.4/configuration. As well as the rest of the documentation for Laravel. How to use config files:
Create your own file db-constant.php in config directory.
Fill it as you need. For example
return [
'FLD_EMAIL' => 'your-field-here',
];
Use it wherever you need
$email = config('db-constant.FLD_EMAIL');
It's all.
I want to use the following AntiXSS library in one PHP file. It is my first time using Composer, but I followed their installation steps and I installed it successfully. I downloaded the library through Composer, updated it and Composer created the vendor/ folder in my directory with the necessary files.
Now I added the following require 'vendor/autoload.php' into my PHP file. I created a new AntiXSS class but I obtain the following error:
Class AntiXSS not found in my directory on line 3.
I tried to use an absolute path instead of vendor/autoload.php but it isn't working yet and I don't know if I should do something more.
Best regards
The class is located in the voku\helper namespace. Use new \voku\helper\AntiXSS() to instantiate it or use use imports to import the namespace.
See php.net for more information about namespaces.
I'm new to Composer, namespaces, and autoload and I wasn't able to figure out where to write my code (under vendor?).
I have created a directory named ilhan under the vendor, and a file named People.php. Then in the main index.php file using use ilhan\People.php as People; doesn't work because I think it must have been written in autoload_namespaces.php initially.
But if I register ilhan as a vendor then I think Composer will look into the packagist.org which it isn't there.
Create ilhan inside root of your project directory, not in vendor directory and put following in your composer.json,
"autoload": {
"psr-4": {
"Ilhan\\": "ilhan/"
}
},
Most probably you already have psr-4 autoload config added in your composer.json file if you are using some sort of framework, in that case just add "Ilhan\\": "ilhan/" in to it.
Now create People.php inside ilhan directory with following content
<?php
namespace Ilhan;
class People{}
Make sure require __DIR__.'/vendor/autoload.php'; is included in index.php any how, then run composer dump-autoload.
Now in index.php just bellow require __DIR__.'/vendor/autoload.php'; following should work,
use Ilhan\People;
But why do you want to use People class in index.php?
Your code goes into the root directory of your project (or any subdirectory). The vendor folder is only for packages/libraries downloaded by composer, you should never change anything in there.
To start a project just create a new file e.g. /my-project/index.php and require the autoload.php which is automatically created by composer:
<?php
require __DIR__.'/vendor/autoload.php';
// here comes your project code
For more information about autoloading see the official composer documentation at Basic Usage: Autoloading
I'm hoping someone can spot what I've forgotten to do. Here are my steps:
Downloaded and unpacked the ZendFramework-2.3.5 into /usr/share.
Updated include_path in my php.ini file to include '/usr/share/ZendFramework-2.3.5/library' per the INSTALL.md, and restarted Apache to confirm the path is set (now ".:/usr/share/php:/usr/share/ZendFramework-2.3.5/library").
Created a test script in my web document root (using the class 'CamelCaseToUnderscore' as an example):
use Zend\Filter\Word\CamelCaseToUnderscore;
$filter = new CamelCaseToUnderscore();
echo $filter->filter('BigsAndLittles');
...and I get the fatal error "class 'zend\filter\word\camelcasetoseparator' not found".
In order to do use Zend classes like this, do I need to do some additional configuration or create an autoloader or something to find them? Seems like this should have worked. If I include the CamelCaseToUnderscore.php file in a require_once statement, then I get a fatal error that it's parent class doesn't exist (CamelCaseToSeparator.php). What am I missing?
You can use require 'Zend/Mvc/Application.php' to test if your include path is correct, but you will need an autoloader:
http://framework.zend.com/manual/current/en/modules/zend.loader.standard-autoloader.html.
You can find an example here (lines 18-20):
https://github.com/zendframework/zf2/blob/master/demos/Zend/Feeds/consume-feed.php
I strongly suggest using composer as it will save you a lot of time troubleshooting your include paths, but it also allows you manage version better. It makes it easier for other developers and to deploy your code.
Starting with composer is very easy, just install it and create composer.json:
https://getcomposer.org/doc/01-basic-usage.md#composer-json-project-setup
Run:
composer require zendframework/zendframework
Composer will download all libraries to vendor folder and will generate an autoloader, all you have to do is to include
require 'vendor/autoload.php';
https://getcomposer.org/doc/01-basic-usage.md#autoloading
Most popular PHP frameworks use composer for managing dependencies:
https://github.com/zendframework/zf2/blob/master/composer.json
https://github.com/symfony/symfony/blob/2.7/composer.json