Skip to content

Slim 3 Framework

Terry L edited this page Aug 22, 2020 · 2 revisions

Slim framework is one of my favorites. Since Slim is a mirco framework, implementing Shieldon Firewall is easy as well. Without further ado, let's get started.

Firewall in Slim Framework

Installation

Use PHP Composer:

composer require shieldon/shieldon ^2

This will also install dependencies built for Shieldon:

Implementing

(1) Add the firewall middleware in your application.

Shieldon has a integration class ready for this middleware, just use it by the following step.

Example:

$app->add(new \Shieldon\Firewall\Integration\Slim3);

For example, if you are using Slim3 skeleton, the code in middleware.php will look like this:

Example:

<?php

use Slim\App;

return function (App $app) {
    $app->add(new \Shieldon\Firewall\Integration\Slim3);
    $app->add(new \Slim\Csrf\Guard);
};

(2) Create a route for control panel.

For example, if you are using Slim 4 framework, the code should look like this. Then you can access the URL https://yourwebsite.com/firewall/panel/ to login to control panel.

Example:

$app->map(['GET', 'POST'], '/firewall/panel[/{params:.*}]', function (Request $request, Response $response, array $args) {

    $firewall = new \Shieldon\Firewall\Firewall($request);

    // The directory in where Shieldon Firewall will place its files.
    // Must be the same as firewallMiddleware.
    $firewall->configure(__DIR__ . '/../cache/shieldon_firewall');

    // The base url for the control panel.
    $firewall->controlPanel('/firewall/panel/');

    $panel = new \Shieldon\Firewall\Panel();

    // Begin - Need to set up CSRF fields if you have enabled Slim-CSRF

    $csrf = new \Slim\Csrf\Guard();
    $nameKey = $csrf->getTokenNameKey();
    $valueKey = $csrf->getTokenValueKey();

    $csrfName = $request->getAttribute('csrf_name');
    $csrfVale = $request->getAttribute('csrf_value');

    $panel->csrf(
        [$nameKey => $csrfName],
        [$valueKey => $csrfVale]
    );

    // End - Slim-CSRF

    $panel->entry();
});

That's it.

Controll Panel

You can access the Firewall Panel by /firewall/panel/, to see the page, go to this URL in your browser.

https://yourwebsite.com/firewall/panel/

The default login is shieldon_user and password is shieldon_pass. After logging in the Firewall Panel, the first thing you need to do is to change the login and password.

Shieldon Firewall will start watching your website if it get enabled in Deamon setting section, make sure you have set up the settings correctly.