• Stars
    star
    113
  • Rank 310,115 (Top 7 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 12 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

⛔️DEPRECATED CodeIgniter base controller with view autoloading and layout support

DEPRECATED: codeigniter-base-controller

Deprecated since I no longer use CodeIgniter. If anybody would like to take over maintainence of this repo, please get in touch.

No Maintenance Intended Build Status

codeigniter-base-controller is an extended CI_Controller class to use in your CodeIgniter applications. Any controllers that inherit from MY_Controller get intelligent view autoloading, layout support and asides/partials. It's strongly driven by the ideals of convention over configuration, favouring simplicity and consistency over configuration and complexity.

Synopsis

class Users extends MY_Controller
{
    protected $models = array( 'user', 'group' );

    protected $helpers = array( 'cookie', 'file' );

    public function index()
    {
        $this->data['users'] = $this->user->get_all();
    }

    public function show($id)
    {
        if ($this->input->is_ajax_request())
        {
            $this->layout = FALSE;
        }

        $this->data['user'] = $this->user->get($id);
        $this->data['groups'] = $this->group->get_all();
    }
}

Usage

Drag the MY_Controller.php file into your application/core folder. CodeIgniter will load and initialise this class automatically for you. Extend your controller classes from MY_Controller and the functionality will be available automatically.

Views and Layouts

Views will be loaded automatically based on the current controller and action name. Any variables set in $this->data will be passed through to the view and the layout. By default, the class will look for the view in application/views/controller/action.php.

In order to prevent the view being automatically rendered, set $this->view to FALSE.

$this->view = FALSE;

Or, to load a different view than the automatically guessed view:

$this->view = 'some_path/some_view.php';

Views will be loaded into a layout. The class will look for an application/views/layouts/controller.php layout file; if it can't be found it will fall back to an application/views/layouts/application.php file, which is the defacto, application-wide layout.

In order to specify where in your layout you'd like to output the view, the rendered view will be stored in a $yield variable:

<h1>Header</h1>

<div id="page">
    <?= $yield ?>
</div>

<p>Footer</p>

If you wish to disable the layout entirely and only display the view - a technique especially useful for AJAX requests - you can set $this->layout to FALSE.

$this->layout = FALSE;

Like with $this->view, $this->layout can also be used to specify an unconventional layout file:

$this->layout = 'layouts/mobile.php';

Any variables set in $this->data will be passed through to both the view and the layout files, as well as any asides.

Asides

Asides are a great way to insert variable content into your layouts that might need to change on an action by action basis. This is especially helpful when you want to load sidebars or render separate forms of navigation.

Asides are arbitrary views loaded into variables. They can be set using the $this->asides variable:

protected $asides = array( 'sidebar' => 'users/_sidebar' );

They're then exposed as $yield_ variables in the layout:

<div id="sidebar">
    <?= $yield_sidebar ?>
</div>

Any variables in $this->data will be passed through to the sidebar.

Model Loading

You can specify a list of models to load with the $this->models variable:

protected $models = array( 'user', 'photograph', 'post' );

The model name is based on the $this->model_string variable. This allows you to name your models however you like. By default, the model string is:

protected $model_string = '%_model';

The percent symbol (%) will be replaced with the model name in the $this->models array. It will then be loaded into the CI object under the declared name.

protected $models = array( 'user' );

public function index()
{
    // $this->load->model('user_model', 'user');
    $this->user->get(1);
}

If, for example, you name your models model_user, you can specify the model string:

protected $models = array( 'user' );
protected $model_string = 'model_%';

public function index()
{
    // $this->load->model('model_user', 'user');
    $this->user->get(1);
}

Helper Loading

You can specify a lost of helpers to load with the $this->helpers variable:

protected $helpers = array( 'cookie', 'file', 'xml' );

Per-controller 404 override

Before CodeIgniter throws a standard 404, MY_Controller will look for a _404 method on the controller. This allows you to customise the output of the 404 page on a controller-by-controller basis.

Changelog

Version 1.3.0 - IN DEVELOPMENT

  • Vastly improved documentation
  • Added unit test suite
  • Added helper autoloading

Version 1.0.0 - 1.2.0

  • Initial Release

More Repositories

1

codeigniter-base-model

⛔️DEPRECATED CodeIgniter base CRUD model to remove repetition and increase productivity
PHP
1,049
star
2

pigeon

⛔️DEPRECATED Intelligent, elegant routing for CodeIgniter
PHP
107
star
3

codeigniter-schema

⛔️DEPRECATED Expressive table definitions
PHP
89
star
4

julian

⛔️DEPRECATED Brilliantly clever PHP calendar class
PHP
87
star
5

sassphp

PHP bindings to libsass - fast, native Sass parsing in PHP!
C
43
star
6

basic

A simple BASIC interpreter written in PHP
PHP
27
star
7

taggable

The best ExpressionEngine tagging engine
PHP
20
star
8

inferno

Quick, lightweight and flexible xUnit-flavoured PHP unit testing
PHP
20
star
9

sherlock

Asset pipelining so simple, it's elementary
PHP
19
star
10

sp_title_filler

Because sometimes you just don't nEEd a title
PHP
12
star
11

sp_table_select

Populate a dropdown with the contents of any table in your database
PHP
11
star
12

jquery.unique-element-id

A simple jQuery plugin to get a totally unique ID for any element
JavaScript
9
star
13

jquery.computed-style

A simple jQuery plugin to fetch the computed CSS styles for any element.
JavaScript
8
star
14

mojoblog

Simple, sexy blogging for MojoMotor
PHP
7
star
15

godfiles

Feel like the Lord almighty when you $ bash
6
star
16

pipes

Clear, easy to use and sophisticated PHP package management
PHP
6
star
17

presenters

Clean up your views with PHP presenters
PHP
6
star
18

postmaster

A tiny SMTP server that stores and exposes your emails in an HTTP API for testing
CoffeeScript
5
star
19

acts_as_subscribable

A loose wrapper around ActiveRecord that simplified subscriptions
Ruby
5
star
20

youoweme

Harass and get paid by friends and family members
Ruby
4
star
21

peepcode-downloader

Download your accessible PeepCode files with ease!
Ruby
4
star
22

blocks.markdown

A small Twig template filter for parsing text as Markdown within the Blocks CMS
PHP
4
star
23

porter

Lightweight, framework-agnostic PHP authorisation system
PHP
3
star
24

traversable_string

Traverse strings like a boss
Ruby
3
star
25

revisionhub

Collaborate and share revision notes
Ruby
3
star
26

hoipolloi

Treat your tweets like your inbox
Ruby
2
star
27

tailer-cowen

Searchable embeddings database for Marginal Revolution
Python
2
star
28

revisionhub-notes

All the revision notes you'll ever need
2
star
29

sp_select_pages

A dropdown for page selection in Low Variables
2
star
30

pipesphp.org

The source code to the Pipes project
PHP
2
star
31

housing-completions-vs-price-per-square-metre-by-local-authority

Jupyter Notebook
1
star
32

pipes-test-pipe

A quick example/test PHP library for Pipes
PHP
1
star
33

pherialize

A PHP serialize() specification and parser in Ruby
Ruby
1
star
34

tuxedo

Give your forms some jazz
PHP
1
star
35

laravel-ideal-hmvc-structure

PHP
1
star
36

gitstarted

Get started with contributing to open source
Vue
1
star
37

alchemy

An acceptance testing tool for Algolia indexes
Go
1
star
38

jamierumbelow.github.io

HTML
1
star
39

crudcontroller

⛔️DEPRECATED An extensible, simple base CRUD controller for Laravel 5 applications
PHP
1
star