Welcome Guest, Not a member yet? Register   Sign In
include main app controller only once
#1

[eluser]gerben[/eluser]
Coming from the Cake framework, I find it convenient to create an app-controller for system-wide settings that extends the main CI controller. All my other controllers extend the app controller, rather than the CI controller.

Right now I include my app-controller in every controller that extends it, like so:

Code:
<?php
include("application/app_controller.php");

class SomeController extends AppController {

// etc.

?>

But isn't there an easier way to include this controller in every controller that extends it?
#2

[eluser]coolfactor[/eluser]
The Controller is a library, so it can be extended by creating a MY_Controller class in your application/libraries folder. It will be autoloaded for you, and you just need to extend your page controller from it. Put all your site-wide functionality into MY_Controller. Also, if a particular page controller does _not_ need any of the site-wide functionality, you can extend it from the Controller class in the usual way.
#3

[eluser]gerben[/eluser]
Thanks Coolfactor! I'll try that!

I also like the fact that it can be left out so easily, when the sitewide settings aren't needed. That's a really good side-effect.

By the way, I created a model that fetches the url of the main template directory from the db, and then i load the model in my app-controller, so I can use your View library like so:

Code:
<?php
$this->view->load($this->theme);
?>

great job you did on that one, just what I was looking for!
#4

[eluser]Rick Jolly[/eluser]
Extending the controller library with MY_Controller is a great option if you require only one custom parent controller.

If you require any number of parent controllers, you are running php 5, and you don't want to explicitly include()/require() parent controllers, then you can use the __autoload() function. I have put it in config.php since that is always loaded. If you follow a convention that any controller file has the word "controller" in it, and controller file names match the class names then this will work:
Code:
// I put this in config.php
function __autoload($class)
{
    if(stristr($class,'controller'))
    {
        require_once(APPPATH . '/controllers/' . $class . EXT);
    }
}
Code:
// The __autoload() function above will automatically find BaseController.php and load it.
// No include() or require() necessary.
class Home extends BaseController
{
    function Home()
    {
        parent::BaseController();    
    }
}

Personally, I don't mind the include() because it documents what parent controller file I'm using.
#5

[eluser]gerben[/eluser]
While the method I descibed in my post works fine, I can seem to get CF's approach to work. Keep getting a blank page.

I've created app_controller.php in app/libraries/app_controller.php:



Code:
<?php

class app_controller extends Controller {

    function app_controller()
    {
        parent::Controller();    

}

//etc.

And my controller is set up like this:
Code:
class Player extends app_controller {


    function Player()
    {
        parent::app_controller();
        
    }
// etc.

I can't see what I'm missing here. Could the problem be that I changed the default path to the application folder? I had no problems with that so far.
#6

[eluser]coolfactor[/eluser]
You have your base class called "app_controller". Did you change the "prefix" setting in config.php to "app_"?
#7

[eluser]gerben[/eluser]
I did that now, but still I get the white page.
#8

[eluser]esra[/eluser]
[quote author="gerben" date="1187995431"]While the method I descibed in my post works fine, I can seem to get CF's approach to work. Keep getting a blank page.

I've created app_controller.php in app/libraries/app_controller.php:



Code:
<?php

class app_controller extends Controller {

    function app_controller()
    {
        parent::Controller();    

}

//etc.

And my controller is set up like this:
Code:
class Player extends app_controller {


    function Player()
    {
        parent::app_controller();
        
    }
// etc.

I can't see what I'm missing here. Could the problem be that I changed the default path to the application folder? I had no problems with that so far.[/quote]

Controller is an alias for CI_Controller, so the code above should work fine, assuming that you are loading your view in your index method which is not shown.

You should not have to set your extended class prefix from MY_ to app_ when usng the Controller alias for the above to work.
#9

[eluser]gerben[/eluser]
I tried changing the subclass_prefix in config to 'app_', and also tried it using the default value ('MY_'). Both don't work. very strange, because I've looked over my code again and again, and can't see what could be wrong.

I'll post the complete code:

Code:
<?php

class app_controller extends Controller {


    function app_controller()
    {
        parent::Controller();    
        
        
    // load the app_model
    $this->load->model('app_model');
    
        // ok, here's where we'll get the main theme from the app model, and set it systemwide!
        $settings = $this->app_model->getSettings();
        $theme = $settings['theme'];
        $this->theme = $theme;  
    
        
    
    }
    

    
}
?>

And this is the normal controller:

Code:
<?php
class Player extends app_controller {


    function Player()
    {
        parent::app_controller();    
        
        
    }
    
    function index()
    {



$this->view->set('maincontent', 'this is the maincontent'); // assign a value to the maincontent variable
$this->view->set('sidevalue', 'This is a value for the sidemenu, inside the sidemenu view'); // assign a value to the maincontent variable
$this->view->part('sidemenu', 'sidemenu/index');
$this->view->load($this->theme);

        
    }
}
?>
#10

[eluser]gerben[/eluser]
I don't get it: I even did a clean install of CI, and only added a simple MY_Controller in application/libraries, and created a controller that extended the MY_Controller. Still I get a blank screen in my browser.

That's also strange: normally when something goes wrong in CI it will throw an error, but now the whole system just seems to break over what I do. I'm on php 5, so that can't be the problem either.

Would there be an alternative approach to getting site-wide settings to be available to each controller?




Theme © iAndrew 2016 - Forum software by © MyBB