sFire PHP Framework

Middleware

Middleware provide a convenient mechanism for filtering HTTP requests entering your application. For example, middleware that verifies the user of your application is authenticated. If the user is not authenticated, the middleware can redirect the user to the login screen. However, if the user is authenticated, the middleware will allow the request to proceed further into the application.

Additional middleware can be written to perform a variety of tasks besides authentication. A CORS middleware might be responsible for adding the proper headers to all responses leaving your application. A logging middleware might log all incoming requests to your application.

In this section we will handle:

  • Creating middleware
  • Registering Middleware
  • Retrieving variables from the route and url
  • Retrieving current route or other routes by a given identifier
  • Using services
  • Setting variables to be used in controllers and models

Creating middleware

Middleware are created in the "modules/{YOUR-MODULE-NAME}/middleware/" folder. By default, the name of a middleware begins with "Middleware" (you may change this configuration in the app.php.

In this example we are going to create middleware that handles all the default headers for our application

<?php
namespace App\Middleware;

use sFire\MVC\Middleware;
use sFire\HTTP\Response;


class MiddlewareHeaders extends Middleware {

    //Will run before application logic is executed
    public function before() {

        Response :: addHeader('X-Frame-Options', 'SAMEORIGIN');
        $this -> next(); //Execute next middleware or execute the controller if no other middleware is available
    }

    //Will run after application logic is executed. You may use this i.e. for logging.
    public function after() {
    }
}
?>

Note: Our application name is "App". Therefore we use the "App\Helpers" namespace.

Note: By calling the next method, sFire will go to the next middleware. If there is no next middleware available or the next method is not manually called, sFire will execute the request controller in "before" modus or will stop executing in "after" modus.

Save the middleware in "modules/App/middleware/" as "HelperCurrency.php" (mind the case-sensitive name).

Registering Middleware

You can register middleware in routes.php with the middleware method:

routes.php
Router :: get('customer', 'customer') -> controller('Customer') -> action('details') -> middleware('Authenticate');

You may also use multiple middleware:

routes.php
Router :: get('customer', 'customer') -> controller('Customer') -> action('details') -> middleware('Authenticate', 'Headers', 'Logging');

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') -> middleware('Authenticate');
Middleware
public function before($customer_id) {

    print_r($customer_id); //Outputs: "52"
}

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.

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);
    }
]);
Middleware
$cache = $this -> service('cache');
$cache -> set('foo', 'bar', 60); //Sets new data in the cache for 60 seconds

Setting variables to be used in controllers and models

You may assign variables to the current route so it is accessible for controllers, models, etc.

Middleware
$this -> route() -> assign('foo', 'bar');
Controller / Model
$this -> route() -> getParam('foo'); //Outputs "bar"