CodeIgniter Forums
Using Composer with Codeigniter? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Using Composer with Codeigniter? (/showthread.php?tid=61461)



Using Composer with Codeigniter? - lexxtoronto - 04-16-2015

I followed this nice tutorial by avenirer - http://avenir.ro/codeigniter-tutorials/codeigniter-with-composer/

I installed SimpleExcel and just wanted to test it.

Code:
use SimpleExcel\SimpleExcel;
//require_once('../vendor/faisalman/simple-excel-php/src/SimpleExcel/SimpleExcel.php');

class Tlc extends CI_Controller{
...
yada yada
...
   public function test_excel()
   {
       $excel = new \SimpleExcel\SimpleExcel('xml');
       $excel->writer->setData(
       array (
               array('Id','Name','Code'),
               array('1','Ed','323'),
               array('2','Des'.'222')
       )
       );
       
       $excel->writer->saveFile('example');
   }
   
It complains that SimpleExcel function cannot be found. I also tried - $excel = new SimpleExcel('xml');

As you can see I also tried a relative path to SimpleExcel.php file - in that case it complains that file cannot be loaded. I also tried an absolute path.


RE: Using Composer with Codeigniter? - silentium - 04-16-2015

That guide is confusing and weird about how to load composer packages.
It mentions in the guide there is a autoload option in the config file of CodeIgniter for Composer, but at the same time, it adds the autoload code manually to the config file... I have no clue why.

All you need is to set the $config['composer_autoload'] to TRUE or a path to your vendor/autoload.php if it is not located in your application folder.

From the config file
Code:
/*
|--------------------------------------------------------------------------
| Composer auto-loading
|--------------------------------------------------------------------------
|
| Enabling this setting will tell CodeIgniter to look for a Composer
| package auto-loader script in application/vendor/autoload.php.
|
| $config['composer_autoload'] = TRUE;
|
| Or if you have your vendor/ directory located somewhere else, you
| can opt to set a specific path as well:
|
| $config['composer_autoload'] = '/path/to/vendor/autoload.php';
|
| For more information about Composer, please visit http://getcomposer.org/
|
| Note: This will NOT disable or override the CodeIgniter-specific
| autoloading (application/config/autoload.php)
*/

In steps
  1. Create a composer.json file in your application folder
  2. Run Composer install to install the packages you need
  3. Use the packages in your code. If you did set the $config['composer_autoload'] to TRUE, they are all loaded and ready to use.

In your code, try changing
PHP Code:
$excel = new \SimpleExcel\SimpleExcel('xml'); 
to
PHP Code:
$excel = new SimpleExcel('xml'); 



RE: Using Composer with Codeigniter? - ivantcholakov - 04-16-2015

If there is still no success, just as an another example have a look at this post of mine at the old forum: http://forum.codeigniter.com/thread-61112-post-315197.html#pid315197


RE: Using Composer with Codeigniter? - lexxtoronto - 04-16-2015

Thank you guys! Im using CI 2, so I found a solution on SO after you guys mentioned autoload. Indeed it was a problem with autoload. I added -- require FCPATH . 'vendor/autoload.php'; -- in index.php file. Thanks for your time guys!


RE: Using Composer with Codeigniter? - lexxtoronto - 04-16-2015

One more question! The tutorial says that the 'vendor' folder should be inside the application folder, but from what I figure based on online research is that it should be in Codeigniter folder?
Codeigniter
-application->vendor
-system

or

Codeigniter->vendor
-application
-system


RE: Using Composer with Codeigniter? - ivantcholakov - 04-16-2015

application/vendor is a better place. Under application/ there is the file .htaccess https://github.com/bcit-ci/CodeIgniter/blob/2.2-stable/application/.htaccess
that protects php files to be accessed directly by the browser.

And you can update this .htaccess file as it is in CodeIgniter 3: https://github.com/bcit-ci/CodeIgniter/blob/3.0.0/application/.htaccess

See https://github.com/bcit-ci/CodeIgniter/issues/1631


RE: Using Composer with Codeigniter? - lexxtoronto - 04-17-2015

Thank you Ivan!


RE: Using Composer with Codeigniter? - polarthedog - 05-12-2016

(04-16-2015, 04:42 PM)lexxtoronto Wrote: Thank you guys! Im using CI 2, so I found a solution on SO after you guys mentioned autoload. Indeed it was a problem with autoload. I added  -- require FCPATH . 'vendor/autoload.php'; -- in index.php file. Thanks for your time guys!

I don't think this is the best way, especially if you are accessing things directly (without url) or with CLI -


RE: Using Composer with Codeigniter? - Batman - 10-31-2017

(05-12-2016, 07:26 AM)polarthedog Wrote:
(04-16-2015, 04:42 PM)lexxtoronto Wrote: Thank you guys! Im using CI 2, so I found a solution on SO after you guys mentioned autoload. Indeed it was a problem with autoload. I added  -- require FCPATH . 'vendor/autoload.php'; -- in index.php file. Thanks for your time guys!

I don't think this is the best way, especially if you are accessing things directly (without url) or with CLI -

This is the way that I finally got loading composer libraries to load also. If this is not the best way, what would you suggest is better.

CI 3.1.6


RE: Using Composer with Codeigniter? - Narf - 11-01-2017

(10-31-2017, 08:47 PM)Batman Wrote:
(05-12-2016, 07:26 AM)polarthedog Wrote:
(04-16-2015, 04:42 PM)lexxtoronto Wrote: Thank you guys! Im using CI 2, so I found a solution on SO after you guys mentioned autoload. Indeed it was a problem with autoload. I added  -- require FCPATH . 'vendor/autoload.php'; -- in index.php file. Thanks for your time guys!

I don't think this is the best way, especially if you are accessing things directly (without url) or with CLI -

This is the way that I finally got loading composer libraries to load also. If this is not the best way, what would you suggest is better.

CI 3.1.6

Don't worry, there's nothing wrong with it.