Codeigniter 3 composer and vendor directory

11.9k Views Asked by At

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?.

3

There are 3 best solutions below

3
On BEST ANSWER

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

0
On

This will open and close the connection

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

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

class Welcome extends CI_Controller {

    public function index()
    {
        $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
        $channel = $connection->channel();
        $channel->queue_declare('hello', false, false, false, false);

        $msg = new AMQPMessage('Yur message goes here....');
        $channel->basic_publish($msg, '', 'hello');

        $channel->close();
        $connection->close();

        $this->load->view('welcome_message');
    }
}
0
On

Change composer_autoload config in /application/config/config.php like below $config['composer_autoload'] = 'vendor/autoload.php';