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 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/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); + } } /** diff --git a/src/Trucker/Url/UrlGenerator.php b/src/Trucker/Url/UrlGenerator.php index 308a6af..d29a11a 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); } @@ -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