Controllers

Controllers are the very center of a Bacon application. They function as entry points to the application, use models to retrieve data and presenter to deliver that data in the requested form.

Configuration

There is not much configuration needed. Currently the only configurable part is the default fallback controller, defined in \Config\Base:

namespace Config;

class Base
{
    public static $app = [
        'timezone'        => 'UTC',
        'base_uri'        => '',
        'fallback'        => 'News' # <- Default fallback controller
    ];
}

The default fallback controller is the one called if no other route was found.

Note: It is planned to split that up into a root controller and fallback (search/404) controller

Pretty URLs

Bacon relies on the Front Controller Pattern and as such all requests should be handled by Bacon's boot.php included in htdocs/index.php of your skeleton project. Many modern Web Application Servers like Nginx and Apache HTTPD have a very simple way of implementing this.

For Apache, add this to your virtual host definition:

# httpd.conf, in the VirtualHost:
FallbackResource /index.php

This means: send all requests that do not point to a specific file to index.php.

Routing

URLs map to controllers and their methods in a very specific way. There is no configuration for routing. We prefer the principle of convention over configuration. The base of this convention is the REST principle. A resource maps to a controller and its actions with the HTTP vocabulary. The only thing needed for introducing a new URL is dropping in a new controller with the same name and implement its actions.

Callable controller actions

Action URL HTTP Method
#index /resource GET
#new /resource/new GET
#show /resource/:id GET
#create /resource/ POST
#edit /resource/:id/edit GET
#update /resource/:id PUT (POST) 1
#destroy /resource/:id DELETE (POST) 1

:id is an arbitrary identifier for a specific resource you wish to access.

Namespaces

Bacon also sports namespaces


  1. Since browsers only allow GET and POST requests, PUT and DELETE are distinguished from a normal POST request by a parameter called _method. It may be embedded in a hidden form field or in the URL as GET parameter. 

Found a typo or want to help extend the documentation? Fork us on Github!