Skip to content
Jeff Johns edited this page Feb 28, 2014 · 9 revisions
Controller Extends Path
Plain_Controller CI_Controller /application/core/Plain_Controller.php

This controller is extended by most of the other controllers in the system. It handles the logic to figure out if to start a session or not, handles redirects and figures what type of view to render.

Loading the controller

No need, it's done for you. Back off Buster Brown.

Properties

Property Visibility Default Value Description
$clean Public object If any POST or GET data is found, it is cleaned and placed in this object.
$csrf_token Public string The CSRF token to be used by the application for the user.
$current_user Public array Holds all the user information about the currently logged in user.
$data Public array Holds all the key/values pairs to pass to the views.
$db_clean Public object If any POST or GET data is found, it is cleaned and escaped for the database and placed in this object.
$flash_message Public array Holds an array of the type and message if one is set for the next view.
$footer Public string The default footer to user for the current view.
$header Public string The default header to use for the current view.
$html_clean Public object If any POST or GET data is found, it is cleaned and escaped for the database wtih HTML tags **NOT** stripped out and placed in this object.
$is_api Public boolean Set to true if current request is an API call, false if not.
$limit Public integer The default limit to use for extracting records from the database.
$logged_in Public boolean Set to true if user is logged in, false if not.
$original Public object If any POST or GET data is found, it will be placed in this object.
$user_admin Public boolean Set to true if user is an admin, false if not.
$user_id Public integer The current user's ID.
$user_token Public string The current user's security token.

Methods

__construct - Public

Called automatically which in turn calls the parent constructor. It also does the following:

  • Figures whether to start a session or not
  • Cleans any GET or POST variables
  • Gets the current user's information
  • Generates a CSRF token where applicable
  • Figures if CSRF token sent is a match to current where applicable
  • Gets any flash messages

addMark - Protected

Adds a mark to the systems and to the user's account.

Arguments

Variable Type Default Required Options Description
$data Array N/A Yes data['url'], data['title'], $data['label_id'] An array of data to create the mark for.
$data['url'] String N/A Yes N/A The URL to create the mark from.
$data['title'] String No Title No N/A The page title from the URL sent.
$data['label_id'] Integer N/A No N/A If you want to apply a label directly to this record for the user, supply it.

Example

$mark = $this->addMark(array(
    'url'      => 'http://somesite.com',
    'title'    => 'Site Title',
    'label_id' => 7
));

checkMark - Protected

Used to check if a mark already exists for the current user. If so it returns the mark, if not it returns false.

Arguments

Variable Type Default Required Options Description
$url string N/A Yes N/A The URL to check for in the current user's account.

Example

// Calling from child controller
$mark = parent::checkMark('http://google.com');

if ($mark == false) {
    // Add it
}
else {
    // Return it
}

clean - Protected

Used to check for GET or POST variables. If found, it saves the original copy, cleaned copy, database cleaned copy and an html cleaned copy under the class properties of $this->original, $this->clean, $this->db_clean and $this->html_clean.

Example

$this->clean();

figureView - Public

Used to figure out if the view should render a JSON only result, a redirect or a web view.

Arguments

Variable Type Default Required Description
$view string null No The view to show for the web view.
$redirect string null No The redirect path or url to redirect to when applicable.

Example

// For API or XMLHttpRequest calls only
$this->figureView();

// Could be a web view, API or XMLHttpRequest call
$this->figureView('marks/index');

// Redirect user
$this->figureView(null, '/marks/tag/TAG_SLUG');

generateCSRF - Protected

Used to verify the current CSRF token vs any REQUEST['csrf_token'] if found. Also will generate a CSRF token if the user does not have one in their session. Safe to call at all times. If not using a session it will automatically figure that out.

Example

$this->generateCSRF();

getFlashMessages - Protected

Checks to find any flash messages and the flash message type. If found it sets the data to $this->flash_message and unsets from session.

Example

$this->getFlashMessages();

getUserInfo - Protected

Used to set $this->user_token, $this->user_id, $this->user_admin and $this->logged_in on every request. Will read from session or API user token.

Example

$this->getUserInfo();

isAdmin - Protected

Returns true or false if the user is an admin.

Example

if ($this->isAdmin() === true) {
    // Cool, lucky you
}

isAJAX - Public

Returns true or false if the request is an XMLHttpRequest request.

Example

if ($this->isXMLHttpRequest() === true) {
    // Well aren't you fancy
}

isAPI - Public

Returns true or false if the request if an API request.

Example

if ($this->isAPI() === true) {
    // Smarty Pants
}

isChromeExtension - Public

Returns true or false if the request is coming from the Chrome extension

Example

if ($this->isChromeExtension() === true) {
    // hey hey hey
}

isCommandLine - Public

Returns true or false if the request is coming from the command line.

Example

if ($this->isCommandLine() === true) {
    // Super geek
}

isInternalAJAX - Public

Returns true or false if the request is an XMLHttpRequest request originating from the same domain.

Example

if ($this->isInternalXMLHttpRequest() === true) {
    // Do work!
}

isPJAX - Public

Returns true or false if the call is coming from the PJAX library. If so it renders the full HTML view minus the header and footer.

Example

if ($this->isPJAX() === true) {
    // don't forget to pushState!
}

isSameHost - Protected

Returns true or false if the host and referer are the same domain.

Example

if ($this->isSameHost() === true) {
    // Samsies
}

isWebView - Public

Returns true or false if the current request is for a web view.

Example

if ($this->isWebView() === true) {
    // Samsies
}

redirectIfInvalidCSRF - Protected

Redirects the user to the url specified if the CSRF is invalid.

Arguments

Variable Type Default Required Description
$url string / No The url or path to redirect the user to.

Example

$this->redirectIfInvalidCSRF();

redirectIfLoggedIn - Protected

Redirects the user to the url specified if they are logged in.

Arguments

Variable Type Default Required Description
$url string / No The url or path to redirect the user to.

Example

$this->redirectIfLoggedIn();

redirectIfLoggedOut - Protected

Redirects the user to the url specified if the user is logged out.

Arguments

Variable Type Default Required Description
$url string / No The url or path to redirect the user to.

Example

$this->redirectIfLoggedOut();

redirectIfNotAdmin - Protected

Redirects the user to the url specified if the user is not an admin.

Arguments

Variable Type Default Required Description
$url string / No The url or path to redirect the user to.

Example

$this->redirectIfNotAdmin();

redirectIfNotAPI - Protected

Redirects the user to the url specified if the request is not an API request.

Arguments

Variable Type Default Required Description
$url string / No The url or path to redirect the user to.

Example

$this->redirectIfNotAPI();

redirectIfNotCommandLine - Protected

Redirects the user to the url specified if the request is not coming from the command line.

Arguments

Variable Type Default Required Description
$url string / No The url or path to redirect the user to.

Example

$this->redirectIfNotCommandLine();

redirectIfNotInternal - Protected

Redirects the user to the url specified if the request is not a web view or an internal call.

Arguments

Variable Type Default Required Description
$url string / No The url or path to redirect the user to.

Example

$this->redirectIfNotInternal();

redirectIfWebView - Protected

Redirects the user to the url specified if the request is for a web view.

Arguments

Variable Type Default Required Description
$url string / No The url or path to redirect the user to.

Example

$this->redirectIfWebView();

renderJSON - Public

Reads the data from $this->data, turns it into a JSON string and prints the response.

Example

$this->data['success'] = true;
$this->renderJSON();

sessionAddUser - Protected

Sets all the user data to the user's current session. Also sets logged_in key to true for the user's session.

Arguments

Variable Type Default Required Description
$user object N/A Yes The user object to pull data from to set into the session.

Example

$this->load->model('users_model', 'user');
$user = $this->user->read("email = 'EMAIL'", 1);
if (isset($user->user_id)) {
    $this->sessionAddUser($user);
}

sessionClear - Protected

Removes all session data and cookies.

Example

$this->sessionClear();

sessionStart - Protected

Figures if the application should start a session based on the request. Command line and API requests will NOT start a session.

Example

$this->sessionStart();

setFlashMessage - Protected

Sets a flash message into memory.

Arguments

Variable Type Default Required Description
$message string N/A Yes The flash message/
$type string error No The flash message type. Can be `error` or `success`.

Example

$this->setFlashMessage('my message')

view - Protected

Used to render views. If your $data argument you can set any number of variables just like the CI_View to be rendered in templates. This view will also support header and footer and automatically append any flash messages for the user it finds to the data. You don't need to do that.

The header and footer are prepended and appended to your view if submitted. Also debug information is always shown under the footer in non-production mode. Get used to it.

Arguments

Variable Type Default Required Description
$view string N/A Yes The view from the view folder to render. (IE: 'index' or 'stats/index' if in a folder).
$data array array() No The data to pass to each view. It will be merged with that is already found in `$this->data`.

Example

$this->view('marks/index', array(
    'page_title'  =>  'Your Marks'
));

Clone this wiki locally