I am using Codeigniter since a year and I also started learning Laravel recently and I noticed that having a composer in your framework really helps you in a many ways.
I notice that the Codeigniter 3 has this option in config.php file to add a composer in it.
$config['composer_autoload'] = TRUE;
so I am thinking to add a composer in my CI.
Is it best practice to add a Composer in CI?
What should be the directory structure for that and is it work smoothly with the CI?
This is how I implemented composer in CodeIgniter 3.It is very easy. You have to install composer on your machine and I think you have it because you use laravel.
First copy and paste composer.json file in the project folder to application folder
Secound in the config.php file $config['composer_autoload'] = TRUE;
Now you have composer in your project. I will saw you an example like how to install mpdf using composer
Open cmd and direct to application folder
Inside application directory Type composer require mpdf/mpdf
Now a vendor folder will be created inside application folder and inside vendor
folder you can see all your packages downloaded by composer.
Now since you autoloaded composer now you can just use the code given by mpdf official manual like
function m_pdf()
{
$mpdf = new mPDF();
// Write some HTML code:
$mpdf->WriteHTML('Hello World');
// Output a PDF file directly to the browser
$mpdf->Output();
}
You can install any packages in https://packagist.org/ like mpdf very simply like this. Remember you don't need to type require_once APPPATH.'/vendor/mpdf/mpdf/mpdf.php'; since you already autoloader composer. If not prefer to autoload composer you must type require_once APPPATH.'/vendor/mpdf/mpdf/mpdf.php' at the beginning of each controllers where you use the mpdf vendor libraries.
in my experience, the best way is installing composer and dependencies independent of CodeIgniter, like 'composer require ', and then give access to the packages in your base controller.
add a file MY_Controller.php to application/core folder and make it extend CI_Controller. then add require_once(APPPATH . 'vendor/autoload.php') at the top. make all your controllers extend this base class, and you have access to all your packages. the same goes if you wanna access them in your models. make the base class MY_Model.php
Related
I'm trying to make a composer package, but I'm struggling to setup the autoload to work in the project where I'm developing it.
I have two projects:
Project Foo (where I'm developing the package).
Project Bar (where I've installed the package: composer require myhandle/mypackage).
In Project Foo I (obviously) have to be able to use the package as well. But setting up the autoload in my own package is not working.
However... When I commit the changes to Github, update the package at Packagist and run composer update in Project Bar, then it works(?!).
And wierdly enough... If I do composer update from Project Foo (the project where it isn't working). So updating the package to it's current version (?), makes it start working.
So it must somehow be related to how I've setup the autoload.
A note:
I started the package by making a new (empty) folder in the vendor directory, and then build the package there. I figured it was smart, since it then would mirror how it would look had I composer required the package.
I don't know if this is bad practice, since Composer no longer 'are in control' of all files in the vendor directory.
There are tons of guides on how to make a composer package out there, - but non of them explains about a good way to structure the files in the project where the package is being developed.
Here's what I do to 'get the error' (in Project Foo):
Create new class file, such as: myhandle/mypackage/src/Test.php
Then I instantiate it like this: $test = new MyNamespace\MyPackageName\Test();
And then I get the error:
Fatal error: Uncaught Error: Class 'MyNamespace\MyPackageName\Test' not found
And this is what works in Project Bar (the very same code).
I can't find a guide on how to correctly setup autoload in the package I'm developing. I'm using this autoload file, that I found in another composer project. I've put it in the root of my project. It looks like this:
<?php
namespace MyNamespace\MyPackageName;
spl_autoload_register(function($cls) {
$cls = ltrim($cls, '\\');
if (strpos($cls, __NAMESPACE__) !== 0) {
return;
}
$classWithoutBaseNamespace = str_replace(__NAMESPACE__, '', $cls);
// Load files from 'src' directory based on their class name without
// the StoutLogic\AcfBuilder namespace.
$path = dirname(__FILE__).
DIRECTORY_SEPARATOR.
'src'.
str_replace('\\', DIRECTORY_SEPARATOR, $classWithoutBaseNamespace).
'.php';
require_once($path);
});
I can't find it in the Composer Documentation, how to set it up in a new project/package. However I can find a bazillions guides on how to use autoload.
As yivi and Daniel Protopopov pointed out:
Check the documentation at getcomposer.org regarding autoloading
Delete your custom autoloader definition, register your namespace in composer.json (hoping you follow PSR-4 already), run composer dump-autoload.
Last but not least, when- and wherever you need to use it, just include the
require __DIR__ . '/vendor/autoload.php';
I'm new to mpdf and I'm having a problem implementing it on my project. I saw a link in Facebook that has a tutorial for mpdf, I read and followed all the instructions but unfortunately there is still an error about some deprecated value.
Here is the link I followed : http://codesquery.com/convert-html-pdf-codeiniter-using-mpdf/
And here is the screenshot of the problem:
I hope you guys can help me.
I guess you use php 7. You must change names of function. In old php for example 5.19 the same name of function and class means that this function is a constructor. In PHP 7 it must be for example:
public function __construct(mPDF $mpdf){}
Click here to see a list of all modification
You can easily install mpdf by using composer in codeigniter. This is how I implemented composer in CodeIgniter 3.It is very easy. You have to install composer on your machine download it from https://getcomposer.org/. After installing composer in your pc,
Copy and paste composer.json file in the project folder to application folder.
In the config.php file $config['composer_autoload'] = TRUE;
Now you have composer in your project. Now i will saw you how to install mpdf using composer
Open cmd and direct to application folder
Inside application directory Type composer require mpdf/mpdf
Now a vendor folder will be created inside application folder and inside vendor folder you can see all your packages downloaded by composer.
Now since you autoloaded composer now you can just use the code given by mpdf official manual like in your controllers.
function m_pdf(){
$mpdf = new mPDF();
// Write some HTML code:
$mpdf->WriteHTML('Hello World');
// Output a PDF file directly to the browser
$mpdf->Output();
}
Remember you don't need to type require_once APPPATH.'/vendor/mpdf/mpdf/mpdf.php'; since you already autoloader composer. If not prefer to autoload composer you must type require_once APPPATH.'/vendor/mpdf/mpdf/mpdf.php' at the beginning of each controllers where you use the mpdf vendor libraries. If you encountered any problems please comment below.
Problem
I am trying to use the PayPal Checkout REST SDK which requires the PayPal library to be autoloaded through composer. I have gone through the steps to enable Composer in CodeIgniter 3 but when I go to my controller where I am autoloading the PayPal\Rest\ApiContext class I get the following error:
Fatal error: Class 'PayPal\Rest\ApiContext' not found in
C:\xampp\htdocs\php\toucan-talk\App\modules\paypal\controllers\Paypal.php
on line 15
What I have so far
Here is my composer.json file
{
"require": {
"paypal/rest-api-sdk-php" : "*"
}
}
I have set $config['composer_autoload'] = TRUE; in my config.php file.
Here is my controller
<?php
use PayPal\Rest\ApiContext;
class Paypal extends MX_Controller
{
public function __construct()
{
$api = new ApiContext(
);
var_dump($api);
}
}
Question
How do I troubleshoot composer and its autoloader so that I can pinpoint where the autoload process is failing.
Well here is the solution: in config.php instead of setting...
$config['composer_autoload'] = TRUE;
You need to put
$config['composer_autoload'] = FCPATH . 'vendor/autoload.php';
however I am still not certain why this works as opposed to the original documentations recommendation. Bit of a headache really.
I know this is quite an old Question but still, i want to put my input that basically CodeIgniter 3 specifically 3.1.6 have problem that when you do
$config['composer_autload'] = TRUE:
then it won't work and doesn't autoload the files from vendor folder now there are 2 possible solutions for this one solution already mentioned by "Jethro Hazelhurst".
The other simple solution is that copy composer.json inside application folder and then run composer install from the application folder or just move vendor folder along with composer.json and composer.lock to the application folder and then this TRUE value will work properly.
Thanks.
You can use composer dump-autoload in command prompt. This will regenerate list of files to be included in the project.
For anybody else that might have this issue.
Codeigniter 3 has third_party folder inside application, use that for putting vendor directory (vendor directory contains the libraries).
composer.json (/composer.json)
tell composer to use the vendor directory inside third_party folder to store libraries
"config": {
"vendor-dir": "application/third_party/vendor"
},
config.php (/application/config/config.php)
Tell Codeigniter 3 to autoload all libraries from third_party/vendor folder
$config['composer_autoload'] = APPPATH . 'third_party/vendor/autoload.php';
PS: Autoloading All Libraries On Every Request could result into a slow website if there are many dependencies and a moderate to heavy traffic site. Load libraries as needed if that is the case.
I'm trying to load a class that has an underscore in it in ZF2.
This is the project I want to use: https://github.com/PHPGangsta/GoogleAuthenticator
The folder paths look like this:
/module
/Application
/Service
/MyService.php
/vendor/
/PHPGangsta
/GoogleAuthenticator.php
GoogleAuthenticator.php has a class named PHPGangsta_GoogleAuthenticator which I want to use in MyService.php, without having to require any files.
Also, I cannot change files inside PHPGangsta, because the project is submoduled under git.
Can you help configure zf2 class autoload?
Assuming you installed ZF2 using Composer (which is the recommended method), edit your composer.json to add phpgangsta/googleauthenticator to the require section. Then run composer install. That's it. You should then be able to use the library in your application - you don't need to do any additional autoload configuration.
Good afternoon, I will start off by saying that I've never included a composer project in my projects unless it was a composer package for the Laravel framework. Where you "require" it and add it to the providers array and aliases if needed.
Now, the problem. I have a composer package I'm trying to play around with https://github.com/WHAnonymous/Chat-API the problem is i dont know how to include it into myu project since its not really made "for" laravel. So I'm trying to use plain php without a framework but I have no idea how to "load" the package in, tried googling and only found information regarding building a package.
To clarify: I can install the package fine, its the php part of "loading" the package in my index.php file that im struggling with, pretend the index.php file is empty.
Can someone please help me?
After installing the package with composer, the composer has generated an autoloader that you can include with:
require_once 'vendor/autoload.php';
Then you can use the classes of the package without further includes. In your example this might be:
// Create an instance of WhatsProt.
$w = new WhatsProt($username, $nickname, $debug);
(taken from https://github.com/WHAnonymous/Chat-API/blob/master/examples/exampleRegister.php)
Note that this line from the example is not necessary, when you use the composer autoloader:
require_once('../src/whatsprot.class.php');
Assuming you have composer installed and have gone through the basics in the link posted by Paul.
You would run the following
~/composer install (same directory your composer.json file resides in).
Contents of composer.json file would be:
{
"require" : {
"whatsapp/chat-api" : "2.5.4"
}
}