Welcome Guest, Not a member yet? Register   Sign In
Load controller from another controller
#1

Hello,
I was wondering if it's possible to load a controller through another controller. As i've searched the internet i found some articles that people mentioned that this is "not a working practice for MVC pattern".

The reason i want to achieve such a thing is for situations like:
e.g.: Let's say i want to get something from the db and place in header or footer (analytics scripts, footer copyrights text etc..) and in order to have cleaner "view" files i want to create some controllers (sth like "common/header", "common/footer" etc.) and be able to load them in the other controllers.

Any ideas / suggestions on that? 

Thanks

//Life motto
if (sad() == true) {
     sad().stop();
     develop();
}
Reply
#2

Did you think of using a library for that? Or create a MY_Controller.php in the application/core folder. Write a method to generate a common set of views or even different sets of views.
Let all your controllers (which are classes) extend the MY_Controller's class. Like that, you can use "common" methods in all your controllers.
Reply
#3

If you use the HMVC it allows controllers to load other controllers.

codeigniter-modular-extensions-hmvc
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

(06-20-2019, 12:46 PM)Wouter60 Wrote: Did you think of using a library for that? Or create a MY_Controller.php in the application/core folder. Write a method to generate a common set of views or even different sets of views.
Let all your controllers (which are classes) extend the MY_Controller's class. Like that, you can use "common" methods in all your controllers.

I've tried this method and yeas it works! But i see a lot of people suggest not to do such thing like extending controllers in core folder. That's why i thought to ask for an alternative (in case there is one)!

//Life motto
if (sad() == true) {
     sad().stop();
     develop();
}
Reply
#5

(06-20-2019, 12:47 PM)InsiteFX Wrote: If you use the HMVC it allows controllers to load other controllers.

codeigniter-modular-extensions-hmvc

I've seen it and i'll use it as it looks like, but i was wondering if there was any other alternatives! Thanks though for pointing the right direction!

//Life motto
if (sad() == true) {
     sad().stop();
     develop();
}
Reply
#6

Quote:But i see a lot of people suggest not to do such thing like extending controllers in core folder.

These people are probably trying to keep you away from modifying the system/core files. But the application/core folder isn't forbidden ground if you want to use a MY_Controller or MY_Model etc.
Reply
#7

WiredDesigns needs to be patched to work with current codeigniter installs. This is the skeleton I created for myself that I use:

https://github.com/mladoux/ci-skeleton

It has HVMC and a custom autoloader mod so you can name your controller extensions whatever you want ( not just MY_Controller ).
Reply
#8

(This post was last modified: 06-21-2019, 03:06 AM by hc-innov.)

You can do that (but I think library and real MY_controller are better):
I Have a directory 'application/font_controller' with a file called Font
Front.php

PHP Code:
<?php
//$object is the oject of my controller
class Front {

 
   public function __construct() {
 
       //Do what you want
 
   }
 
   
    public 
function footer($object) {
 
       $object->load->view('template/footer',$object->data);
 
   }
 
   
    public 
function header($object) {
 
       $object->data['header'] = 'My header';
 
       $object->load->view('template/header',$object->data);
 
   }
 
   
    public 
function page($object) {
        
//Example: you can load everything with $object
        
$object->load->helper('url_helper');
 
       $this->header($object);
 
       $this->footer($object);
 
   }

My controller

PHP Code:
include_once (__DIR__.'/../Front_controller/Front.php');

class 
Com extends CI_Controller {
 
   public $data;
 
   
    public 
function __construct() {
 
       parent::__construct();
 
   }
 
   
    public 
function index() {
 
       $this->data['footer'] = 'My footer';
 
       $front = new Front();
 
       $front->page($this);
 
   }

Reply
#9

The correct way to do this is to have a global Model being loaded by autoload, and there you create a function that will perform the insertion of your header and footer, you can also send a parameter and use as embedding views.

public function loadLayout($view = null, $data) {
//Load your data here

$this->load->view('header', $data);
$this->load->view($view, $data);
$this->load->view('footer', $data);
}
Reply
#10

Use a library or a model.
Codeigniter is simply one of the tools you need to learn to be a successful developer. Always add more tools to your coding arsenal!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB