Placeholder Types

Specify the type of data to be expected in the Placeholder

When creating routes with placeholders, we can specify the type of data in the placeholder that is to be expected. Currently this framework supports int, string and path types. Below we will see how to use these different types with placeholders.

To specify the type of placeholder, we write it in the format name:type in the route.

Router::add_route("GET", '/echo/{name:string}', [Controller::class, 'index']);

Integers

To specify integer placeholders in the route we use the type int .

Router::add_route('GET', '/echo/{id:int}', [Controller::class, 'index']);

In the above line, we created a route /echo/{id:int} which would basically capture the number that comes after /echo/ . It will only match integers and hence would cause error handler to be called if we use string or path in place of integer.

Strings

To specify the placeholders as strings, we just use the type string.

Router::add_route("GET", '/echo/{name:string}', [Controller::class, 'index']);

Now, placeholder will capture a string that comes after /echo/

Path

Now, lets say i want to capture an entire Path which comes at the placeholders place. path type is used for this and it captures a path.

Router::add_route('GET', '/echo/{way:path}', [Cont::class, 'index']);

So, now if a user tries to visit /echo/shoaib/wani the way placeholder will contain shoaib/wani.

Last updated