Search code examples
codeigniter-3vcf-vcard

Generate Vcard using Codeigniter 3


I am trying to use jeroendesloovere VCARD in Codeigniter 3 controller as below:

public function get_contact($unique_id)
{

            namespace JeroenDesloovere\VCard\VCard;
            require_once 'vendor/behat/transliterator/src/Behat/Transliterator/Transliterator.php';
            require_once 'vendor/jeroendesloovere/vcard/src/VCard.php';
            $vcardObj = new VCard();
            $status_db_customers['data']=$this->Site_model->get_customer_QR($unique_id);
            foreach ($data as $row){
                $vcardObj->addName($row->first_name. " " . $row->first_name);
                $vcardObj->addBirthday($contactResult[0]["date_of_birth"]);
                $vcardObj->addEmail($row->email);
                $vcardObj->addPhoneNumber($row->mobile);
                $vcardObj->addAddress($row->add_line_1);
            }
            return $vcardObj->download();

}

But this always gives an error.

Message: syntax error, unexpected token "namespace"

Please help me on how to use this ? This is inside my controller class.


Solution

  • Your controller should look like this:

    <?php
    defined('BASEPATH') or exit('No direct script access allowed');
    
    use JeroenDesloovere\VCard\VCard;
    
    class Welcome extends CI_Controller
    {
        public function get_contact($unique_id)
        {
            $vcardObj = new VCard();
            $status_db_customers['data']=$this->Site_model->get_customer_QR($unique_id);
            foreach ($data as $row){
                $vcardObj->addName($row->first_name. " " . $row->first_name);
                $vcardObj->addBirthday($contactResult[0]["date_of_birth"]);
                $vcardObj->addEmail($row->email);
                $vcardObj->addPhoneNumber($row->mobile);
                $vcardObj->addAddress($row->add_line_1);
            }
            return $vcardObj->download();
        }
    }
    

    Also make sure you've set up CodeIgniter to use Composer's autoloading file. For this you need to set $config['composer_autoload'] in application/config.php, depending on where Composer's vendor folder is:

    • If the vendor folder is inside the application folder, set it to TRUE:
      $config['composer_autoload'] = TRUE;

    • Or if the vendor folder is at a different location, specify it's path.
      For example, if the vendor folder is at the same level as the application folder, set it to vendor/autoload.php:
      $config['composer_autoload'] = 'vendor/autoload.php';