I am doing my project using codeigniter
I want load a function when the page loads. i tried to give the function in __construct() but i need to call the function at every page.
So i want to call the function which is in library without calling from __construct()
Can anyone give me the solution for my problem
Take a look into the hooks. You can configure a function or a method of a class to be executed in various places in the life cycle of every request, depending in your needs.
If you need the controller instance for your functionality (the $this in most contexts) you probably need the post_controller_constructor one, and use the get_instance() to get a hold of the controller instance inside of the hook, to load in libraries or call models and such.
An other way could be that you extend the CI_Controller class with a MY_Controller class, place it under application/core/MY_Controller.php and move your code inside that classes __construct and use the MY_Controller as the base class of your regular controllers, as described the Creating Core System Classes page.
Add a library in autoload, then in that library file use __construct to call your function.
Related
I have looked online for the meaning of parent::init(); . All I was able to find was that init() is to initialize some settings which want to be present every time the application runs.
Can anyone please explain the meaning of parent::init() in exact sense, like significance of both the words?
Thanks in advance.( I am sorry if its too simple! )
When we use parent::init(), we are just calling the parent method (in this case init()) inside a method of the current class.
About parent::
For example, let's say we have a class called MyClass. This class have a awesome method that runs alot of things:
class MyClass
{
public function runStuffs()
{
// trigger events, configure external stuff, adding default values to properties.
}
}
Now, after some time, we decided to create a new Class that extends from the first one. And we called MySecondClass:
class MySecondClass extends MyClass
{
}
It already have the method runStuffs(), but, for this second class, we need to do more things in this method, but maintaining what it have.
Sure we could rewrite the whole method and just copy and paste what we have in MyClass and add the new content. But this isn't elegant or even a good practice. what if later on We change the method in MyClass, you probably would like that MysecondClass have that changes too.
So, to solve that problem, we can call the parent method before write your new content:
class MySecondClass extends MyClass
{
public function runStuffs()
{
parent::runStuffs();
// do more things!
}
}
Now MySecondClass->runStuffs() will always do what its parent do and, after that, more stuff.
About the init() method.
init() is a method used in almost all classes from Yii2 framework (since most of then extends from yii\base\Object at some point) and works just like the __constructor() method (native from PHP). But there is some differences, you can read more here.
Actually the init() method is called inside the __constructor(), and the framework encorage us to use init() instead of __construct() whenever is possible.
Now if both are pretty much the same thing, why do they create this method? There is an answer for that here. (take a look at qiang's answer, from the dev team):
One of the reasons for init() is about life cycles of an object (or a component to be exact).
With an init() method, it is possible to configure an object after it is instantiated while before fully initialized. For example, an application component could be configured using app config. If you override its init() method, you will be sure that the configuration is applied and you can safely to check if everything is ready. Similar thing happens to a widget and other configurable components.
Even if init() is called within constructor rather than by another object, it has meaning. For example, in CApplication, there are preInit() and init(). They set up the life cycles of an application and may be overridden so that the customization only occurs at expected life cycles.
Conclusion
So, when you use a init() method and calls parent::init() you are just saying you want to add more things to that method without removing what it already was doing.
The parent::init(); Method is useful to execute a code before every controller and action,
With an init() method, it is possible to configure an object after it is instantiated while before fully initialized.
For example, an application component could be configured using app config.
If you override its init() method, you will be sure that the configuration is applied and you can safely to check if everything is ready.
Similar thing happens to a widget and other configurable components.
In Yii, init() method means that an object is already fully configured and some additional initialization work should be done in this method.
For More Information check this link :
https://stackoverflow.com/questions/27180059/execute-my-code-before-any-action-of-any-controller
Execute my code before any action of any controller
might be helpful to you.
I am using codeigniter framework. let say i had controller class named myclass, this class have a function that i want to access from another controller class.
I got troubled when access the function from another controller class, i cant create an instant of this myclass class, it say class 'myclass' not found. i dont want to use include 'myclass.php'; because class myclass have __constructor method, i afraid the content inside __constructor method will conflict with another __constructor.
what the best solution for my case?
Thanks
CodeIgniter framework has a way to automatically loading your custom classes. Look here: http://www.codeigniter.com/userguide2/general/autoloader.html
Just make it possible for the framework to auto-load your class(es) and the problem is solved.
First disclaimer - I have inhered project, and there is a lot of legacy code which I can't delete (I can delete it, but then I will need to spend few months to write everything from scratch). I extended all the models with custom MY_Model (it is extending the core) which have save function. Also most of old models have save function.
My question is this:
How can I call the save function from MY_Model, and not from the class that is extending MY_Model? Is it possible?
I am unsure about the implementation but when you want to call a static function in current class not the class that extends it you can use :-
self::save();
See it elaborated in https://stackoverflow.com/a/1189663/2489860
I’m learning CI coming from a non MVC OOP in-house framework, so I’m still confused on a bunch of things, I don’t always understand what is normal OO programming VS. things that done the code igniter way. Anyway I’ll try to explain my problem:
I have one controller, called Admin which extends CI_Controller.
I made a class (let’s say custom class meaning is something I made not part od CI) I put that class in the library folder, that class extends CI_Controller as well (that extension allowed me to have load method and some other CI methods).
From my Admin controller, I’m loading my custom class (named “ComponiPannello” since I’m italian ), this way:
$parameters = array('baseUrl' => $this->baseUrl);
$this->load->library('ComponiPannello', $parameters);
everything works perfectly my “ComponiPannello” does hits job and returns something I need for my Admin view.
Now I wanted to load a model right after that class, because I need to pass more info to the Admin view, those info are stored in the DB, I have a specific model to pull those extra info, so I added a few lines and the code looks as follow:
$parameters = array('baseUrl' => $this->baseUrl);
$this->load->library('ComponiPannello', $parameters);
$setup = $this->componipannello->return_data();
$this->data = array_merge($this->data, $setup);
$this->load->model('categories_model');
$this->data['categories'] = $this->categories_model->get_categories();
were $this->data is what I’m passing to the Admin’s view.
Few more info:
as I said both Admin controller and my custom class ComponiPannello extends CI_Controller
in ComponiPannello I have this constructor:
function _construct($params) {
parent::_construct();
$this->load->library('session');
$this->baseUrl = $params['baseUrl'];
}
if I move the $this->load->model part before the $this->load->library I get no errors everything works and returns the correct data, but I would like to learn what I’m doing.
if I keep it as I have it the error I get is the following:
Fatal error: Call to a member function get_categories() on a non-object in ... on line ...
I hope I was able to explain my issue in a way that is clear enough for you to help me, thanks in advance for the time spent.
Luke
Your problem is probably coming about because you are loading another controller as a library, when in fact, it is not a library.
You can load libraries, helpers, etc from a library like this:
$CI =& get_instance();
From within your library, instead of $this->load->helper() to load a helper you would now use $CI->load->helper().
More info on this is available in the CI documentation: http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html
Whay u use database object in ur library?
Utilizing CodeIgniter Resources within Your Library.
To access CodeIgniter's native resources within your library use the get_instance() function. This function returns the CodeIgniter super object.
Normally from within your controller functions you will call any of the available CodeIgniter functions using the $this construct:
$this->load->helper('url');
$this->load->library('session');
$this->config->item('base_url');
etc.
$this, however, only works directly within your controllers, your models, or your views. If you would like to use CodeIgniter's classes from within your own custom classes you can do so as follows:
First, assign the CodeIgniter object to a variable:
$CI =& get_instance();
$CI->load->helper('url');
$CI->load->library('session');
$CI->config->item('base_url');
etc.
I'm very new to codeigniter ,
I wanted to know what is the meaning of a constructor in a controller . I saw the following code in a codeigniter tutorial -
class upload extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper(form);
}
// rest of the class...
My question is when is the constructor invoked - is it called every time the controller serves a request (e.g the controller class is instantiated for each request it receives?)
Well, that's a more general PHP question. Anyway, yes, the magic method __construct() is called (automatically) upon each instantiation of the class, as you can see in the manual: http://www.php.net/manual/en/language.oop5.decon.php
Usually, in CI is not necessary to call a constructor, unless you actually want one. In the example you posted, the code loads the helper on every instantiation of the class - which is the same as loading the helper in every method, just saves a lot of typing and ensures it's not forgotten. You can alternatively put the library/helper/model you want to have alywas loaded in the respective autoload array in config/autoload.php (check "autoloading" in CI's manual)
Once you define a constructor in your child Controller you're compelled to call the parent constructor (of the mail CI_Controller class), because there is where the main CI object is created and all the classes are loaded, and you need those in your child controller too; if fail to do so your child class will construct separately and won't inherit.
I hope I made myself clear, english is not my mothertongue :)
the constructor is magic Literally its called a magic method.
what makes the constructor cool is that it will do things for you BEFORE any of the methods. So if you have an admin class, and someone should be logged in in order to access it - you can check for login in the constructor and bounce them out if they are not authorized.
in the constructor you can load the models, libraries, helpers, etc that your class needs, and they will be available for any method in the class.
you can load in variables that are used by methods. this is really useful for models.
Don't use _construct() function in latest apache & codeigniter
Use helperlin in index() function
That's a general question. Constructor is a function that is automatically called when instantiated. this function helps us to intialize the things that we are going to need frequently in our code like when we have to load the models of helpers like form e.t.c.
$this->load->model('Model_name');
now when you write this line in your constructor you don't need to load this model again and again in your methods of that class.