Design - same function in two controllers - codeigniter

I have two controllers in my CI apps.
admin_controller.php, and
user_controller.php
in user_controller.php, there is a function show_message() like this:
class User_controller extends Controller{
function show_message(){
echo "your message";
}
}
and, in admin_controller.php, I want to write a function like show_message().
My question, what is the best way to use one function in two controllers? Should I rewrite function show_message in admin_controller.php like this ?
class Admin_controller extends Controller{
function show_message(){
echo "your message";
}
}
or should I create that function in file helper and call the function in controller admin and user?

Class MY_Controller extends CI_Controller
{
function show_message(){
echo "your message";
}
Class Admin extends MY_Controller {}
Class User extends MY_Controller {}
$this->admin->show_message(); // Result: "Your message"
$this->user->show_message(); // Result: "Your message"

Related

How to Call Subfolder Controller method in Routes in CodeIgniter 4

I have controllers in subfolders
--Controllers
----Admin
--------UserController
--------AccountController
----User
--------UserController
--------AccountController
I write routes for it
$routes->group('user', function ($routes){
$routes->get('dashboard', 'UserDashboard::index');
$routes->get('changePassword', 'User\AccountController::changePassword');
});
It give me 404 even though I have method and have something output as well
Controller code
<?php namespace App\Controllers;
use App\Controllers\BaseController;
class AccountController extends BaseController
{
public function index()
{
echo "Hello";
exit();
return view('user/account/changePassword');
}
public function changePassword()
{
echo "Change Password View";
exit();
return view('user/account/changePassword');
}
}
You need to change the namespace of your controllers.
User controllers
<?php namespace App\Controllers\User;
use App\Controllers\BaseController;
class AccountController extends BaseController
{
}
Admin controllers
<?php namespace App\Controllers\Admin;
use App\Controllers\BaseController;
class AccountController extends BaseController
{
}

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();
}
}

Codeigniter - Cannot call function of another class

I am about trying to learn about Codeigniter,
here in this try and error project, i want to call function from another class.
Here is the detailed code:
controller/admin/dashboard
class Admin extends CI_Controller{
public function dashboard() {
$this->load->library("overview");
$this->overview->method_overview();
}
}
Where, the overview is a the filename of Overview class that i want to call and the method_overview is a function inside overview class.
Here the overview.php
class Overview extends CI_Controller
{
public function method_overview() {
}
}
And this is the error that i got:
Unable to load the requested class: overview
Can somebody please give me solution or explanation?
Overview class inherit the property of CI_Controller you just inherit Overview class
So it would be
class Admin extends Overview{
public function dashboard() {
$this->load->library("overview");
$this->method_overview();
}
}
class Overview extends CI_Controller
{
public function method_overview() {
}
}
Since you're calling the overview class as a library, save that class as Overview.phpinside application/libraries.
In that file, something similar to:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Overview {
function __construct() {
$this->CI =& get_instance(); //gives access to the CI object
}
public function method_overview() {
//....
}
}

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