mainly about tips of web application tech in English from Japan.

Silex is the best web application framework for developers, who want to lean Symfony Components and PHP 5.3 (closure, name space..etc). Silex allow you to create new controllers with "mount" method which adds a controllers collection with "ControllerProvider".

Install

Then I tried to create new controllers for BASIC Authorization which is simple and reuseable. It is very easy to set up this controller provider, you have only to get code from gist in "src/Silex/Provider" directory.

Set up

This provider require UrlGeneratorServiceProvider and SessionServiceProvider, which are default providers Silex has. Then you have to register these providers before in index.php like below:

BasicAuth requires a name "home" of URL which is used after redirecting, of course you can change the name.

Then, add one line which calls the mount method in index.php. It is too simple:

$app->mount('/auth', new Silex\Provider\BasicAuthControllerProvider());

After adding, if you access to "http://example.com/", this application redirects you to "/auth/login" and require your name and password. If you want to logout, you have to access to '/auth/logout'. At Default, name is "demo" and password is "123456", so after you submit them, you can access to "home" page. If you want to remove Basic Authorization, you have only to comment out this mount line.

Customize

If you want to customize the setting about name, password and url to be redirect, just define like below: