I want to mark a parameter as optional in a Restler patch method, and it's of type string, but I can't figure out how to do this. If I put $description = null
as the parameter then the router is failing with a message saying description
is a required parameter.
I can't just use an empty string either becuase this is a PATCH type method, so I have to be able to differentiate between them wanting to blank out the description by passing an empty string vs. a null value meaning they didn't pass in any update.
Keeping $description
to null
works fine. Just make sure all optional parameters appear after required parameters in your api method. This is a php restriction.
composer create-project restler/application=dev-basic rest
To create a basic restler project. Then edited rest/src/Home.php to add the patch method
<?php
class Home
{
public function index()
{
return [
'success' => [
'code' => 200,
'message' => 'Restler is up and running!',
],
];
}
public function patch($name, $description = null)
{
return compact('name', 'description');
}
}
Started the web server using php serve
on the command line. Launched the explorer with http://localhost/explorer and then tested the patch method with the following json
{
"name": "arul"
}
The api result is
{
"name": "arul",
"description": null
}
Which is the expected result