Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some new features added #77

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class TestController
{
return "Hello World";
}

/**
* Logs in a user with the given username and password POSTed. Though true
* REST doesn't believe in sessions, it is often desirable for an AJAX server.
Expand Down Expand Up @@ -71,6 +71,21 @@ class TestController
$user = User::saveUser($data); // saving the user to the database
return $user; // returning the updated or newly created user object
}

/**
* Returns a JSON array containing all arguments passed as URL. If you pass "test" argument it will be set to what
* you defined else it will be set to "default_value", this is same on "bool" argument where default value is "true"
*
* @url GET /wa
* @url GET /wa/$par1
*
* @param test "Sample argument passed by URL Query" "default_value"
* @param bool "Sample boolean argument" true
*/
public function testWithArg($par1 = null, $args = null)
{
return array($par1, $args);
}
}
```

Expand Down Expand Up @@ -102,6 +117,19 @@ but POSTing a new user object for our saveUser method could look like this:

So you’re able to allow POSTing JSON in addition to regular web style POSTs.

Last method is `testWithArg`, where you’ll notice there is a new kind of doc-comment tag in the docblock. `@param` maps a URL query to the method below it and is in the form:

`@param <NAME> <DEFINITION> <DEFAULT_VALUE>`

All parameters passed to the URL as URL query will be stored in `$args` parameter but the two predefined parameters named `test` and `bool` will be set to their defined default value if not set when calling. for example requesting GET on http://www.example.com/wa?test=Sample%20text&extra=1 will set `test` equal to "Sample%20text" and let `bool` equal to true. So `$args` will be:
```php
array(
[test] => Sample text,
[bool] => true,
[extra] => 1
)
```

I call these classes that handle the requests `Controllers`. And they can be completely self-contained with their URL mappings, database configs, etc. so that you could drop them into other RestServer services without any hassle.

### REST index.php
Expand All @@ -116,7 +144,7 @@ $server = new RestServer($mode);
// $server->refreshCache(); // uncomment momentarily to clear the cache if classes change in production mode

$server->addClass('TestController');
$server->addClass('ProductsController', '/products'); // adds this as a base to all the URLs in this class
$server->addClass('ProductsController', '/products',); // adds this as a base to all the URLs in this class

$server->handle();
```
Expand Down Expand Up @@ -196,4 +224,4 @@ composer install
```
cd <your project>
composer require 'jacwright/restserver:dev-master'
```
```
18 changes: 17 additions & 1 deletion example/TestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ public function test()
{
return "Hello World";
}

/**
* Returns a JSON array containing all arguments passed as URL. If you pass "test" argument it will be set to what
* you defined else it will be set to "default_value", this is same on "bool" argument where default value is "true"
*
* @url GET /wa
* @url GET /wa/$par1
*
* @param test "Sample argument passed by URL Query" "default_value"
* @param bool "Sample boolean argument" true
*/
public function testWithArg($par1 = null, $args = null)
{
return array($par1, $args);
}


/**
* Logs in a user with the given username and password POSTed. Though true
Expand Down Expand Up @@ -81,4 +97,4 @@ public function getCharts($id=null, $date=null, $interval = 30, $interval_months
public function throwError() {
throw new RestException(401, "Empty password not allowed");
}
}
}
Loading