mnlg/install-middleware

Install middleware for the Slim framework

Installs: 3

Dependents: 0

Stars: 3

Watchers: 2

Language: PHP

1.0.2 2014-02-19 16:22 UTC

README

This middleware adds a simple web installer to your Slim application.

How it works

This middleware will first look for a lock file, in case it doesn't exist it will show the installer template. Once the form is submited it runs your own callback and creates the lock file to avoid showing the install template again.

The default template will display a form with some basic database fields such as database host, name or password. You can set your own template for more control.

To set an error in the installer form use the Flash's middleware flashNow method. This will avoid the creation of the lock file and will display the install template again along with the flash messages.

In case no flash messages are set in the callback the script creates the lock file and redirects to the home page. You can overwrite the redirect url by passing the redirect option to the middleware.

How to install

Update your composer.json manifest to require the mnlg/install-middleware package (see below). Run composer install or composer update to update your local vendor folder.

{
    "require": {
        "mnlg/install-middleware": "*"
    }
}

How to use

Use as a regular middleware adding it to the Slim application. Just take into account the Flash middleware must be added after the install Middleware.

$app->add(new \Mnlg\Middleware\Install(array(), function() {
  // your code here
}));
$app->add(new \Slim\Middleware\Flash());

By default it will look for the install.lock file to check if the application has already been installed. It also displays a default template with a form including some common database fields. The lock file and the template page can be overwritten for more control.

$app->add(new \Mnlg\Middleware\Install(array(
  'lock' => 'config/app_installed.txt',
  'template' => 'install.php'
  ), function() use ($app) {
    var_dump($app->request->post());
  }
));
$app->add(new \Slim\Middleware\Flash());

It's also possible to display errors and avoid the script installation in case the user submited data is not valid. This can be done setting flash messages in your callback, it will force the install template to be displayed again along the messages.

$app->add(new \Mnlg\Middleware\Install(array(
  'lock' => 'config/app_installed.txt',
  'template' => 'install.php'
  ), function() use ($app) {
    $app->flashNow('error', 'Wrong username or password');
  }
));
$app->add(new \Slim\Middleware\Flash());

After the callback is run the Install middleware will redirect the user to the home page by default. To redirect to a custom url instead, use the 'redirect' option.

$app->add(new \Mnlg\Middleware\Install(array(
  'redirect' => 'http://example.com'
  ), function() use ($app) {
      // your code here
  }
));
$app->add(new \Slim\Middleware\Flash());

License

All code in this repository is released under the MIT public license.

http://opensource.org/licenses/MIT