Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Danieloplata's avatar

Middleware question

Hi, I've got an app that I'm making while learning and want to ask how to handle this situation. Apologies if this isn't a middleware thing.

I have a route in my app:

Route::get('/start/{panelid}/{respondentid}', 'RedirectController@start')->name('redirectStart');

This route will basically create a record in my database and then redirect you to a relevant external site.

There are some criteria, where I may wish to prevent the user from being redirected. For example..

// Get users country via API

if(in_array($countrycode, $allowedCountries)) {
    // continue the redirect process
}

I intend to add more checks, such as preventing someone with the same ip/useragent being redirected to the same place twice, and logging it as a duplicate attempt if necessary.

When I've done this before in Vanilla/Procedural PHP, I've just stuck a bunch of if statements before the code I want to run, so I'm a little lost on how I should structure this. If middleware is the way to handle this, should each "check" be it's own middleware?

0 likes
2 replies
Snapey's avatar

Sounds to me like you can just handle this in a regular controller

Think about middleware as a wrapper around lots of routes.

If your redirect function has only one endpoint then middleware would not be appropriate.

Please or to participate in this conversation.