Laravel 5.1 Middleware to restrict a site or specific routes using HTTP basic authentication
- Add
"divineomega/laravel-route-restrictor": "dev-master"to therequiresection of yourcomposer.jsonfile. - Run
composer update divineomega/laravel-route-restrictor(or justcomposer update). - Add
\DivineOmega\LaravelRouteRestrictor\Http\Middleware\BasicAuthentication::classto the$middlewarearray in yourapp/Http/Kernel.phpfile. - Add
RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization}]immediately belowRewriteEngine Onin yourpublic/.htaccessfile. This is required for web servers that are configured to use CGI as their PHP handler.
In order to restrict all routes in your Laravel application, add the following to your .env file.
ROUTE_RESTRICTOR_GLOBAL_USERNAME=cat ROUTE_RESTRICTOR_GLOBAL_PASSWORD=hat To restrict specific routes, you must be using named routes. Once you have a named route you wish to restrict, add the following to your .env file.
ROUTE_RESTRICTOR_ROUTE_MYNAMEDROUTE_USERNAME=fat ROUTE_RESTRICTOR_ROUTE_MYNAMEDROUTE_PASSWORD=cat Replace MYNAMEDROUTE with your route's name, capitalised. You can add as many route specific restrictions as you wish by repeating the above section as required.
Note: If you have both route specific restrictions and a global restriction, both will work, but route specific restrictions will take priority.