openswoole / ext-openswoole

Programmatic server for PHP with async IO, coroutines and fibers
https://openswoole.com
Apache License 2.0
808 stars 51 forks source link

Proposal for static rewrite feature #139

Closed hauptmedia closed 2 years ago

hauptmedia commented 2 years ago

Hi,

i want to discuss how we could implement a solution for the following feature:

If i deploy a single page application (like vue, react, etc) I want to redirect all non php routes routes to a single index.html page.

E.g. if the single page application generates a path /entity/$id/blah it should be mapped to /index.html internally to deliver the single page application. The single page application will then take care the path and route to the corresponding sub page within the single page application.

Currently the only way (known to me) to implement this is in Openswoole is:

A more elegant solution would be:

What do you think? Or am I missing something here?

Thank you for your time,

Julian

doubaokun commented 2 years ago

We think this should be implemented at the application layer.

You should be very easily to implement the following logics within the on('request') callback:

  1. If the URL is not prefixed with /api, then $response->sendfile(__DIR__ . '/index.html')
  2. Otherwise, go to the api application