Controllers

Chapter 3

Introduction


A controller is where all the application's logic goes and where all the data abstraction is done.

Creating a New Controller


To create a new controller simply create a new file in your controllers directory. The name of the file should be the controller name followed by "_controller.php".

For example if I want to create an article controller I would name it article_controller.php.

Once you've create the file you'll need to define the controller's class and it's actions. For my article controller I want an action called "index" so I'll type the following:

<?php
# controllers/article_controller.php
class articleController extends appController {
  function index() {
    
  }
}
?>

Organizing Controllers in Directories

Madeam allows you to easily group controllers into directories like you would with a traditional php application. For example if you wanted to create an admin directory for all of your admin controllers you can do that and then edit your routes to point to your admin controllers.

<?php
# config/routes.php
$router->connect('admin/:action/:id', array('controller' => 'admin/[^/]+'));
?>

<?php
# controllers/admin/admin_article_controller.php
class adminArticleController extends appAdminController {
  function index() {
    
  }
}
?>

Action View


All actions have an output which is defined in the views directory. To add a new view for our action index in the article controller open the views directory and create a new directory called "article". This directory will be home to all of our article controller's action views.

Now create a file called index.html and inside type "hello, world". When you go to the page that executes the article's index action (article/index.html by default) the output will be "hello, world".

We use .html because it's the default format of all Madeam pages. We could also create another file called index.rss or index.xml and view those by instead going to article/index.rss or article/index.xml.

Sending Data To The View

Before data you've taken from the database in your controller's action can be accessed and formatted in the action's view it needs to be sent using the set() method.

For example to send a variable called "name" to the view with the value of "Joshua Davey" I would type:

<?php
$this->set('name', 'Joshua Davey');
?>

If I wanted to send a bunch of articles to the view I would type:
<?php
$this->set('articles', $this->Article->find_all());
?>

Using Models


To use a model that you've loaded into your models directory just type the name of the model in singalized form and the first letter capitalized.

For example to use the Article model you would type:

<?php
$articles = $this->Article->find_all();
?>

Using Components


To use a component that is loaded into your components directory you just type the name of the component in all lowercase letters.

For example to call the header component you would type:

<?php
$this->header->css('css/screen.css');
?>

Using Helpers


Madeam doesn't discourage the user of helpers inside of your controllers or models. Although sometimes it is poor practice other times it can be very useful.

For example to call the breadcrumb helper you would type:

<?php
breadcrumbHelp::add('Home', 'index');
?>

Callbacks


Callbacks are methods that are called before Madeam does certain things. To create global callbacks you can define them in your application controller (controllers/_app_controller.php).

before_action()

This is the most used callback. Everything in here executes before any actions in the controller are executed. This is a great place to set global settings and to check to make sure a user has access to an area of your site.

after_action()

This method executes after an action is called

before_render()

This method is identical to after_action but is executed after it. You can use either after_action or before_render or both. Just make sure you don't confuse anyone.

after_render()

This method is called after the view is rendered.

Built-in Methods


The following methods are built in and accessible in every controller.

render()

When this method is called the action's view is no longer rendered unless the value is "true". If the value is plain text then the plain text will be rendered and if the value is "false" then nothing will be rendered.

<?php
// renders "Hello, world!" inside of the layout
$this->render('Hello, world!');

// renders nothing
$this->render(false);
?>

set()

This is the most used method and the most important of all. This method is used to set data to the action's view. For example data retrieved from the database can't be seen on the action's view unless it's added using set().

<?php
// adds the variable "$name" to the action's view
$this->set('name', 'Joshua Davey');

// adds the variable "$article" to the action's view and set it's value to the value of $article
$this->set('article', $article);
?>

redirect()

This method does an HTTP location re-direct on the server side.

<?php
// redirects the user to the index page
$this->redirect('index');
?>

flash()

view()

This method sets the view for the action. By default you don't need to use this method because the view is named after the action but in special cases where you need to call a different view this method is very useful.

<?php
// calls the show action view from the article controller
$this->view('article/show');
?>

layout()

When called this method sets the layout to be used by the action it's used by.

<?php
// uses the special layout instead of the standard layout
$this->layout('special');
?>
Previous Chapter Next Chapter