Abstract Class Misago\ActionController\Base

Actions Controllers are the core logic of your application. They process HTTP requests, manipulate your models, pass data to views and return HTTP responses.

A sample controller looks like this:

class PostsController extends Misago\ActionController\Base
{
  function show()
  {
    $this->post = new Post($this->params[':id']);
  }
  
  function create()
  {
    $this->post = new Post($this->params['account']);
    if ($this->post->save()) {
      $this->redirect_to(show_post_path($this->post));
    }
  }
} 

Inheritence

An ActionController is composed of the following classes:

And uses some objects:

$cache
ActiveSupport\Cache\Store
$flash
Misago\ActionController\Flash
$request
Misago\ActionController\AbstractRequest

Renders

Actions, by default, render a view from app/views using the name of the controller for the folder, the name of the action for the template, and the current format (defaults to html).

For instance calling /posts/index will process PostsController::index() and render app/views/posts/show.html.tpl.

Attributes

Any attribute you define in your action to your controller, will then be available to your view. For instance:

class PostsController extends Misago\ActionController\Base
{
  function show() {
    $this->post = new Post($this->params[':id']);
  }
} 

Then in your view you can access $this->post:

<h1><?= $this->post->title ?></h1>

Inheritence

Extends:
RequestForgeryProtection

Public instance attributes

Protected instance attributes

Methods

Public instance methods

__construct()

head($status_or_headers)

Returns a response that has no content (merely headers).

Examples:

head(array('status' => 201, 'location' => show_post_url($this->post));
head(403); 

render($options=null)

Renders a view or exports a resource.

Render a view:

Renders the view associated to an action, using the current request's format (html by default) or the one specified.

# renders the current action:
render();

# renders a specific action:
render('edit'); => edit.html.tpl

# renders a specific action using a specific format:
render(array('action' => 'edit', 'format' => 'xml')); => edit.xml.tpl 

Render a template

Instead of rendering an action, you may specify the template directly.

render('posts/show'); 

Layouts

By default a particular layout per controller will be rendered, for instance layouts/products.html.tpl for ProductsController. If no such layout exists it will fall back on layouts/default.html.tpl.

# use a particular layout:
render(array('action' => 'create', 'layout' => 'admin'));
  => renders create.html.tpl inside layouts/admin.html.tpl

# use a particular format:
render(array('action' => 'index', 'layout' => 'feeds', 'format' => 'rss'));
  => renders index.rss.tpl inside layouts/feeds.rss.tpl

# no layout at all:
render(array('layout' => false)); 

You may also override the layout you want to use for the whole controller. For instance in the following example layouts/website.html.tpl will be used for all view:

ProductsController extends Misago\ActionController\Base {
  protected $default_layout = 'website';
} 

To render the page content in the layout, as well as pass data from the template to the layout, use the yield() method:

# my_template.html.tpl
<? $this->yield('page_title') ?>

<section id="main"> ... </section>
<aside id="sidebar"> ... </aside>

# my_layout.html.tpl
<!DOCTYPE html>
<html>
<head>
  <title><?= $this->yield('page_title') ?></title>
</head>
<body>
  <div id="content"><?= $this->yield('content') ?></div>
</body>
</html>

Export a resource in a particular file format:

Being able to talk in XML or JSON to your server is great. Being able to easily export your data to these formats is even better.

render(array('xml' => $this->user));
# => exports $this->user as XML

render(array('json' => $this->products));
# => exports $this->products as JSON 

Note: no layout is rendered when using XML or JSON exports.

Headers

You may set the status and location headers:

render(array('json' => $this->products->errors, 'status' => '412'));
render(array('xml'  => $this->product, 'status' => '201',
  'location' => show_product_url($this->product->id))); 

Available options:

  • action - render the view associated to the current action.
  • format - use this particular format.
  • json - export resource as JSON.
  • layout - use a particular layout (false for no layout).
  • locals - pass some variables to be available in template's scope.
  • location - set HTTP location header.
  • status - set HTTP status header.
  • template - renders a template, from template root path (eg: 'errors/404').
  • text - render some text, with no processing --useful for pushing cached html.
  • xml - export resource as XML.

TODO: render(:partial => 'xx/yy') => app/views/xx/_yy.html.tpl (no layout) TODO: render(:file => '/xx/yy/zz.html.tpl') => /xx/yy/zz.html.tpl (no layout)

render_to_string($options=null)

Renders a view or exports a resource, returned as a string. See render() for documentation.

Protected instance methods

is_xml_http_request()

Checks wether the request is made from AJAX.

redirect_to($url, $status=302)

Redirects to another URL.

redirect_to('/path');
redirect_to(articles_path());
redirect_to(show_article_url($account->id));
redirect_to(array(':controller' => 'contact', ':action' => 'thanks')); 

By default a '302 Moved' header status is sent, but you may customize it:

redirect_to('/posts/45.xml', 301);  # 301 Found
redirect_to('/posts/45.xml', 201);  # 201 Created 

remote_ip()

Returns IP of current user (REMOTE_ADDR), trying to bypass proxies (HTTP_X_FORWARDED_FOR & HTTP_CLIENT_IP).