From 3366434b6e38e0290492da19ea5e867ff7e07168 Mon Sep 17 00:00:00 2001 From: Victor Macko Date: Sun, 23 Aug 2015 12:27:47 +1000 Subject: [PATCH 1/4] Fixed non-default identify property used when updating records --- src/Trucker/Finders/InstanceFinder.php | 2 +- src/Trucker/Url/UrlGenerator.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Trucker/Finders/InstanceFinder.php b/src/Trucker/Finders/InstanceFinder.php index 810c1c7..e309224 100644 --- a/src/Trucker/Finders/InstanceFinder.php +++ b/src/Trucker/Finders/InstanceFinder.php @@ -63,7 +63,7 @@ public function fetch($model, $id, $getParams = array()) //init the request $request->createRequest( Config::get('request.base_uri'), - UrlGenerator::getInstanceUri($model, [':id' => $id]), + UrlGenerator::getInstanceUri($model, [':'.$model->getIdentityProperty() => $id]), 'GET' ); diff --git a/src/Trucker/Url/UrlGenerator.php b/src/Trucker/Url/UrlGenerator.php index 308a6af..2593b1d 100644 --- a/src/Trucker/Url/UrlGenerator.php +++ b/src/Trucker/Url/UrlGenerator.php @@ -122,7 +122,7 @@ public function getCollectionUri($model, $options = array()) */ public function getInstanceUri($model, $options = array()) { - $uri = implode("/", array($this->getURI($model), ':id')); + $uri = implode("/", array($this->getURI($model), ':'.$model->getIdentityProperty())); foreach ($options as $key => $value) { $uri = str_replace($key, $value, $uri); } From 85461ca52bd96723b37bbc659f3f4034ffb4af88 Mon Sep 17 00:00:00 2001 From: Victor Macko Date: Wed, 26 Aug 2015 00:48:29 +1000 Subject: [PATCH 2/4] Handling for PUT requests (content needs to be in body of request) --- src/Trucker/Requests/RestRequest.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Trucker/Requests/RestRequest.php b/src/Trucker/Requests/RestRequest.php index 0b87d20..e3780e1 100644 --- a/src/Trucker/Requests/RestRequest.php +++ b/src/Trucker/Requests/RestRequest.php @@ -193,15 +193,22 @@ public function setModelProperties(Model $model) $cantSet = $model->getReadOnlyFields(); //set the property attributes + $body = array(); foreach ($model->attributes() as $key => $value) { if (in_array($key, $model->getFileFields())) { $this->request->addPostFile($key, $value); } else { if (!in_array($key, $cantSet)) { - $this->request->setPostField($key, $value); + $body[$key] = $value; } } } + + if($this->request->getMethod() == 'POST') { + $this->request->addPostFields($body); + } else { + $this->request->setBody($body); + } } /** From 1e5a539493c1a65206b8142c2545555b31bf4af7 Mon Sep 17 00:00:00 2001 From: Victor Macko Date: Mon, 31 Aug 2015 21:34:25 +1000 Subject: [PATCH 3/4] Add path_prefix to config --- src/Trucker/Url/UrlGenerator.php | 3 ++- src/config/request.php | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Trucker/Url/UrlGenerator.php b/src/Trucker/Url/UrlGenerator.php index 2593b1d..d29a11a 100644 --- a/src/Trucker/Url/UrlGenerator.php +++ b/src/Trucker/Url/UrlGenerator.php @@ -172,6 +172,7 @@ function ($item) { $uri = implode("/", $uriResult) . "/$uri"; } - return "/$uri"; + $prefix = Config::get('request.path_prefix', '/'); + return "{$prefix}{$uri}"; } } diff --git a/src/config/request.php b/src/config/request.php index f16bf79..14e566c 100644 --- a/src/config/request.php +++ b/src/config/request.php @@ -12,6 +12,18 @@ 'base_uri' => 'http://example.com', + /* + |-------------------------------------------------------------------------- + | API endpoint path + |-------------------------------------------------------------------------- + | + | This is the optional path where the API endpoint is located under, + | for instance /admin/ would result in http://example.com/admin/ + | + | + */ + 'path_prefix' => '/', + /* |-------------------------------------------------------------------------- | HTTP Request Driver From 33895775beb8dcb0ec33713e4cdd089d8df9b7f9 Mon Sep 17 00:00:00 2001 From: Victor Macko Date: Mon, 31 Aug 2015 21:37:36 +1000 Subject: [PATCH 4/4] Update readme with path_prefix --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 8cf48ce..64709a0 100644 --- a/README.md +++ b/README.md @@ -185,6 +185,8 @@ logical_operator=AND Setting | Default | Description --- | --- | --- `base_uri` | `null` | This is the base URI that your API requests will be made to. It should be in a format such as http://my-endpoint.com +`path_prefix` | `null` | This lets you set a prefix for all API requests - eg. /api/ + defaults to '/' if nothing is set `driver` | `rest` | This parameter specifies the driver to use for making HTTP requests to the remote API. The driver handles how the requests are made, formatted etc.

_Supported Options:_ `rest` `http_method_param` | `null` | This is a parameter to send with the request that will contain a string disclosing the desired HTTP method ('put', 'post', 'patch', 'delete' etc.). If specified PUT, POST, PATCH and DELETE requests will all be made as a POST and the given parameter will be added with the http method as it's value. An example might be "_method".

Otherwise a true PUT, POST, PATCH or DELETE request will be made