bramus / bramus/router

Before middleware inside mount() that works for / and /.*

Open
#55 11 comments 1 reaction 0 assignees View on GitHub
Dominant language
PHP
Stars
1.1k
Forks
236
PR merge metrics
No merged PRs in 30d

Description

Hey,

let's say I have the following route :
```php
$router->get('/', 'HomepageController@index');
```
I would like to use the before route middleware to restrict non-logged in users from accessing the website. So I did this :
```php
$router->before('GET', '/.*', 'AuthController@login');
```
The problem is that this doesn't work for the **'/'** route, but **'/blablabla'** for example.

Is there a subpattern or something else that allows my application to restrict non-logged in users using before route middleware in 1 single line rather than doing like this ?
```php
$router->before('GET', '/', 'AuthController@login');
$router->before('GET', '/.*', 'AuthController@login');
```

My idea would be to add a before route middleware to the **mount()** method so all routes inside this method will call first **AuthController@login**.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reading the router's before() and mount() entry points and trace how the '/' and '/.*' patterns are matched. Done means a before middleware configured for a mount applies to every route inside it, including the root route, with behavior checked for both examples.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.