codeigniter how to call function which resides in another controller - php

One stupid question, I want to call from my admin_news controller function which resides in another controller Admin. FUnction name is is_logged_in();
admin.php
public function is_logged_in()
{
....
}
admin_news.php
public function __contruct()
{
parent::__construct();
//admin->is_logged_in();??
}
how can I do that?
Thanks

You will have to move that functionality somewhere else, Codeigniter's architecture doesn't allow multiple controller instances in one request. You have multiple options like using a common base class, libraries, helpers and so on.
I would recommend you to create your own MY_Controller base class (see Extending Core Classes) and put your method there, like this:
class MY_Controller extends CI_Controller {
protected function is_logged_in() {
// ...
}
}
Once you have it there you can call it like:
class AdminNews extends MY_Controller {
public function __construct() {
parent::__construct();
$this->is_logged_in();
}
}

Related

How to access model in constants.php in Codeigniter?

I want to access a function on application/config/constants.php, which is written in one of my CodeIgniter model. Is it possible to do it?
There is no reason to change a constant with a model function or any function for that matter. Constants are meant to be static and strict definitions. Like application paths, version numbers, .etc.
They are similar to a variable except that they can never be changed.
If you wish to have a variable that is a superglobal but needs to be dynamic in some respect you can create a /application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
public $someglobalvar;
public function __construct() {
parent::__construct();
$this->load->model('somemodel');
$this->someglobalvar = $this->somemodel->get_var();
}
}
and have your controllers extend it instead of CI_Controller
(application/controllers/Some_controller.php)
class Some_controller extends MY_Controller {
public function index() {
var_dump($this->someglobalvar);
}
}

Codeigniter, why do I need CI_Controller's construct?(code below)

Why do I need to use the parent::__construct(); constructor, what does it have I need?
//CONTROLLER
class users_ctrl extends CI_Controller {
function __construct() {
parent::__construct(); //Why do I need to include it?
$this->load->model('select_model');
}
public function index()
{
$data['user_list'] = $this->select_model->get_all_users();
$this->load->view('show_users', $data);
}
}
//MODEL
class select_model extends CI_Model{
function __construct() {
parent::__construct();
}
function get_all_users()
{
$query = $this->db->get('students');
return $query->result();
}
}
In your given examples, you are calling the load class that the base controller class loads. Without the parent's constructor, you would have to load an instance of it yourself.
As for your model, you would have to manually load your db object.
Remove it and you should get something like called to undefined propery $class::load
CodeIgniter aside, it really is a basic fundamental of object-oriented programming.
If you make a class that extends another, and you declare a new constructor in the child class, the parent constructor will never run - since you've overridden it, and since CodeIgniter base controller does (most likely) a lot of things behind the scenes, if you do not run parent constructor, your controller most likely won't be injected in CI's container.

Codeigniter. One __constructor to several controllers

I have some controllers, where __constructor is similar in every of it. How to avoid copy-paste of similar code and write it in a one place?
You can create a controller lets say ParentController and extends it with base controller then add __contructor in that.
Now in all of your controllers where you want this constructor just extend your controllers with the created controller ParentController.
ParentController.php:
class ParentController extends CI_Controller {
function __construct()
{
parent::__construct();
//your constructor code here..
}
}
Now the controllers in which you want the same constructor can be extended from ParentController :
ClassA.php
class ClassA extends ParentController {
function __construct()
{
parent::__construct();
}
//your first controller
}
ClassB.php
class ClassB extends ParentController{
function __construct()
{
parent::__construct();
}
//Your second controller
}
Hope this helps.
Best place for ParentController.php would be application/core/ folder.

Using an abstract Base Controller in CodeIgniter

I have a set of controllers that should only be accessible if you are an admin (as opposed to a regular user).
Thus, in the constructor for each of the controllers, I would do:
public function __construct() {
parent::__construct();
if (! is_admin()) {
show_404();
}
}
Instead of adding this code to the constructor of every Admin Controller, is there a better way to do this?
I was thinking I could create a Base controller called Admin_Controller that would look like this:
public class Admin_Controller extends CI_Controller {
public function __construct() {
//the above code goes here
}
}
And then all my other controllers can extend this class, instead of the CI_Controller class. The only problem with this is, I need to include this file at the top of my other controllers, or CodeIgniter cannot find Admin_Controller.
Is there a better way to do this?
place this in your application/core folder: MY_Controller.php (note the correct use of capitals)
class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
if (! is_admin())
{
show_404();
}
}
Then in all your normal controllers that you want users to be logged in
class Whatever extends MY_Controller
{
public function __construct()
{
parent::__construct();
}
}
See userguide here on extending core classes
I think that you could use Hooks
I've done the same thing, but I require a much more precise control so I have to check on every controller. Not all controllers are forbidden for single users.

Codeigniter cannot extend controller

I have created an admin controller that extends the blog controller (both are located in application/controller). However, when I tried to access the controller, it gives me an error that the blog controller was not found. If I put require_once(APPPATH.'controllers/blog.php'); inside the admin.php file it works. But I'm just wondering if there's another possible way to extends the blog controller without having to use require_once inside the admin.php
From CI user guide
If you are extending the Controller
core class, then be sure to extend
your new class in your application
controller's constructors.
class Welcome extends MY_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->view('welcome_message');
}
}
That means your Blog controller must extends CI_Controller
Example:
class MY_Blog extends CI_Controller {
function hello() {
$data = 'something';
}
}
class Admin extends MY_Blog {
function do_something() {}
}
Userguide

Categories