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
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.
second
and inside php-amqplib
and inside php-amqplib
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?.
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;
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