Basic routing using REQUEST_URI

So for nginx it is not straightforward to setup PHP-FPM so that PATH_INFO is correctly populated. Lamb uses the following /index.php/some/other type routing, where /some/other should be the PATH_INFO. Instead I want to make setup for a variety of web-servers straightforward, so I've switched to the more robust REQUEST_URI. This simplifies nginx configuration and Caddy and the PHP built-in web-server are compatible.

REQUEST_URI contains everything after the domain name, including the query string, so that needs to be removed:

$request_uri = '/home';
if ( $_SERVER['REQUEST_URI'] !== '/' ) {
    $request_uri = strtok( $_SERVER['REQUEST_URI'], '?' );
}

We can see that for a request for the root of the site, REQUEST_URI returns / whereas PATH_INFO would be empty, so the code above takes that into account. We can then deduct a router action as follows:

$action = strtok( $request_uri, '/' );

Once the $action is known, it can be checked against an allowed list of actions:

switch ( $action ) {
    case 'edit':
        ...
        break;
    default:
        respond_404();
        break;

#php #lamb

March 21, 2023 at 12:41 pm

Related posts

  • July 16 at 10:38 am I've archived my php-activate project…
  • January 31 at 11:57 am Use an expression to select an array value…
  • January 2 at 9:59 am PHP-Activate 0.3.1…
  • December 15 at 9:47 am PHP-Activate 0.3.0…
  • October 5, 2023 at 8:59 am PHP-Activate 0.2.0…
  • March 21, 2023 at 2:28 pm I've released php-activate 0.1.2, a PHP pr…
  • March 21, 2023 at 12:41 pm Basic routing using REQUEST_URI…
  • September 27 at 9:37 am I switched back to the vintage theme as th…
  • September 4 at 8:40 am There is no barrier when there is no UI fr…
  • September 4 at 8:39 am By the time you're logged into your C…
  • September 4 at 8:38 am We need more instant publishing on the web…
  • June 12 at 4:11 pm Trying out a new 2024 theme I've star…
  • January 3 at 2:07 pm What would it take to make writing vanilla…
  • December 6 at 2:28 pm Lamb image test…
  • July 28, 2023 at 11:43 am Lamb 0.3.0…
  • March 24, 2023 at 9:46 am Lamb 0.2…
  • March 16, 2023 at 4:49 pm 404 Fallback comes to Lamb I've added a 40…
  • March 15, 2023 at 12:42 pm Alright got the webserver configuration fi…