riverside / php-express

:horse: PHP micro-framework inspired by Express.js
https://riverside.github.io/php-express/
MIT License
26 stars 10 forks source link

Implemented Arbitrary Parameters match from Path #4

Open cstayyab opened 3 years ago

cstayyab commented 3 years ago

Now we can provide arbitrary and multiple parameters name in URL Path.

Example

$app->get(
        'user/:username/pictures/:id',
        function ($req, $res) {
            $res->json($req);
        }
    );

This will output the resultant Request Object in JSON format where we can see the params array providing username and id

{
  "app": {
    "router": {}
  },
  "body": [],
  "cookies": [],
  "files": [],
  "hostname": "localhost",
  "ip": "::1",
  "method": "GET",
  "originalUrl": "/user/cstayyab/pictures/2",
  "params": {
    "username": "cstayyab",
    "id": "2"
  },
  "path": "user/cstayyab/pictures/2",
  "port": "80",
  "protocol": "HTTP/1.1",
  "query": [],
  "route": "",
  "scheme": "http",
  "secure": false,
  "session": null,
  "xhr": 0
}

Future Work

In future we can provide an option to pass custom regex expression like this:

$app->get(
        'user/:username([A-Za-z0-9_]+)/pictures/:id([0-9]+)',
        function ($req, $res) {
            $res->json($req);
        }
    );

Fixes #3

cstayyab commented 3 years ago

@riverside Can you please review and merge it so it can be published and used from composer?