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.
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');
}
}
You can use variabels in the URL and variables that are assigned to the route in routes.php.
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.
Router :: add('customer/details/{int}', 'customer.detail') -> controller('Customer') -> action('details');
public function actionDetails($customer_id) {
$viewmodel = ViewModel('customer.details');
$viewmodel -> assign('customer_id', $customer_id);
}
The customer id is: {{ $customer_id }}.
You can assign variables which can be retrieved in the Controller.
Router :: get('customer', 'customer.index') -> assign('foo', 'bar');
public function actionIndex() {
$this -> route() -> getParam('foo'); //Outputs "bar"
$this -> route() -> getParams(); //Outputs Array('foo' => 'bar')
}
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();
All services defined in the app.php can be retrieved using the service method. Pass the name of the service and retrieve the instance.
//Service providers
Application :: add('services', [
'cache' => function() {
$driver = new sFire\Cache\Driver\Filesystem();
return new sFire\Cache\Cache($driver);
}
]);
$cache = $this -> service('cache');
$cache -> set('foo', 'bar', 60); //Sets new data in the cache for 60 seconds
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);
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
$this -> helper('Currency') -> format(25);
namespace App\Helpers;
class HelperCurrency {
public function format($number = 0, $currency = '$') {
return $currency . number_format($number, 2, '.', ',');
}
}
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.
$this -> template('format', function$number = 0, $currency = '$') {
echo $currency . number_format($number, 2, '.', ',');
});
@format(25) //Outputs "$25,00"