• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using Composer with Codeigniter?

#1
I followed this nice tutorial by avenirer - http://avenir.ro/codeigniter-tutorials/c...-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.
Do the one thing you think you cannot do. Fail at it. Try again. Do better the second time. The only people who never tumble are those who never mount the high wire.
Reply

#2
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'); 
Reply

#3
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-6111...#pid315197
Reply

#4
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!
Do the one thing you think you cannot do. Fail at it. Try again. Do better the second time. The only people who never tumble are those who never mount the high wire.
Reply

#5
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
Do the one thing you think you cannot do. Fail at it. Try again. Do better the second time. The only people who never tumble are those who never mount the high wire.
Reply

#6
application/vendor is a better place. Under application/ there is the file .htaccess https://github.com/bcit-ci/CodeIgniter/b.../.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/b.../.htaccess

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

#7
Thank you Ivan!
Do the one thing you think you cannot do. Fail at it. Try again. Do better the second time. The only people who never tumble are those who never mount the high wire.
Reply

#8
(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 -
Reply

#9
(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
Reply

#10
(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.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.