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');
Related
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.
I am trying to access a module's model from another module ,
ex:
protected/modules/module1/DefaultController
protected/modules/module2/DefaultController
i want to access module1 models from module2 ,
i tried
Yii::app()->getModule('module1');
$m = new module1;
it showing the error
include(module1.php): failed to open stream: No such file or directory
I believe that you did not declare your module in config (main.php):
'modules'=>array(
...
'module2'=>array(
...
),
),
Also check whether you have named the class module. It should be Module1Module.
And I can see that you do not have a the correct directory structure of the module. Controllers should be in the directory сontrollers:
/protected/modules/module1
Module1Module.php
controllers/
defaultController.php
views/
index.php
I configure YII-BOOTSTRAP using these steps
http://www.cniska.net/yii-bootstrap/setup.html
But unable to configure & Get the below error, as shown below: Any ideas?
Controller.php is the base class for all controllers in Yii. Make sure you're autoloading it by having this in config.main
// autoloading model and component classes
import'=>array(
...
'application.components.*',
....
),
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
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
)