[eluser]Unknown[/eluser]
This is a front controller I implemented on one of my projects. It allows me to include universal libraries, configure libraries, protect pages and more.
The _isMemberOfPages method is extremely helpful for targeting functionality for specific pages. For example all of the create, edit and delete pages are protected in the front controller and you won't need to write code for it anywhere else. You can also see that I protect all of the admin pages with only a few lines of code.
When passing data to the view I always use the $this->data variable. In it I pre-set the URI information and by default I set it as an associative array. This is VERY VERY helpful because no longer do you have to call uri segments like.. $this->uri->segment(3). Instead I just say $uri['id'].
The first 3 segments default to controller, action and id. I find this to be the best setup.
so the uri "pages/index/all/order/desc" would return...
$uri['controller'] => 'pages'
$uri['action'] => 'index
$uri['id'] => 'all'
$uri['order'] => 'desc'
Note all params after the first 3 are associative (name/value).
Code:
<?php
class Front extends Controller {
/**
* This is the entry point to the website. All pages pass through here
* and it allows us to handle global functionality
*
* @param boolean $frontController
*/
public function __construct($frontController = true) {
// * protected the front controller from being accessed
// in child controllers call this construct like so: parent::__construct(false);
// unless you want it to be another front controller that extends this front controller
// like an admin front controller
if ($frontController === true ) { exit('This page is unavailable.'); }
// * set error reporting levels
error_reporting(E_ALL);
//error_reporting(0);
// * load base controller construct
parent::Controller();
//$this->output->enable_profiler(TRUE);
// * load global libraries
// note: you can use the _isMemberOfPages() method to exclude some pages
// don't load all libraries as it will slow things down.
// only load libraries that are used on every page or used on all but a few
$this->load->library('Widget');
$this->load->library('Validation');
$this->load->library('Session');
$this->load->library('Pagination');
// * set default error delimiters
$this->validation->set_error_delimiters('<span class="formHint">', '</span>');
// * set global variables
$this->data = array();
// set uri keys
$this->data['uri'] = $this->input->xss_clean($this->uri->uri_to_assoc(4));
$this->data['uri']['controller'] = $this->input->xss_clean($this->uri->rsegments[1]);
$this->data['uri']['action'] = $this->input->xss_clean($this->uri->rsegments[2]);
$this->data['uri']['id'] = @$this->input->xss_clean($this->uri->rsegments[3]);
// set default page title
$this->data['page_title'] = 'My Default Page Title';
// * authentication for create, edit and delete pages
if ($this->_isMemberOfPages(array('.*/entry', '.*/create', '.*/edit', '.*/delete', '.*/update'))) {
if (!$this->usersession->isRegistered()) {
redirect('auth/login');
return;
}
}
// * admin authentication
if ($this->_isMemberOfPages(array('admin'))) {
// authentication code for admin page
if (!$this->usersession->isAdmin()) {
redirect('auth/login');
return;
}
}
}
/**
* Useful method for checking if a page is a member of a list of pages
*
* @param array $pages
* @return boolean
*/
protected function _isMemberOfPages($pages) {
// path to page represented as an array. (substr removes ending "/")
//$pagePath = array(substr($this->uri->router->directory, 0, -1), $this->uri->router->class, $this->uri->router->method);
$pagePath = array($this->uri->rsegments[1], $this->uri->rsegments[2]);
// set current page (directory/controller/action)
$currentPage = implode('/', $pagePath);
// add the beginning "/" id it doesn't exist
if (substr($currentPage, 0, 1) != '/') { $currentPage = '/' . $currentPage; }
// check each page to see
foreach ($pages as $page) {
// escape forward slash
$page = str_replace('/', '\/', $page);
// compare page to current page
if (preg_match('/^\/' . $page . '/', $currentPage)) {
// return true as soon as a match is found
return true;
}
}
// no matches were found. This page is not a memeber
return false;
}
}
A child controller that extends the front controller
Code:
<?php
class Posts extends Front {
public function __construct() {
// note that you pass "false" to the parent front controller to let it know you're not a front controller -- security so that front controller isn't exposed.
parent::__construct(false);
}
}
If you have any recommendations I'm open to suggestion.