What namespace do I use to load a composer class in CodeIgniter? - php

I am writing a composer package that I want to use in multiple CodeIgniter projects. Answers to that question can explain how to modify both package and application.
The package
I created a composer package called steevedroz/codeigniter-twig. Its composer.json looks like this:
{
"name": "steevedroz/codeigniter-twig",
"type": "library",
"description": "A simple CodeIgniter library that allows using Twig along with the usual CI helpers",
"keywords": ["codeigniter", "twig"],
"homepage": "https://gitlab.com/SteeveDroz/codeigniter-twig",
"license": "MIT",
"authors": [{
"name": "SteeveDroz",
"role": "Developer"
}],
"require": {
"php": ">=7.0",
"twig/twig": "^2.5"
},
"autoload": {
"psr-4": {
"CodeigniterTwig\\": "/"
}
}
}
It contains a main class that I'd like to load from CodeIgniter, located at the root of the package. It's called Twig.php and contains:
<?php
namespace CodeigniterTwig;
class Twig {
// ...
}
The CodeIgniter project
My POC project is a basic CodeIgniter project with a composer.json file at the root (next to application/, system/, etc.) that contains:
{
"require": {
"steevedroz/codeigniter-twig": "dev-master"
}
}
I ran composer install that loaded my package correctly.
In application/config.php, I set:
$config['composer_autoload'] = '/vendor/autoload.php';
I one of my controllers, I'd like to load my Twig class as a library like this:
$this->load->library('CodeigniterTwig/twig');
But when I check the result, I get the error:
Unable to load the requested class: Twig
I tried with different namespaces (see below), but the result is always the same!
steevedroz/CodeigniterTwig/twig
CodeigniterTwig/twig
twig
steevedroz/codeigniter-twig/twig
I guess the problem has to do with namespaces.
Any clue?

Related

Class not being autoloaded

I've made a little library for myself and I'm trying to autoload it into my laravel project, it installs fine but whenever I try to use the class it simply says it's not found.
I've checked all the classmap files in vendor/composer and it doesn't seem to be in any of them.
This is my composer.json for my lib:
{
"name": "my-user/aspect-parser",
"version": "1.0.0",
"type": "package",
"require": {
"nesbot/carbon": "^1.22"
},
"autoload": {
"psr-4": {
"AspectParser\\": "src/"
}
}
}
My file structure is:
AspectParser
src
Parser.php
It was an issue with the type in composer.json. I've changed it to library and it adds to the autoload classmap.

Unable to use composer package in laravel

I am trying to use the following class in a laravel 5 project. https://packagist.org/packages/mk-j/php_xlsxwriter#dev-master
I do the:
composer require mk-j/php_xlsxwriter
Then it gets downloaded in my vendor folder. However I am unable to use it in a controller as it cannot find the class XLSXWriter.
I then look at the composer.json of mk-j/php_xlsxwriter and see the following:
{
"name": "mk-j/php_xlsxwriter",
"description": "PHP Library to write XLSX files",
"keywords": ["php", "library","xls", "xlsx", "excel"],
"type": "project",
"homepage": "https://github.com/mk-j/PHP_XLSXWriter",
"license": "MIT",
"autoload": {
"classmap": ["xlsxwriter.class.php"]
},
"require-dev": {
"phpunit/phpunit": "4.3.*"
}
}
It looks like the class is not PSR-4 compliant. Should I tweak the composer.json here to try to make it compliant but then that does not see adequate as another developer would have to do the same.
What can I do to be able to use the class in a controller? I am not very PSR savvy.
Thank You

Composer autoloading vendor classes

I have 2 packages on Packagist.
https://packagist.org/packages/erayalakese/envato-market-api (A)
https://packagist.org/packages/erayalakese/envato-update-checker (B)
B requires A.
Now I'm using B package on my projects. But I'm getting Class 'erayalakese\Envato_Update_Checker' (package B) not found error.
This is my composer.json file
{
"name": "",
"description": "",
"require": {
"erayalakese/envato-update-checker": "^1.3"
},
"authors": ...
}
And my project file:
<?php
require_once(__DIR__.'/vendor/autoload.php');
new erayalakese\Envato_Update_Checker(...);
When I add this to my composer.json as temporary solution, it's working :
"autoload": {
"classmap": ["vendor/"]
}
But I'm not sure I really need to add vendor folder to autoload . I was expecting it will autoload my vendors automatically.
Can you tell me what's I'm missing?
You have to change the composer.json of both packages.
Both packages need to define a autoload section.
Referencing: https://getcomposer.org/doc/04-schema.md#classmap
erayalakese/envato-market-api
https://github.com/erayalakese/envato-market-api/blob/master/composer.json
{
"name": "erayalakese/envato-market-api",
"description": "Envato Market API to verify and download Envato purchases",
"authors": [
{
"name": "Eray Alakese",
"email": "erayalakese#gmail.com"
}
],
"require": {},
"license": "GPL v2",
"autoload": {
"classmap": ["Envato_Market_API.php"]
}
}
Now this package has a autoload classmap definition, which consists of one PHP file. When you composer install, the autoload definition of the package will be added to the Composer Autoloader.
Same game for the other package:
erayalakese/envato-update-checker
https://github.com/erayalakese/envato-update-checker/blob/master/composer.json
{
"name": "erayalakese/envato-update-checker",
"description": "Checks Envato WordPress plugins' updates and download its if any update available",
"require": {
"erayalakese/envato-market-api": "^1.0"
},
"authors": [
{
"name": "Eray Alakese",
"email": "erayalakese#gmail.com"
}
],
"license": "GPL v2",
"autoload": {
"classmap": ["Envato_Update_Checker.php"]
}
}
In your main project:
require the "updater" package in the composer.json of your main project
the updater packages included the api package via it's require section (so you get both)
add require_once(__DIR__ . '/vendor/autoload.php'); to the project bootstrap
enjoy Class via Composers Autoloader: new erayalakese\Envato_Update_Checker(...);
Remove this line:
https://github.com/erayalakese/envato-update-checker/blob/master/Envato_Update_Checker.php#L11

Composer not composing - saying file not found

I'm using the excellent phpwkhtmltopdf library and want to update to latest version and for this I need to use composer.
File structure:
vendor
--mikehaertl
--php-shellcommand
--php-tmpfile
autoload.php
Composer.json file:
{
"name": "mikehaertl/phpwkhtmltopdf",
"description": "A slim PHP wrapper around wkhtmltopdf with an easy to use and clean OOP interface",
"keywords": ["pdf", "wkhtmltopdf", "wkhtmltoimage" ],
"homepage": "http://mikehaertl.github.com/phpwkhtmltopdf/",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Michael Haertl",
"email": "haertl.mike#gmail.com"
}
],
"require": {
"php": ">=5.0.0",
"mikehaertl/php-tmpfile": "1.0.*",
"mikehaertl/php-shellcommand": "1.0.*"
},
"autoload": {
"psr-4": {
"mikehaertl\\wkhtmlto\\": "src/"
}
},
"extra": {
"branch-alias": {
"dev-master": "2.0.x-dev"
}
}
}
I'm trying to use the library like this:
require '/home/bookmark/vendor/autoload.php';
use mikehaertl\wkhtmlto\Pdf;
...
$pdf = new Pdf('http://anysite.com'); <-- error points to this line
The problem is I get the error:
Fatal error: Class 'mikehaertl\wkhtmlto\Pdf' not found in /home/bookmark/public_html/ajax/action.php on line 132
This is my first time using composer, any idea what I'm doing wrong?
If you are using some package, you must not copy their composer.json file - that won't work.
The best thing would be to run composer init once to create an initial composer.json file for your project, and composer require mikehaertl/phpwkhtmltopdf:~2.0 to add this package you want to work with.
After that, it should work.

Composer package class not found

I'm trying to create a composer package but I'm getting a class not found error. I'm using Laravel 4 but I'm trying to create a generic PHP package.
I created the package in my vendor/ directory.
vendor/
theninthnode/
defaqto/
src/
Defaqto.php
vendor/
composer.json
Here is my composer.json file
{
"name": "theninthnode/defaqto",
"description": "",
"authors": [
{
"name": "Billy Jones",
"email": "billyjones26#gmail.com"
}
],
"minimum-stability": "dev",
"require": {
"php": ">=5.3.0"
},
"autoload": {
"classmap": ["src"]
}
}
And my class:
<?php
class Defaqto
{
....
}
When I try to call the package class from my app like so
$defaqto = new Defaqto();
I just get
Class 'Defaqto' not found
I've tried composer dump-autoload from within my package and also my applications root.
I also tried adding theninthnode/defaqto to my root composer.json file.
Am I missing something?

Categories