yii vendors for all sites - php

folders structure:
htdocs:
--includes
----yii
------framework
--------vendors
----------my_vendor1.php
----------my_vendor2.php
--site1
--site2
How to import my_vendor1.php and my_vendor2.php for site1, site2 ?

Assuming that they are inside the yii/framework folder as you show, you can easily import anything in the Yii framework directory using the system Path Alias, like so:
Yii::import('system.vendors.*');
Assuming both sites using the same Yii installation, you can now call my_vendor1.php in either.
You can actually add this to each site's configuration file as well:
'import'=>array(
'application.models.*', // your regular site-specific imports
'system.vendors.*', // your custom imports
)

Related

Yii, importing library does not work

i'm trying to implement Identicon library to my site but i get the error that class is not found.
i tried with Yii::import('application.vendor.*'); so i put the library in vendor folder, but it does not work.
i also tried adding the library to the component controller but still gives me the error.
and i tried making the import in the view where the code will be
<?php
Yii::import('application.vendor.*');
$identicon = new Identicon;
$identicon->displayImage('test');
?>
and yet it tells me the error that this class is not found. i just copied the src folder from the zip to vendors and components. how can i import this library?
if your folder structure is like
-- root
-- protected
|---- vendors
|---- myfolder
|---- MyClass.php
you can import it like this
Yii::import('appplication.vendors.myfolder.MyClass');
since yiis auto load is based on file name, if class Identicon was defined in MyClass it wont get loaded because it has different file name, so in this case you have to go with:
Yii::import('appplication.vendors.myfolder.*' , true);
i finally imported Identicon library by editing every file from the library and store it into components/Identicon.
it seems like the use of namespaces that each file had on the code wasn't allowing Yii to import, so it works by deleting the namespaces and the use command on every file, then import it in the config file.
'import'=>array(
'application.models.*',
'application.components.*',
'application.components.Identicon.*'
),
Note: All files found in generator must be in the same folder that Identicon.php is place.
so you can use the library almost like the readme from Identicon says
$Identicon = new Identicon;
$identicon->displayImage('foo'); //Displays the image.

In yii 1.1.x move all forms in new /protected/forms/ directory

In all yii 1.1 examples I see that forms are kept in /protected/models/ subdirectory.
I want to move all forms in new /protected/forms/ directory. which is the safe way of this ?
Just move models in this subdirectory and add to import in config (main.php) this line:
'import'=>array(
'application.models.forms.*',
...
)

Yii Share models between two applications

I've two web applications which are located on one server but on different domains (for example app A is administration and app B is client).
The problem is that I want to share models (ActiveRecords) from application A to be available in application B.
Is there any clever way to do this?
Thanks!
Try to make an alias in one app to the second ( And from the 2nd to the 1st ;) ) using
YiiBase::setPathOfAlias()
Documentation:
http://www.yiiframework.com/doc/api/1.1/YiiBase#setPathOfAlias-detail
Sure, just follow a few easy steps:
1. Put your models in a shared directory
For example, if your current directory structure looks like this:
/www
/application1
/protected
/models
/application2
/protected
/models
Create another "shared" directory. It's a good idea to put some structure in there as well, in case you want to share more than just some models:
/www
/application1
/protected
/models
/application2
/protected
/models
/shared
/models
Put the active record models you want to share in /www/shared/models.
2. Alias the shared directory in both applications
Go to your main.php configuration file in both applications and create an alias for the shared directory:
Yii::setPathOfAlias('shared','../shared/'); // or use an absolute path
3. Import shared models
Still in your main.php configuration, import your shared models:
'import'=>array(
// ...existing imports here...
'shared.models.*',
),
You can now directly refer to the shared classes anywhere in your application and Yii will load the appropriate classes automatically.
If you later add more directories to /shared then simply add corresponding lines to the import configuration.
Yii::setPathOfAlias('applicationA','path/to/applicationA/protected');
Then when you making import in config:
'import' => array('applicationA.models.*'....
Now you will be able to use models from appA in appB.
Same can be done with modules, controllers and views.
Views - viewPath
Modules - modulePath
Controllers - in index.php add
$app->setControllerPath('////protected/controllers');
Before $app->run();

Sharing config among multiple applications in codeigniter

I am trying to create multiple application under 1 codeigniter application, however some of them would be sharing same config such as database config.
-application
-config
-models
-project_1
-config
-controllers
-models
-project_2
-config
-controllers
is it possible to load the config in the first level of application folder, and look for the application level's folder if the config file doesn't exist? I know it is possible when I load configs manually by using add_package_path(), but autoload doesn't work.
Why not keep it simple:
<?php
// This app has no config. Use the shared one
require_once('/path/to/common/config.php');
You should use packages:
http://ellislab.com/codeigniter/user-guide/libraries/loader.html
I tend to create a package called 'share' within the web root, i.e. packages/share
Inside your packages folder, you can create a folder structure similar that of the core CI application config, helpers, models, views etc.
Say you want to only have one DB file, you can place the db config file inside packages/share/config and call this file using the following within your applications database file:
include_once(FCPATH . 'packages/share/config/database.php');

Yii import functionality

In my main config i have:
'import' => array(
'application.models.*',
'application.components.*',
I read somewhere that Yii import only calls the relevant class when needed.
Wanted to know if it's true and if importing all the folder at once is good practice in Yii.
Thanks,
Danny
In the import call, when you pass in a folder like 'application.models.*', what Yii does, its add that path to the php include_path, so that when you call a class contained in that folder, if the Yii autoloader fails php looks for that class in the include path, thats fast.
If you include a file like 'application.models.FormModel', then Yii autoloader is aware of it and also loads the class on deman only.
you can find more information on the Yii guide: importing classes

Categories