I have a library which is used by all controllers. But for a specific controller i dont want to load that library. Is there any way i can stop loading that library for that controller.
i am using this command but its failing:
$this->load->library('xyz',array('autoload' => FALSE));
Thanks
Autoloading is meant for site-global items.
A cleaner solution may be to extend the controller and load the library in that new controller's constructor. Then all of your controllers extend from that controller, except the one(s) you don't want to load that library - those can extend the original CI controller.
That should take you < 5 minutes to implement and you won't have to hack anything.
You can take a look at this link:
http://xplus3.net/2010/05/31/conditional-auto-loading-of-libraries-in-codeigniter/
Basically, you'd be overwriting the autoload.php library to check for a variable. If that variable is false, then explicitly add the specific library to the autoload array.
Related
I ran into a problem, while coding around in CI 3.0.3 and latest HMVC Extension. I'm autoloading some libraries, helpers and much more. The Session library too. Now i have a few controllers all working fine, except one.
In that controller i use this code:
modules::run($module->module . '/' . $module->controller . '/' . $module->method, unserialize($block->configuration));
While using this static function i got the error:
Unable to locate the specified class: Session.php
The log says that the Session class is initialized.
INFO - 2015-12-01 09:41:40 --> Session: Class initialized using 'database' driver.
Any ideas why i got this error?
EDIT: -----------------------------------------------------------------------
To prevent stupid answers as below ...
In CI we can’t call more than 1 controller per request. Therefore, to achieve HMVC, we have to simulate controllers. It can be done with libraries, or with this “Modular Extensions HMVC” contribution.
The differences between using a library and a “Modular HMVC” HMVC class is: 1. No need to get and use the CI instance within an HMVC class 2. HMVC classes are stored in a modules directory as opposed to the libraries directory.
EDIT 2: ---------------------------------------------------------------------
For further questions:
Yes, the Session Library is the problem
Yes, only in combination with this HMVC Plugin
Yes, only if you use the static method modules::run();
No, i need this method for simulating a controller in a controller
Simple.
If you want to load a controller in a controller use:
Modules::load();
or outsource the Modules::run(); into a view that is loaded by the main controller.
Simple.
I'm using php and codeigniter, It has autoload class.
My first question is,
If I use autoload class to load all model will it make my application slower? Or is there no effect?
My second question :
Which one is better and faster, loading all model you need using autoload class, Or load only some models you need in a class function?
1) Autoload class will obviously make your application slower. because it uses php4 require function to load files. there are some hacks to use php 5 autoloading feature. Hope, new owner of codeigniter will add add support for autoloading.
2) It is best to use load specific models rather than autoload. in previous point I stated the reason behind this. basically it is good practice to load only the needed model, helper, library and assets. it assures that you use minimum time and memory.
I use autoload. and its working like a charm with no significant impact on loading time.
Method
Add this peace of Code on top of any libraries/models that you use in you CI autoload.php
example for me my config/autoload.php looks like
$autoload['libraries'] = array('database','door','acl','form_validation','notify');
and in libraries/Door.php i added
<?php//libraries/Door.php
function multi_auto_require($class) {
#var_dump("requesting $class");
if(stripos($class, 'CI') === FALSE && stripos($class, 'PEAR') === FALSE) {
foreach (array('objects','core') as $folder){//array of folders to look inside
if (is_file(APPPATH."{$folder}/{$class}.php")){
include_once APPPATH."{$folder}/{$class}.php";
}
}
}
}
spl_autoload_register('multi_auto_require');
class Door {
i added this snippet just above class Door{ in this way this snippet will run on every time codeigniter loads door library.
Testing and benchmarking
now For benchmarking i tested this code in a page with 17 DB queries from 8 Autoloaded Objects from 2 different folder with array of 3 folders to look in.
Result
using above mentioned method Vs include_once 'classlocation.php' for all project classes,
the average of 10 page refreshs for both methods where around 0.6sec;
so you can see there is no significant difference between both methods.
yet although i didn't test it on pages that doesn't use all class's, yet im sure autoloading makes my CI life much better and i'm happy with it.
I have an enCrypt and deCrypt class which I want to use in my whole Zend Framework project without having to declare it on every need, but just once. Where should this be done? Thank you for any help...
Zend Framework is quite flexible in implementing things.
You could create an instance in a bootstrap file and save it to the registry; whenever you need to call the class just get it from registry?
You could have every controller extend Zend_Controller_Action and put the two functions into this class (only ideal if you're calling the classes from the controller).
Or, what I did, was make my functions static, register my own library (in your application config.ini file, enter the line: autoloaderNamespaces[] = "MyPrefix_", then create a folder in the library folder called MyPrefix) and drop my class in there. When I need it I call $encryptedString = MyPrefix_Crypt::encrypt($string); and $string = MyPrefix_Crypt::decrypt($encryptedString);
Hope this helps :)
I'm building a library for our CodeIgniter app, but it requires many classes (currently I'm at 12).
Is there a best practice for packaging these many clients into one library. So I can just make one call to load it. i.e:
$this->load->library('soaplibrary');
Thanks!
As Summer points out, they have handled this situation somewhat elegantly in CI 2.0 with the concept of Drivers.
With a Driver, you actually create a subdirectory within your 'libraries' directory that contains your 'super' class, and another directory for 'child' classes. Better visual representation of the structure...
This was taken from Here.
and once you have constructed your library, here is the documentation on how to use it.
In CI 2.0, there are drivers to handle this situation. Good luck!
In CodeIgniter 3.1.9 when you load a library file, all classes in this file are included into code.
Let's say in soaplibrary.php you have
class SoapLibrary {
public function someMethod(...
class Test {
public function anotherMethod(...
In your controller you can do:
$this->load->library('soaplibrary');
//now on you can do
$this->soaplibrary->someMethod();
//but also
$test = new Test();
$test->anotherMethod();
CodeIgniter attempts to call the constructor of class SoapLibrary, hence a class with that name must be in there.
I am learning how to use the Zend Framework. I come from a codeigniter background.
What I want to do is define a function somewhere that performs a very simple yet useful function. I am predominantly going to use the function within view scripts. I don;t really want to make a whole class for such a simple thing, so my question is, is there anywhere were can I put a file containg all of my general functions and how do I go about using it?
Thanks
John
What you are looking for are view helpers.
A view helper however is a function in a helper class. Therefore only one view helper can be put in a single class.
If you are using the project setup as used in the quick start tutorial or as generated by Zend_Tool, your view helpers should be put in the application/views/helpers directory.
Declaring a view helper is pretty simple, and is explained in great detail on this page of the zend framework documentation (i must say it's a bit hidden in the docs):
http://framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.custom
Some background information on view helpers as well as some standard included ones can be found on this page: http://framework.zend.com/manual/en/zend.view.helpers.html
Hope this helped you in the right direction.
If you realy whant to use a function you can make a library class with a static method , make a folder like this Application/Library/MyLib , then at bootstrap register MyLib namespace like this
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('MyLib'); , then inside MyLib folder you can make a filename MyClass , with a class name MyLib_MyClass , then inside you're view you can call MyLib_MyClass::staticMethod().
Tough i suggest you make a view helper for this . You don't realy use functions in ZF like you where used to in CI ( i was in you're exact situation a few months ago ) , ZF is all about OOP .