the-moisrex / webpp

C++ web framework | web development can be done with C++ as well.
https://t.me/webpp
MIT License
132 stars 9 forks source link

Constrained Named Route Parameters #218

Open the-moisrex opened 1 year ago

the-moisrex commented 1 year ago
Route::get('/user/{name}', function ($name) {
    //
})->where('name', '[A-Za-z]+');

Route::get('/user/{id}', function ($id) {
    //
})->where('id', '[0-9]+');

Route::get('/user/{id}/{name}', function ($id, $name) {
    //
})->where(['id' => '[0-9]+', 'name' => '[a-z]+']);

Seems like a really cool thing to make, but they seem like a sh*tty design in PHP.

They also have global constraints which is nice as well.

the-moisrex commented 1 year ago

Ideas:


struct username_validator {
    user_type user;
    bool operator()(context& ctx) {
       // ...
       user = ...;
    }
};

struct app {
  dynamic_router router;
  username_validator username{};

  app() {
    router.options["username"} = username;

    router += router / "users" / var{"username"} / "profile" >> &app::profile;
    router += router / "users" / username / "profile" >> &app::profile;
  }

  response profile (context& ctx) {
     auto user = ctx.params["username"];
     auto user = ctx.params[username];
  }

  response profile (username_validator username) {
     // ...
  }
}