php-express icon indicating copy to clipboard operation
php-express copied to clipboard

Implemented Arbitrary Parameters match from Path

Open cstayyab opened this issue 5 years ago • 1 comments

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 avatar Sep 03 '20 11:09 cstayyab

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

cstayyab avatar Sep 09 '20 07:09 cstayyab