Adding Twilio To Yii 1.1 Project

Hi,

I have added the twilio library (twilio-twilio-php-latest-1-g348f5f2.zip from github) to yii 1.1.14 but in order to get it to work I had to make a change to the YiiBase.php file.

with a simple PHP page (outside of yii) and the twilio folder in the same folder as the php file, the twilio functions work perfectly.

I followed steps found at http://www.yiiframework.com/doc/guide/1.1/en/extension.integration to load the library in the vendor section.

Inside YII, I created the following structure:




protected

    vendor

        twilio

            Services

                Twilio

            docs

            tests



I created the CommsController.php file:




Yii::import('application.vendor.twilio.*');

require_once('Services/Twilio.php');


class CommsController extends Controller

{

        public $sid = "<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />?"; // Your Account SID from www.twilio.com/user/account

        public $token = "<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />?"; // Your Auth Token from www.twilio.com/user/account

        

        public function getSid()

        {

            return $sid;

        }

        

        public function getToken()

        {

            return $token;

        }

        

	public function actionSendsms()

	{

		$this->render('Sendsms');

	}


        public function actionReceivesms()

        {

                $this->render('Receivesms');

        }

}



At this point, it would load Services/Twilio.php but would throw an error saying that Services_Twilio_Resource.php could not be found.

I tracked through the twilio library and found that the underscores needed to be replaced with / so it could read the file at twilio/Services/Twilio/Resource.php so i got it working after I inserting the following line above line 427 in yii/framework/YiiBase.php.

$className = str_replace(’_’, ‘/’, $className);

Its obviously not a good idea to be customising the framework files to make 3rd party extensions work so if someone can tell me how it should be done correctly it would be greatly appreciated.

The insertion of the line hasnt harmed the project, everything works perfectly so happy with the result, just not overly happy about having to hack the YiiBase.php file to do make it work.

Regards

Greg Johncock

Yii’s autoloader sometimes doesn’t play nice with other autoloaders but there’s an easy workaround to make sure that autoloaders will be called in the right order:




spl_autoload_unregister(array('YiiBase', 'autoload'));

require_once 'Services/Twilio.php';

spl_autoload_register(array('YiiBase', 'autoload'));



Brilliant!

Thank you so much.

Regards

Greg J

Thank you very much.

So much support for YII from YII developer