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
Related
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?
there is a syntax error in my composer.json file but I just can't seem to find the error. I already have a Laravel object on top of the file, but I also want to add Goaop, like the following code.
how can I do it? thank you
//newly added code
{
"name": "goaop/goaop-laravel-bridge",
"description": "Integration bridge for Go! AOP framework",
"type": "library",
"keywords": ["bridge", "laravel", "aop", "php", "aspect"],
"require": {
"goaop/framework": "^1.0|^2.0",
"laravel/framework": "^5.0"
},
"license": "MIT",
"authors": [
{
"name": "Lisachenko Alexander",
"email": "lisachenko.it#gmail.com"
}
],
"autoload": {
"psr-4": {
"Go\\Laravel\\GoAopBridge\\": "./src"
}
}
}
Looks like you just copied the package's composer.json and pasted it to the bottom of yours. That is not how you install packages.
From your command line run:
composer require goaop/goaop-laravel-bridge
This will update your composer.json and .lock files and install the package.
I am in the midst of learning how to better maintain the code that I write. I have two projects that I am currently working on - both are in development and both are pushed to GitHub.
I am trying to include the one project in the other (by declaring it a dependency in the one project's composer.json file) - but after it is included, it is missing the proper autoload configurations.
I'll explain a little better: Project A's composer.json file has an autoload line for "psr-4" mapping the namespace to the "src" directory - all is good and working after I run:
composer install
I am then able to include the /vendor/autoload.php file and immediately start working with the project. When I look at the generated "autoload_psr4.php" file in that project's vendor directory, I can see the array contains a reference for the mapped namespace / directory per the composer.json file.
The problem comes when I try to include Project A within Project B as a dependency - everything looks to work after running the composer install - the vendor directory is created and the files are copied there - however when I look at the generated "autoload_psr4.php" file in project B it is missing the mapped namespace / directory per Project A's composer.json file.
Can someone point me in the direction for what I might be missing to get the autoload line to carry over into Project B?
Here's the two composer.json files:
Project A:
{
"name": "jfreynik/hydra-net",
"description": "Network classes for the Hydra framework.",
"type": "library",
"keywords": [ "http", "net", "rest" ],
"license": "MIT",
"require": {
"php": ">=5.3.0"
},
"autoload": {
"psr-4": {
"hydra\\net\\": "src/"
}
}
}
Project B:
{
"name": "jfreynik/simple-mvc",
"description": "A pretty simple MVC framework for building websites.",
"type": "framework",
"keywords": [ "MVC", "Website Framework" ],
"license": "MIT",
"repositories": [
{
"type":"package",
"package": {
"name": "jfreynik/hydra-net",
"version":"*-dev",
"source": {
"url": "https://github.com/jfreynik/hydra-net.git",
"type": "git",
"reference":"master"
}
}
}
],
"require": {
"jfreynik/hydra-net": "*-dev"
},
"minumum-stability": "dev"
}
Thank You!
The issue was that I was including Project A into Project B as a "package". I believe "packages" are not scanned for their contained composer.json files. (please correct me if I'm wrong) - by changing Project B's composer.json file to reference Project A as "vcs" then the autoloading works again. - Here is the adjusted composer.json file for Project B.
{
"name": "jfreynik/simple-mvc",
"description": "A pretty simple little MVC framework for building websites.",
"type": "framework",
"keywords": [ "MVC", "Website Framework" ],
"license": "MIT",
"repositories": [
{
"type":"vcs",
"url": "https://github.com/jfreynik/hydra-net"
}
],
"require": {
"jfreynik/hydra-net": "dev-master"
},
"minumum-stability": "dev"
}
I really had trouble finding working examples as I am really just starting to use composer and prior to this had only included professional packages from packagist. I have much to learn (even in regard to terminology) - so if anyone can add additional relevant information for the proper way to include 1 development project in another I will accept your answer.
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.
I'm not clear on what the best practice is for including external libraries in a custom module that I plan on distributing.
Normally I'd place the external library under the application's vendor directory. But I want to ensure that all dependencies are met when I distribute my custom module (i.e. I don't want to force people to manually download the dependency into their app's vendor directory).
Is the correct practice to include a vendor directory under the module directory as per the the following?
/application_dir
/vendor
/module
/my_module
/vendor
Use composer, you can find documentation here:
Composer Documentation
Basically you would modify the file composer.json in the root of your application and add your dependencies in the require section:
{
"name": "zendframework/skeleton-application",
"description": "Skeleton Application for ZF2",
"license": "BSD-3-Clause",
"keywords": [
"framework",
"zf2"
],
"homepage": "http://framework.zend.com/",
"require": {
"php": ">=5.3.3",
"zendframework/zendframework": "2.2.*",
"doctrine/common": "dev-master",
"zendframework/zendpdf": "2.*",
"zendframework/zendservice-recaptcha": "2.*",
"thiagoalessio/tesseract_ocr": ">= 0.1.2",
"zf-commons/zfc-user": "dev-master"
// add your requirements here**
}
}
if the dependancy is on a private github repository, you can add it like this:
{
"require": {
"vendor/my-private-repo": "dev-master"
},
"repositories": [
{
"type": "vcs",
"url": "git#bitbucket.org:vendor/my-private-repo.git"
}
]
}
Don't forget to do composer.phar update after your done adding them.