Use 3rd-party classes out of Yii site directory

I’m trying to implement SimpleSAMLphp authentication to my Yii framework based site. Since SimpleSAMLphp is located out of web server’s DocumentRoot, it can’t be put anywhere under “protected” directory. So my question is how to register and use 3rd-party classes which are located out of Yii document root.

Here are the steps I made:

  1. Create new module. Location - /var/yii-site/protected/module/simplesamlphp

  2. Enable module in configuration (main.php):

‘modules’=>array(

<…>

 'simplesamlphp'=&gt;array(


     'simplesamlphpPath'=&gt;'/path/to/simplesamlphp',


 ),

<…>

  1. In /var/yii-site/protected/module/simplesamlphp/SimplesamlphpModule.php init() function:

<…>

$this->setImport(array(

  'simplesamlphp.models.*',


  'simplesamlphp.components.*',


  &#036;this-&gt;simplesamlphpPath . '/lib/_autload.php',

));

<…>

  1. This error is shown:

Alias "/path/to/simplesamlphp/lib/_autload.php" is invalid. Make sure it points to an existing directory or file.

I’m very new to Yii, so thanks for patience.

You can simple create an alias for your app in the index.php

http://www.yiiframework.com/doc/api/1.1/YiiBase/#setPathOfAlias-detail

Yii::setPathOfAlias(‘simplesamlphp’, ‘/path/to/simplesamlphp/folder’);

and then import what you need

Yii::import(‘simplesamlphp.*’);

Yii::import(‘simplesamlphp.whatever’);

Thanks for the help. Unfortunately, I fall into another problem - SimpleSAMLphp has its own autoloader, so it throws error when trying to create 3rd-party object:




Yii::setPathOfAlias('ssaml', $this->simplesamlphpPath . '/lib/_autoload.php');

Yii::import('ssaml');

$this->as = new SimpleSAML_Auth_Simple($this->authenticationSource); // <-- here comes error:



include(SimpleSAML_Auth_Simple.php): failed to open stream: No such file or directory

There is no such file, so it won’t be able to find it anywhere…

But this piece of code works fine:




// temporary disable Yii autoloader

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

    

// create 3rd-party object

require_once($this->simplesamlphpPath . '/lib/_autoload.php');

$this->as = new SimpleSAML_Auth_Simple($this->authenticationSource);

    

// enable Yii autoloader

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



It doesn’t look so “clean”, but at least it works. It would be great to use Yii methods to import classes, create objects, but I can’t find the way how to do that.

Hello Arminas.

Sorry for offtopic but could you please post your example code of using Yii with SimpleSAMLphp?