sFire PHP Framework

Controllers

The Controller can be summed up simply as a collector of information, which then passes it on to the Mapper to be organized for storage, and does not contain any logic other than that needed to collect the input and to push information to the ViewModel.

In this section we will handle:

  • Creating a basic controller
  • Retrieving variables from the route and url
  • Retrieving current route or other routes by a given identifier
  • Using services
  • Retrieving current module config
  • Using helpers
  • Creating template functions

Creating a basic controller

Below is an example of a basic controller class. Note that the controller extends the base controller class included with sFire. The base class provides a few convenience methods such as the config method, which may be used to retrieve the current module config.

namespace App\Controllers;

use sFire\MVC\Controller;
use sFire\MVC\ViewModel;

class ControllerCustomer extends Controller {

    public function actionIndex() {
        new ViewModel('customer.overview');
    }
}

Retrieving variables from the route and url

You can use variabels in the URL and variables that are assigned to the route in routes.php.

URL variables

When using variables in your URL's, you can pass them from the routes.php to the constroller which then can pass the variables to a ViewModel.

In the example below a route is defined for displaying the customer detail page. When the URL "/customer/details/52" is requested, the route will call the Controller which will retrieve the variable (in this case "52") as parameter.

routes.php
Router :: add('customer/details/{int}', 'customer.detail') -> controller('Customer') -> action('details');
Controller
public function actionDetails($customer_id) {

    $viewmodel = ViewModel('customer.details');
    $viewmodel -> assign('customer_id', $customer_id);
}
Template
The customer id is: {{ $customer_id }}.

Assigned route variables

You can assign variables which can be retrieved in the Controller.

routes.php
Router :: get('customer', 'customer.index') -> assign('foo', 'bar');
Controller
public function actionIndex() {

    $this -> route() -> getParam('foo'); //Outputs "bar"
    $this -> route() -> getParams(); //Outputs Array('foo' => 'bar')
}

Retrieving current route or other routes by a given identifier

You can retrieve the current route by calling the route method. This method also accepts String which will represent a identifier.

public function actionIndex() {

    //Retrieve current route
    $this -> route();

    //Retrieve route based on identifier
    $this -> route('customer.index');
}

This will return a sFire\Routing\Extend\Route instance which has all the options that has been set in the routes.php.

$route = $this -> route();

$route -> getModule();
$route -> getStrict();
$route -> getController();
$route -> getAction();
$route -> getViewable();
$route -> getDomain();
$route -> getPrefix();
$route -> getIdentifier();
$route -> getUrl();
$route -> getMethod();
$route -> getAssign();
$route -> getUses();
$route -> getWhere();
$route -> getMatch();
$route -> getParam('foo', 'default value');
$route -> getParams();

Using services

All services defined in the app.php can be retrieved using the service method. Pass the name of the service and retrieve the instance.

app.php
//Service providers
Application :: add('services', [

    'cache' => function() {

        $driver = new sFire\Cache\Driver\Filesystem();
        return new sFire\Cache\Cache($driver);
    }
]);
Controller
$cache = $this -> service('cache');
$cache -> set('foo', 'bar', 60); //Sets new data in the cache for 60 seconds

Retrieving current module config

To load the config of the current module, you can call the config method. This will return the config in Array format.

$config = $this -> config();
print_r($config);

Using helpers

Helpers extends your application with features that can be used in your controller, mappers and even in the view. You can call a helper by passing the name of the helper to the helper method. This will return the instance of a helper which makes it possible to call the helper function directly.

Note: Pass only the name of the helper without the prefix

Controller
$this -> helper('Currency') -> format(25);
Helper
namespace App\Helpers;

class HelperCurrency {

    public function format($number = 0, $currency = '$') {
        return $currency . number_format($number, 2, '.', ',');
    }
}

Creating template functions

You can create functions to use in the templates. This will allow you to extend the template functionality. The first parameter is the name of the function, while the second parameter is a callable function with or without parameters.

Controller:
$this -> template('format', function$number = 0, $currency = '$') {
    echo $currency . number_format($number, 2, '.', ',');
});
Template:
@format(25) //Outputs "$25,00"