CodeIgniter call same function for all controllers - codeigniter

I'm developing an application, where the same function I'm writing for some controllers. So I wanted one common function for all controllers. Please help me out I've stucked up with this. The work would be more appreciated.

You can develop a helper or library.
codeigniter version 2 : https://codeigniter.com/userguide2/general/creating_libraries.html
codeigniter version 3 : https://codeigniter.com/user_guide/general/creating_libraries.html?highlight=library

You can create MY_Controller class in application/core
Class MY_Controller Extends CI_Controller{
public function __construct(){
parent::__construct();
}
public function common(){
// code here
}
}
Now extend every class with MY_Controller and the function
will be available for each class
Class Test Extends MY_Controller{
public function __construct(){
parent::__construct();
}
}
When in the url you call
http://localhost/app/test/common
This will work.

create a model class and create that all function after that extend that model all model classes .you can use one function different controller.other wise you can use Cms helper.this is the best way call a common function in different controller.

Related

How to use different method of a controller in laravel application

In my laravel application i should use some create and update method of some controller in another controller
According to my search is not a good thing to call a method from controller in another
I cant see the why don't call a controller method in another controller
I'm doing this way :
class Controller extends BaseController
{
protected $variable;
public function __construct()
{
$this->variable = "Hello";
}
}
and
class ClientController extends Controller
{
public function __construct()
{
parent::__construct();
}
}
The __constructor is a magic method of class. It calls when you trying to create instance of class. So there is no way to use constructor without creating an instance or extendeding from another class. If you have a common code in different classes there a best way to use traits. thats give you an opportunity to include your trait and use methods ,making your code beutiful , flexible , readable following principes DRY,KISS.
you can create a base class with constructor and extend other controller of it
or you can put your code in to Http\Controllers\controller.php ('main controllers constructor')
also you can use trait

How to create multiple MX Controller (Base controllers) in codeigniter 2.2.2

im trying to create a base controller for some controllers.
Im extending MY_Controller from CI_Controller on application/core. I have a folder named "ajax" in application controllers and i pretend to create a parent class and extend from ajax/classes. Extending all from MY_Controller.
Some like this
class Ajax extends MY_Controller{
public function __construct(){
blablabla
}
}
class Ajax extends MY_AJAX{
public function __construct(){
blablabla
}
}
class user extends Ajax{
blablabla
}
Is it necessary to use includes, load_class, etc. or are there another "legal" way to do that?
Regards!
SOLVED YET:
How to create two parent controllers in Codeigniter?
This works for me:
class MY_Ajax extends MY_Controller{
public function __construct(){
CI_Controller::__construct();
}
}

Is it possible to access MY_Controller Public Function in CodeIgniter via browser

I am learning CodeIgniter. I have defined a MY_Controller Class residing inside Application/Core Folder.
Inside this, there is a public function.
My question is, Can I directly access this function or any other public function defined in My_Controller via the browser url.
Yes it it Possible.
If the method is public you can simply call it using any controller that extends My_controller and does not override the method ( i.e does not have a method with the same name as that defined in the My_Controller class)
like so :
This is how you can do it. Create a simple controller which will extend My_Controller :
someController.php
class someController extends MY_Controller{
}
now you can access it from url as such :
yourdomain.com/someController/yourMyControllerMethodName
You can call it from browser URL.
So that inside your controller you should define index() function. Because when it calls to your controller and if there is no any method calling with it, CodeIgniter automatically call index() function.
So
www.myproject.com/contact - this will call index() function of contact controller
www.myproject.com/contact/branch - this will call method inside contact controller

how do i extend a class in codeigniter?

this is my controller: common :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Common extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function test(){
echo 1;
}
}
and this the second controller (register):
class Register extends Common {
public function __construct() {
parent::__construct();
}
public function user_registration(){
$this->test();
}
}
when i access the user_registration function it shows me this error:
Fatal error: Class 'Common' not found in /home/attilana/domains/attila-naghi.com/public_html/application/controllers/register.php on line 3
How do i access the test() function from the class common , in the user_registration function from the class register?
In Codeigniter calling a controller from another controller even in an example like this is not possible because of the situation that it doesn't allow multiple controller instances in a request. Because of this situation you have three main options actually.
First, you can extend Codeigniter's base controller, CI_Controller, with MY_ prefix so that Codeigniter will recognize it as a common base class. The main idea here is to write the methods that will be common in most of your controllers.
If we take a look at your example and try to adapt it as mentioned above :
Initially, you need to extend the CI_Controller class:
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function test(){
echo 1;
}
}
Secondly, create your register controller as below:
class Register extends MY_Controller {
public function __construct() {
parent::__construct();
}
public function user_registration(){
$this->test();
}
}
Another possibility is you can create a helper and call that function globally or you can create a library and load it within your controller and call that.
For further information, you can read from here: http://ellislab.com/codeigniter/user-guide/general/core_classes.html or from old docs: https://github.com/EllisLab/CodeIgniter/wiki/MY-Controller

Can't access public properties from MY_controller when extending from MX_Controller in HMVC

I have implemented Codeigniter HMVC which works fine. Then I'm extending MX_Controller with MY_Controller to share functionality and properties between all my modules and to keep my code DRY.
But I noticed that while extending MY_Controller from MX_Controller I can no longer access the public properties from MY_Controller on any of the extended child classes.
Some sample code:
class MY_Controller extends MX_Controller {
public $variable;
function __contruct()
{
parent::__contruct();
$this->variable = 'Foo';
}
}
Then on any controller which I extend from MY_Controller:
class Foo extends MY_Controller {
function __construct()
{
parent::__construct();
}
function someFunction()
{
var_dump($this->variable);
}
}
When I trying to access the public property $variable in any child controller I get Null or empty string.
I have searched high and low for this with no luck, my only guess is an issue with HMVC MX_Controller. Any Ideas?
After hours of checking, it was a simple mistake on my behalf. I had misspelled the name parent::__contruct on MY_Controller, it should be parent:__construct.

Resources