John John - 4 months ago 35
PHP Question

How to route Lumen for single page app

I'm using Lumen + Vue js to build an app.
I have this code in routes.php

$app->get('{any}', function () {
return view('vue', []);
});
$app->get('/', function () {
return view('vue', []);
});


This works great for /login, /users, /anything . But when I add a subroute like /users/agents or /a/b, /a/b/c -> anything with more than one slash it gives me the 404 from lumen

Answer

You have the 404 error because {any} will not catch the parameters that contain slash. I order to make it do so, you need to add a pattern:

$app->get('{any:.+}', function ()  {
    return view('vue', []);
});