phpcodeigniterphp-amqplib

Codeigniter 3 composer and vendor directory


I have installed codeigniter 3.1.8 and installed this library https://github.com/php-amqplib/php-amqplib using composer:

composer require php-amqplib/php-amqplib

This has created a directory vendor enter image description here

I have looked the accepted answer on how to use the library here How to use composer packages in codeigniter?

but this is my directory structure in vendor.

enter image description here

second

and inside php-amqplib

enter image description here

and inside php-amqplib

enter image description here

If i look at index.php i find that fcpath has already been defined

// Path to the front controller (this file) directory
define('FCPATH', dirname(__FILE__).DIRECTORY_SEPARATOR);

require_once BASEPATH.'core/CodeIgniter.php';

and defining it again will result in an error.How can i get to use php-amqplib inside my controller methods?.


Solution

  • 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