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

jonnyserra's avatar

AB Testing in Laravel

Hi i have a question regarding Laravel, i'm trying to create a Landing Page application that has optional AB Testing which can be triggered (or not) depending on Session variables and configuration values, meaning that i have a config file where config('ab_test.versions') returns the number of AB Testing versions available, and where a Session variable can be set or not depending on the existence of AB Testing on the particular Landing Page.

The goal of this AB Test is to simply redirect the user to the views of a different folder depending on the AB Test that the session specifies. So if i get 'a' from the session, all routes must look for their views inside of the folder 'a' meaning that there's a a.index view as well as a b.index view and so on depending on the amount of tests.

My question is how would you go about doing this in laravel? I'm thinking about using Middleware to do it, but i can't return a view from Middleware can i? All i want is to append the AB Test letter to the view() function call if the Session is set, otherwise just leave it as view('index') without touching it. Would i have to create a new controller for this?

0 likes
3 replies
yatsenkolesh's avatar

I think the answer is simple. You need to create the different views and resolve them directly in your controller. But if your changes are not huge, they might be resolved in a single view. Additionally, you can see some examples in ABRouter client and ABRouter Laravel example: https://github.com/abrouter/laravel-abtest -- client https://github.com/abrouter/laravel-example/blob/main/app/Http/Controllers/ExampleController.php - example in controller

Please or to participate in this conversation.