i have downloaded phpepub via composer
then started to run the test file to understand how to use the library but it throws an error
Class 'com\grandt\EPub' not found
and then i started to view the test folder and opened the file exampletest1.php which also threw an error saying that
Class 'PHPePub\Core\Logger' not found
i'm thinking a way of working out this error for a while now checked the privileges (which is fine) also the file is also present in the folder
here is the file structure of the library
phpepub/
legacy/
src/
PHPePub/
Core/
structure/
Logger.php
.
.
.
Helpers/
tests/
demo/
EPub.Example1.php
.
.
.
composer.json
vendor/
composer/
grandt/
phpzip/
.
.
.
README.md
test.php
ReadMe.html
.
.
.
.
composer.json
You need to require the vendor/autoload.php file in each file where you're using components installed using composer.
test.php :
<?php
require_once __DIR__.'/vendor/autoload.php';
//...
test/exampletest1.php
<?php
require_once __DIR__.'/../vendor/autoload.php');
//...
See Basic Usage - Autoloading in Composer Documentation.
Usage in your projects
In the root directory of your project, add "grandt/phpepub": ">=4.0.3" to your composer dependencies and run composer install.
Let's say your project directory structure is :
project
vendor
public
index.php
composer.json
When you run composer install, Composer creates a directory vendor/ in the project root and generates an autoload file vendor/autoload.php.
To use the installed libraries in index.php, require the autoload file :
index.php :
<?php
require_once __DIR__."/../vendor/autoload.php";
//...
For a quick detailed explanation, try reading Juan Treminio - Composer Namespaces in 5 minutes
Related
I have a very simple PHP/Composer application with the following structure:
- src
- content
- test
- Sandbox.php
Sandbox.php has only a static function to print "test" and its namespace is
namespace MyApplication\Content\Test;
My autoload.php has MyApplication an "autoload" property.
"autoload" : {
"psr-4": {"MyApplication\\": "src/"}
},
I run composer install --no-dev in an Windows environment with XAMPP and in a test.php file I do (for the sake of a very simple test):
$autoloadFile = __DIR__ . '/wp-content/plugins/sandbox/vendor/autoload.php';
require $autoloadFile;
echo 'autoload = ' . file_exists($autoloadFile);
echo '<br />';
echo 'class_exists = ' . class_exists('MyApplication\Content\Test\Sandbox');
When I run this test.php file locally, it works perfectly. MyApplication is loading Sandbox class.
However, when I release this to my server which is a Linux based server but running on the same PHP version, Sandbox class is not found.
I made sure my /vendor/ folder is properly uploaded as well.
I'm wondering if the problem is happening because I'm running composer install on a Windows environment while it should be running the same command in my server (which I can't at the moment). Shouldn't the /vendor/ folder upload be enough to make the autoload classes work well?
Path to your file is src/content/test/Sandbox.php and according to PSR-4 it should be src/Content/Test/Sandbox.php - on Windows it does not matter, but on Linux it does.
I'm developing a brand new Wordpress plugin and I would like to use Composer to autoload classes.
Here is the plugin directory heriarchy:
my composer.json content:
{
"autoload": {
"psr-4": {
"G4S_ECommerce\\": "src"
}
}
}
In the directory where composer.json is, on cmd, I execute:
composer install -> this generates the vendor/composer folder and the vendore/autoload.php.
composer composer dumpautoload -o -> outputs "Generated optimized autoload files containing 0 classes"
In the main file G4S_Ecommerce.php I put the following line:
require __DIR__.'/vendor/autoload.php';
In the same file I put
use G4S_Ecommerce\Includes\Ecommerce;
$starter = new Ecommerce();
but it leads me to a Fatal error: Uncaught Error: Class 'G4S_Ecommerce\Includes\Ecommerce' not found
Why the composer dumpautoload -o returns 0 classes? What am I doing wrong?
Thanks
First (it is not obvious from your files structure) you need to set a namespace for your Ecommerce class (i.e., G4S_Ecommerce/Includes)
Second, based on what you've declared in the autoload directive, composer is expecting to find the G4S_Ecommerce folder under the src folder, and in that folder you need to place your php class file with a name identical to the class name (i.e., Ecommerce).
I'm new to Composer and I'm really struggling to auto-load my classes with composer. What am I missing in the following process?
I installed the package in my PHP includes folder (which is outside the document root - I'm not sure if that matters) like this:
composer require monolog\monolog
It stated it completed successfully and I confirmed the project was added to my vendor folder.
My entire composer.json file looks like this:
{
"require": {
"monolog/monolog": "^1.22"
}
}
My entire test file looks like this:
<?php
require_once "vendor/autoload.php";
use Monolog\Logger;
$log = new Logger("name");
?>
And I get this error when I load the page:
Fatal error: Uncaught Error: Class 'Monolog\Logger' not found in C:\Dropbox\Projects\Web\Websites\Instamation\wwwroot\qbtest.php:6 Stack trace: #0 {main} thrown in C:\Dropbox\Projects\Web\Websites\Instamation\wwwroot\qbtest.php on line 6
It includes the vendor/autoload.php file without any error.
I've tried to run these commands in composer without any change:
composer update
composer dump-autoload -0
I've also tried it with different packages and I get the same error, so I'm pretty sure it has nothing to do with the monolog package.
Is there a step here I'm missing? I don't need to manually define which classes to autoload in a json file if I require them in composer, do I?
Edit 1:
As requested, here's the paths to my different files.
Path to the test page:
C:\Dropbox\Projects\Web\Websites\Instamation\wwwroot\qbtest.php
Path to the composer.json file (outside the document root but in my includes path):
C:\Dropbox\Projects\Web\Websites\Instamation\wwwincludes\composer.json
My vendor folder is here:
C:\Dropbox\Projects\Web\Websites\Instamation\wwwincludes\vendor\
And inside my vendor folder I have these folders and file:
bin/
composer/
monolog/
psr/
autoload.php
You need to include autoload in your qbtest.php as following:
require_once "../wwwincludes/vendor/autoload.php";
use Monolog\Logger;
$log = new Logger("name");
I'm developing a PHP component called php-app-config using composer.
This component, once required by another project, and installed using composer install, should look for config files inside the config folder of the root package, something like root_package/config/config.yml.
The ./config/config.yml should exists only in the root package and not inside the component imported by the "require:" in composer.json, as below:
▾ root-package/
▸ bin/
▸ build/
▾ config/
▸ locales/
config.yml
▸ src/
▸ tests/
▾ vendor/
▸ composer/
▸ phpdocumentor/
▸ phpspec/
▸ phpunit/
▾ robotdance/
▾ php-app-config/
▾ src/
Config.php -> how to get the root "config/config.yml" path from here?
▸ tests/
composer.json
composer.lock
phpunit.xml
README.md
The root package can be a web app or command line utility.
Is there any way to get the root package path using composer? If not, what is the better way?
Using ReflectionClass:
$reflection = new \ReflectionClass(\Composer\Autoload\ClassLoader::class);
$vendorDir = dirname(dirname($reflection->getFileName()));
You can use composer's very own \Composer\Factory::getComposerFile(); to get to the project root directory:
$projectRootPath = dirname(\Composer\Factory::getComposerFile());
And with your case, you can access your root-package/config/config.yml by:
$configYmlPath = $projectRootPath . '/config/config.yml'
Don't forget to add composer to your dependencies for the \Composer\Factory::class to be available:
$ composer require composer/composer
I would suggest "anchoring" your application (web or cli) by defining the root path as a constant.
When you have for instance a root-package/src/application.php file, it should know where it lives, something like define('APP_ROOT_FOLDER', dirname(__DIR__)); could help. Once the constant is declared, it's available for dependencies, too.
So, in /php-app-config/Config.php you would simply use the constant:
$config = APP_ROOT_FOLDER . '/config/config.yml';
(Or define a APP_CONFIG_ROOT_FOLDER constant which points directly to the config folder of the application.)
You could also try go some folder levels up from the dependency.
In php-app-config/Config.php you would use __DIR__, which is root-package/vendor/robotdance/php-app-config/src. Now, you would need to go 4 levels up to reach root-package/.
$config = __DIR__.'/../../../../config/config.yml';
This will not work out, when your application gets packaged as a PHAR.
Is there any way to get the root package path using Composer?
If you have the Composer object, you can get the path of the vendor directory from the Config object:
$vendorPath = $composer->getConfig()->get('vendor-dir');
then, go one folder up $config = dirname($vendorPath) . '/config/config.yml';
I'm trying to install Slim on my local LAMP server on Ubuntu only I get stuck at second base.
From my tutorial and various documentation found online:
You now have access to the composer command. Sure enough if I go to
the terminal and enter:
$ composer
Composer version b474944155429eb4cce186c746d55287ee6bb3f4
Usage:
[options] command [arguments]
The next step is to specify Slim as a required package for your app.
This can be accomplished, via a composer.json file within the root of
your project.
Where is the root of my project? I thought it would be
/var/www/slim
I've tried adding composer.json to:
/var/www/slim
and stood in /slim define an index.php script with:
<?php
require 'vendor/autoload.php';
$app = new \Slim\Slim();
$app->get('/hello/:name', function ($name) {
echo "Hello, $name";
});
?>
Go to
http://localhost/var/www/slim
and the browser returns:
Warning: require(vendor/autoload.php): failed to open stream: No such file or directory in /var/www/slim/index.php on line 2
Fatal error: require(): Failed opening required 'vendor/autoload.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/slim/index.php on line 2
Well, there seems to be more then one question in your post, but regardless ...
"Where is the root of my project?"
The root of your project should be in /var/www if you haven't changed your hosts / Apache settings.
Then on to your 2nd question, which I will take the liberty to rephrase :)
"How to create your web App, and include the composer-installed packages inside?"
go to your respective web-root directory, likely in your case /var/www and in it create "index.php". Then, there, in the console, run the command:
composer install
This should install the packages defined in your composer.json, which should in the same web root dir.
If everything went OO, you will have in the following a new directory: /var/www/vendor
Now, go to your web-root directory and create your index.php and in it, in the beginning add the following lines:
require 'vendor/autoload.php';
// to load all the packages installed with composer, not only slim. If
//composer was run in the right directory and w/o problems of course :)
$app = new \Slim\Slim();
// to instantiate Slim script instance and assign it to the pointer $app
$app->get('/hello/:name', function ($name) {
echo "Hello, $name";
})
//to create your 1st route ...
you need to run
composer install
from terminal. After that, add
$app->run();
in index.php.