Search code examples
phpepub

having difficulty setting up phpepub library


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 


Solution

  • 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