Overwrite CodeIgniter Common.php - codeigniter

There is a method in code igniter under system/core/Common.php called load_class().
I would like to overwrite this method. Usually to overwrite a code igniter class I create a file such as MY_Common.php however in this case Common.php is a collection of methods and there are no classes that encapsulates them.
So how exactly do I do this?

There's no officially supported way to do this by the built in extending mechanisms. Consider some other way to achieve your goal.
However the functions inside Common.php are all wrapped inside an if checking if the function is already exists or not so you can do the following:
Create your MY_Common.php put somewhere in your project (maybe application/core/ to mirror other similar extends)
Open your index.php file in the root of the project
insert include APPPATH.'core/MY_Common.php'; before the closing require_once BASEPATH.'core/CodeIgniter.php'; line
Now if you have you have a load_class function in your MY_Common.php it will shadow the original version.

The correct/official way to do that is overwriting the core common function into ie. common_helper.php application/helpers and setting up in config/autoload.php

Related

best place to put library in magento

I'm a magento developer.
I am trying to make library file which contains common function like login(id, pass), deleteDir, uploadFile() etc...
So each Controller can use this functions.
But the only file that does similar things is Helper/Data.php.
But Data.php looks like common data container file.
So which file can i put common functions?
You can create helper file (like Helper/Mylib.php) in your module and call the function like
Mage::helper('mymodule/mylib')->myFunction($pId);
You can make the block to put common functions.

Loading a model inside a module from API in codeigniter

I have one module Foo in my codeigniter HMVC. Also I have an api controller inside my application/controllers. I want to load a model inside application/module/foo/models from application/controllers/testapi
I have tested it by autoload as
$autoload['model'] = array('foo/Foo_model');
and called from testapi
$this->load->model('Foo_model');
But its not working
Try
$this->load->model('foo/foo_model');
from your controller.
then call model's function like this
$this->foo_model->function_name();
save model in application/modules/foo/models/Foo_model.php
CodeIgniter loads all possible models from application/models, I would suggest you modify the path of your models to something like application/models/modules with this you can load your model using
$this->load->model('modules/foo/Foo_model')
If you do want to retain your folder structure, I suggest you look at the constant paths of CodeIgniter and start modifying them to your will (although this is not really recommended).

Using helper function inside a Core Class in CodeIgniter

There is a helper which I would like to use inside a core class, CI_Router (MY_Router, to be more accurate). In this custom router, I made some modifications to the original code, in order to be able to insert hyphens into my urls.
I have defined the helper on the autoload.php file, as usual, but it seems that I canĀ“t invoque a helper function inside a class other than a view or controller.
Any ideas about how to handle this? My initial approach was to use a helper, so I can reuse it on any place I want.
TYVM.
Helpers are not instantiated until after the core, thus why it does not work.
You will either have to:
Duplicate the function in your MY_Router class, or,
Rethink why you might be using the same function in the Router that you use in a standard controller or view.
Option 1 is obviously easier, but might not be preferable depending on how bad your OCD is.
You could try getting the instance of the main CI object and setting it to a variable, then load the helper using that. Ex:
$ci =& get_instance();
$ci->load->helper('date');
I know that works in other areas, not 100% sure about any of the router classes.

codeigniter: accessing a callback from config/form_validation.php config file

Callback functions are not executing from my form_validation config file when called upon. Am I correct when assuming the reason for the non-execution is due to the location of the callback function? For the callback to execute, it will have to be placed inside the config file or have the $config array placed into the controller file? If that is the case, what avenue do you suggest I take or do something completely different to accomplish this?
The more I learn about CI the more I want to use its clean structure.
-Thank You,
Rich
Callback functions are placed within the same controllers or models from which they are called in form validation. Maybe in this case you need to put your callback function into a global controller liker MY_controller.php.

How to serve a MVC-view with image paths from directory?

I'm kind of new to the whole MVC concept, since I just recently started developing a web site using CakePHP framework. Therefore I turn to you asking for how to achieve the following in a best-practice-way.
I want to be able to place a number of pictures in a directory, which then is scanned for all filenames in it. These filenames should afterwards be passed to an arbitrary view, which then loops through all filenames making img tags of them.
It would be nice if this could be done in a general way, so that I could reuse the code for same task but with a different directory name.
I have already tried the following two approaches. Nevertheless, non of these felt like the best way to do it, for some reason.
Create a model with $useTable=false.
Create an ordinary class and import it as a vendor.
What is the best way to achieve what I describe above?
I'm thinking your original idea is the better one, use the Model to traverse/read/write the directory. Think of the File structure as your data source. You can pass the dirname to the model with $this->data and then let it use the File class to retrieve what you need. This would make it portable across Controllers (with loadModel())
Later on down the road if you move your image paths into a DB you only have to re-write the model to take that into account.
I've done the following before: create an e.g ImageManager class and pass it as an IImageManager to the constructor of your e.g ImageController instead of passing a repository. The ImageManager could then do all the work that you mentioned.
EDIT: I did that in .net mvc and don't have experience of CakePHP, but I guess there's a place where you can register the concrete implementation of IImageManager as ImageManager so the framework would know what to pass to the controller's constructor
I would create a helper for this purpose.
Edit: And for trawling through the filesystem use the Folder class from within your helper.

Resources