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

1.0 - Fix for using non-default identity property #25

Open
wants to merge 6 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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.<br /><br />_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". <br /><br />Otherwise a true PUT, POST, PATCH or DELETE request will be made

Expand Down
2 changes: 1 addition & 1 deletion src/Trucker/Finders/InstanceFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);

Expand Down
9 changes: 8 additions & 1 deletion src/Trucker/Requests/RestRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Trucker/Url/UrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -172,6 +172,7 @@ function ($item) {
$uri = implode("/", $uriResult) . "/$uri";
}

return "/$uri";
$prefix = Config::get('request.path_prefix', '/');
return "{$prefix}{$uri}";
}
}
12 changes: 12 additions & 0 deletions src/config/request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down