How to Use Composer Packages in Codeigniter

How to use composer packages in codeigniter?

Credit to @jmadsen

This is possible by just getting the order of loading correct:

/*
* --------------------------------------------------------------------
* LOAD THE BOOTSTRAP FILE
* --------------------------------------------------------------------
*
* And away we go...
*
*/
// Composer Autoloader
require FCPATH . 'vendor/autoload.php';

require_once BASEPATH.'core/CodeIgniter.php';

/* End of file index.php */

How to use a composer package in CodeIgniter 4?

Try this

<?php

namespace App\Controllers;

use App\Controllers\BaseController;
use chillerlan\QRCode\{QROptions, QRCode};

class Qr extends BaseController
{
public function index($path)
{
$data = 'otpauth://totp/test?secret=B3JX4VCVJDVNXNZ5&issuer=chillerlan.net';

echo '<img src="' . (new QRCode)->render($data) . '" alt="QR Code" />';
}
}

Codeigniter 3 composer and vendor directory

IF your question is - How can i get to use php-amqplib inside my controller methods ?

STEP - 1 :

Allow or require the autoload.php file on very beginning, but on codeigniter you cannot do that in general.So change one line on config file that will automatically require autoload.php file.

CodeIgniter/application/config/config.php find :

$config['composer_autoload'] = FALSE; to $config['composer_autoload'] = TRUE;

STEP - 2 :

On your controller class like this :

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

class Welcome extends CI_Controller {
public function index()
{
// your business logic like this
$msg = new AMQPMessage($msg_body);
$ch->batch_basic_publish($msg, $exchange);
}
}
STEP - 3 :

Now you can call it via URL

http://example.com/Welcome

Now more ref: PhpAmqpLib

How to use composer packages embed/embed inside codeigniter

Install your repository('composer require embed/embed') via composer on your project root/home, where you can find default index.php.

Go to application>config>Config.php.

Look for $config['composer_autoload'].

It will be set to FALSE in default, just change it to TRUE.

Use use Embed\Embed; in your controller.

Now try using it in your controller it should be accessible.

use Embed\Embed;
class Home extends MY_Controller {

public function og_test()
{
//Load any url:
$info = Embed::create('https://www.youtube.com/watch?v=PP1xn5wHtxE');

//Get content info

echo $info->title; //The page title
echo $info->description; //The page description
echo $info->url; //The canonical url
echo $info->type; //The page type (link, video, image, rich)
echo $info->tags; //The page keywords (tags)

}

}

Adding composer into Codeigniter

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

  1. Open cmd and direct to application folder
  2. 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.

Adding composer to existing codeigniter project

You have to use the proper class - mPDF uses namespaces, and as documented at https://mpdf.github.io/installation-setup/installation-v7-x.html, you have to instantiate it as following:

$mpdf = new \Mpdf\Mpdf();

CodeIgniter Composer package.json locations shluld be in application or root folder?

Well, if you take a look at the Codeigniter Documentation you can see there is a Configuration setting called composer_autoload. This information can be found here.

If you set this true, Codeigniter tries to load Composer's autoload.php in the APPLICATION.'vendor' folder. If you take a closer look at the Codeigniter.php file, you'll see that you can define a directory for this setting too.

So in your case you don't have to change the package.json in the root folder because you need one in the application folder, in case you set composer_autoload to true, which i would recommend.

By the way if you try to install a package via composer in your application directory, composer asks you if you want to use the one it found on the root folder - just decline that and press n.

As you can see in the picture below (i just tried to install the mpdf package).

Sample Image



Related Topics



Leave a reply



Submit