Yii import functionality - php

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

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.

Failed To Import My Class In Yii Extensions

I wrote a class CronTab_Manager to generating and managing cron jobs whith PHP. Now I want to access it everywhere in my project.
In my config file I set path of alias like below:
$cronTab = dirname(dirname(dirname(__FILE__))).'/extensions/CronTab_Manager';
Yii::setPathOfAlias('cronTab',$cronTab);
And also import that class in this way:
'import'=>array(
'application.models.*',
'application.components.*',
'cronTab.*',
),
My class isn't extended from other classes, and when I want to create an object of this class I receive this error:
include(CronTab_Manager.php): failed to open stream: No such file or directory
Any suggestion will be appreciated.
Thanks in advance.
you can put your class file inside models folder or components folder.
if it is as extension then
'import'=>array(
'application.models.*',
'application.components.*',
'ext.cronTab.*',
),
Set path like this:
Yii::setPathOfAlias('cronTab','ext.CronTab_Manager');

Zend Autoloader unable to find existing file

I'm having an unusual autoloading problem with my Zend website. Up until now autoloading has been working a treat. Now though, I added a new file the project and autoloading just can't find it. I've reduced the problem to the minimal test case and was wondering if anyone could help me out.
In my website I have a the usual directory structure, like so:
site/
application/...
library/
Zend/...
PHPUnit/...
Ext/
Extras/
Test.php
Service/
Test.php
I've correctly set up auto loading (as per other helpful comments on StackOverflow) and registered the Ext_ namespace, which is proved by being able to correctly instantiate Ext_Extras_Test.
The problem comes when I try to instantiate Ext_Service_Test. Autoloading "failed to open stream". I've checked the correct spelling, listed the directory contents using find, ls, and the file explorer to make sure that the file exists in the correct place.
I just can't get it to &^%%£* find the file! Does anyone have any clues?
Found it you are trying to override a resource autoloader, even though it's not specified I'm pretty sure this will effect all namespaces:
42.3.2. The Module Resource Autoloader Zend Framework ships with a concrete implementation of Zend_Loader_Autoloader_Resource that
contains resource type mappings that cover the default recommended
directory structure for Zend Framework MVC applications. This loader,
Zend_Application_Module_Autoloader, comes with the following mappings:
forms/ => Form
models/ => Model
DbTable/ => Model_DbTable
mappers/ => Model_Mapper
plugins/ => Plugin
services/ => Service
views/
helpers => View_Helper
filters => View_Filter
As an example, if you have a module with the prefix of "Blog_", and
attempted to instantiate the class "Blog_Form_Entry", it would look in
the resource directory's "forms/" subdirectory for a file named
"Entry.php".
When using module bootstraps with Zend_Application, an instance of
Zend_Application_Module_Autoloader will be created by default for each
discrete module, allowing you to autoload module resources.

yii vendors for all sites

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
)

php zf project: db classes in models directory do not get auto-included

I'm trying to create a Zend Framework project using PHP 5.3.2 and Zend Framework 1.10.3.
i created the source files of the project using the zend framework tool and the db related classes i created with zend-db-model-generator.
example:
in models directory i have the following:
FbUser.php - class Default_Model_FbUser
FbUserMapper.php - class Default_Model_FbUserMapper
DbTable/FbUser.php - class Default_Model_DbTable_FbUser
The models in the models directory should be included automatically when I use them in one of the controllers, but it seems that it doesn't.
what should i configure in order for the db class models will be auto-included when used ?
update
I tried adding the following code to the bootstrap file:
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('Default_');
the autoloader still looks for Default/Model/FbUser.php in include path instead of Model/FbUser.php in the zf project.
I did not need to use Zend_Loader_Autoloader at all, although it's a cool component to auto-load classes in your include path.
i need to add to application.ini which is the main config of the application, the following line:
appnamespace = "Default_"
The program understands the application name space and then loads the db model classes properly.

Categories