2

I have a problem with the Lumen router (web.php): My project includes vue.js with the vue router, so I want to point all routes to the router, what is indeed working fine.

$router->get('{path:.*}', function () {
    return view('app');
});

My problem is: I also have a few api routes, handled by Lumen/controllers:

$router->group(['prefix' => 'api'], function ($router) {
    $router->group(['prefix' => 'authors'], function ($router) {
        $router->get('/', 'AuthorController@showAllAuthors');
        $router->get('/id/{id}', 'AuthorController@showAuthorById');
    });
});

Well, the route localhost/api/authors just works fine. But localhost/api/authors/1 returns the app..

I was thinking of putting in an exception to the vue route:

$router->get('{path:^(?!api).*$}'

..but this will lead to a NotFoundHttpException. Is something wrong with the regex? It should exclude all routes that start with /api.

zero2579
  • 218
  • 2
  • 8

1 Answers1

4

You're really close. Regex happens after the get/post statements in laravel's routes. Like so:

$router->get('/{catch?}', function () { 
    return view('app'); 
})->where('catch', '^(?!api).*$');

Here's the docs for reference: https://laravel.com/docs/5.8/routing#parameters-regular-expression-constraints

Edit: Lumen specific should be solved in a group prefix.

$router->get('/{route:.*}/', function () { 
    return view('app'); 
});
SavvyK
  • 87
  • 5
  • Thank you for the answer, but the problem is; Lumen has no `where`. You are just able to put the regex in the get-parameter.. – zero2579 Aug 06 '19 at 09:00
  • Sorry about that I was half asleep. Updated the answer for Lumen. – SavvyK Aug 06 '19 at 12:35
  • No problem! :) It's still not working, though I changed `$app` to `$router`again. I splitted my app in two with one part for the backend and one part just vue. Now it's working, but..yeah, not the answer to my question. :/ – zero2579 Aug 06 '19 at 18:31
  • @zero2579 I did some testing and made a change to my answer. This worked perfectly for me. Let me know what you get. – SavvyK Aug 08 '19 at 04:27
  • Seems to work if you don't use the vue router. I'm not sure what's the problem with that. But I guess for the main case, this is the right answer, thanks! – zero2579 Aug 13 '19 at 12:22