Welcome Guest, Not a member yet? Register   Sign In
MY_Controller extended Controller class
#1

[eluser]Jamie Rumbelow[/eluser]
This is an extended Controller class to use in CodeIgniter applications that provides some clever model loading, view loading and layout support. It uses a Rails-like convention system to automatically load the view based on the controller name and the current method, and uses a custom layout system.

Usage
Drag the MY_Controller.php file into your application/libraries folder. CodeIgniter will load and initialise this class automatically for you. Then all you have to do is make sure all your controllers extend from MY_Controller and all the functionality will be baked into your controllers automatically!

Tutorial
I've written a tutorial that takes a reasonable look at the functionality provided by the entire class, as well as my MY_Model base CRUD model on my delightful blog. Check it out and feel free to leave a comment.

Download
You can get the source directly from GitHub, where I'll be updating the library. You can also fork it and customise it yourself! Alternatively, you can download it as a .zip or a .tar.

View Loading
Depending on the controller and action running, this class will try to guess the location of the view and load it for you. To pass data through to the view you simply set the $this->data array in a similar fashion to normal and it will be passed through to the view. For example, if we were in the Games controller and the index() action, the class would try to load the application/views/games/index.php view.

If you need to customise this view loading convention in any way, you can by setting the $this->view instance variable anywhere in your controller. Set this to any view and that view will be loaded, or set it to FALSE to not load a view at all. It's very flexible this way.

Layout Loading
This class will also try to load your view into a layout. By default, it will first look for a layout file (located in your application/views/layouts folder) with the same name as the current controller. If it can't find that it will then look for application/views/layouts/application.php - a global, application level layout file. For example, if you had an Admin controller it will first look for application/views/layouts/admin.php and then if that doesn't exist, it will try to load application/views/layouts/application.php layout instead.

You can also override this functionality by setting the $this->layout instance variable. Set it to a string to load that layout, or FALSE to load no layout. This will take precedence over the conventional loading strategy.

Model Loading
Set the $this->models instance variable in your controller to be an array of all the models you want to load and this class will loop through that array, loading each one sequentially. It looks for the model with a filename of $model_name_model.php and the class name of $model_name_model, but will load the model into the CI super object under $model_name. For instance, if my $this->models array had a 'game' model, it would look for the file and class Game_model but will load it to $this->game.

You can customise the suffix or prefix of the model name by setting the $this->model\_string variable. Use the % symbol, which will be replaced with the model you want to load.

Custom 404 Messages
If you try to call a method that doesn't exist, the class will first look for a \_404 method in your controller. This method will be passed the method that you're trying to call. Otherwise, it will display a default CodeIgniter 404 message.

Upcoming Features
* More customisable options
* Custom model names
* Automagic Library Loading

Version History
* 1.2.0 A bugfix, support for custom model names and custom 404 messages.
* 1.1.0 Partial support, asides and an improvement to documentation
* 1.0.5 Added support for multiple view rendering.
* 1.0.2 A few bugfixes
* 1.0.0 First release of library and basic functionality, with basic readme
#2

[eluser]ayukawaa[/eluser]
OK, INSTALLED. AND NOW, WHAT ELSE?

Hi, I've just downloaded and installed latest version from Github over a CI 1.7.2 (SVN).

The standard empty installation works fine (class welcome loaded ok) but after the installation of MY_Controller it fails.

What am I doing wrong?

welcome.php:

Code:
class Welcome extends MY_Controller {

    function Welcome()
    {
        parent::__construct();
    }
    
    function index()
    {
        //$this->load->view('welcome_message');
    }
}

reply from system:

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Welcome::$router

Filename: libraries/MY_Controller.php

Line Number: 147
A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: libraries/MY_Controller.php

Line Number: 147
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Welcome::$router

Filename: libraries/MY_Controller.php

Line Number: 147
A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: libraries/MY_Controller.php

Line Number: 147
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Welcome::$prerendered_data

Filename: libraries/MY_Controller.php

Line Number: 148
An Error Was Encountered

Unable to load the requested file: /.php

The only thing I can see is that the app is installed in h**p://localhost/jamierumbelow/index.php (^_^) but the requested file is wrong.

I'm NOT using an .htaccess right now.

Thanks,
#3

[eluser]Phil Sturgeon[/eluser]
You might want to modify line 113, there is a huge bug:

Code:
$data['title']          = ($this->title !== null) ? $this->title : "Jamie Rumbelow Rocks!";

Should read:

Code:
$data['title']          = ($this->title !== null) ? $this->title : "Jamie Rumbelow is fairly good... sometimes... perhaps!";

:-p
#4

[eluser]Jamie Rumbelow[/eluser]
[quote author="ayukawaa" date="1259861233"]OK, INSTALLED. AND NOW, WHAT ELSE?

Hi, I've just downloaded and installed latest version from Github over a CI 1.7.2 (SVN).

The standard empty installation works fine (class welcome loaded ok) but after the installation of MY_Controller it fails.

What am I doing wrong?

welcome.php:

Code:
class Welcome extends MY_Controller {

    function Welcome()
    {
        parent::__construct();
    }
    
    function index()
    {
        //$this->load->view('welcome_message');
    }
}

reply from system:

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Welcome::$router

Filename: libraries/MY_Controller.php

Line Number: 147
A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: libraries/MY_Controller.php

Line Number: 147
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Welcome::$router

Filename: libraries/MY_Controller.php

Line Number: 147
A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: libraries/MY_Controller.php

Line Number: 147
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Welcome::$prerendered_data

Filename: libraries/MY_Controller.php

Line Number: 148
An Error Was Encountered

Unable to load the requested file: /.php

The only thing I can see is that the app is installed in h**p://localhost/jamierumbelow/index.php (^_^) but the requested file is wrong.

I'm NOT using an .htaccess right now.

Thanks,[/quote]

Okay, so, as by convention, the class is trying to load an index.php file in your application/views/welcome/ directory. It also wants an application/views/layouts/application.php layout file. Do these exist?

To turn that autoloading off you need to set $this->view to FALSE and $this->layout to FALSE.
#5

[eluser]Jamie Rumbelow[/eluser]
Oh, and I've updated a fix for one of the errors that snuck through, so download the latest version on GitHub.
#6

[eluser]ayukawaa[/eluser]
Well, less errors but... still errors.

This is only a simple skeleton app I do to see how it works because I liked the autoloading of models and views.

This are my files:

application/controllers/welcome.php
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends MY_Controller {
    function Welcome()
    {
        parent::__construct();
    }
    function index()
    {
        //data to pass to application/views/welcome/index.php
        //this view is loaded in template in $yield var
        $this->data                        = array
                                        (
                                            'content'    => 'This is the content of the index.php file'
                                        );
        
        //array with template parts
        //each of them are loaded in template in $yield_????? var
        $this->asides                    = array
                                        (
                                            'header'    => 'includes/header',
                                            'menu'        => 'includes/menu',
                                            'footer'    => 'includes/footer'
                                        );
    }
}
/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */

application/views/layouts/application.php
Code:
<html>
<head>
<title>CodeIgniter MY_Controller</title>
</head>
<body>
<table>
    <tr>
        <td colspan="2">&lt;?php /**/ echo $yield_header; ?&gt;</td>
    </tr>
    <tr>
        <td>&lt;?php /**/ echo $yield_menu; ?&gt;</td>
        <td>&lt;?php /**/ echo $yield; ?&gt;</td>
    </tr>
    <tr>
        <td colspan="2">&lt;?php /**/ echo $yield_footer; ?&gt;</td>
    </tr>
&lt;/body&gt;
&lt;/html&gt;

applications/views/welcome/index.php
Code:
&lt;!-- START : application/views/welcome/index.php --&gt;
<h1>&lt;?php echo $content; ?&gt;</h1>
&lt;!-- END : application/views/welcome/index.php --&gt;

application/views/includes/header.php
Code:
&lt;!-- START : application/views/includes/header.php --&gt;
Welcome to the application!
&lt;!-- END : application/views/includes/header.php --&gt;

application/views/includes/menu.php
Code:
&lt;!-- START : application/views/includes/menu.php --&gt;
<ul>MENU
    <li>Option 1</li>
    <li>Option 2</li>
</ul>
&lt;!-- END : application/views/includes/menu.php --&gt;

application/views/includes/footer.php
Code:
&lt;!-- START : application/views/includes/footer.php --&gt;
(c)2009
&lt;!-- END : application/views/includes/footer.php --&gt;

with the changes you've made there are now less errors
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined property: Welcome::$router

Filename: libraries/MY_Controller.php

Line Number: 121
A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: libraries/MY_Controller.php

Line Number: 121
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Welcome::$router

Filename: libraries/MY_Controller.php

Line Number: 121
A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: libraries/MY_Controller.php

Line Number: 121
An Error Was Encountered

Unable to load the requested file: /.php

It seems my english is not so good as I thought it was because I don't seem to understand your code... arrrrrgghhhh

*_*

NOTE: Perhaps if you put a simple app for downloading with all working... (^_^)
#7

[eluser]Jamie Rumbelow[/eluser]
Another update, you fine people. A bug fix in partial looping, support for custom model prefixes or suffixes and support for custom 404 messages! Get it while it's hot, people!

Jamie
#8

[eluser]gazugafan[/eluser]
It looks like passing FALSE to $this->layout to disable loading a layout isn't working quite right. I think the _load_view function should be changed to this...

Code:
private function _load_view() {
        if ($this->view !== FALSE) {
            $view = ($this->view !== null) ? $this->view . '.php' : $this->router->class . '/' . $this->router->method . '.php';

            $data['yield'] =  $this->prerendered_data;
            $data['yield'] .= $this->load->view($view, $this->data, TRUE);

            if (!empty($this->asides)) {
                foreach ($this->asides as $name => $file) {
                    $data['yield_'.$name] = $this->load->view($file, $this->data, TRUE);
                }
            }

            $data = array_merge($this->data, $data);

            if (!isset($this->layout)) {
                if (file_exists(APPPATH . 'views/layouts/' . $this->router->class . '.php')) {
                    $this->load->view('layouts/' . $this->router->class . '.php', $data);
                } else {
                  $this->load->view('layouts/application.php', $data);
                }
            } elseif ($this->layout === FALSE) {
                echo($data['yield']);
            } else {
                $this->load->view('layouts/' . $this->layout . '.php', $data);
            }
        }
    }

Seems to do the trick for me! Love these base classes you came up with btw--they're great!
#9

[eluser]elambiguo[/eluser]
Hi Jamie...

I can't download any from your github account... not files in github anymore?.... In this case... how I can download the my_model and my_controller ?... or anyone that can send me a mirror.....

Thanks...

P.D. Sorry for my bad English......
#10

[eluser]ciKD[/eluser]
Unfortunately the links on github are still broken. Does anybody have a copy?

Thank you very much in advance!




Theme © iAndrew 2016 - Forum software by © MyBB