Skip to content

Commit cbd0ea5

Browse files
committed
Merge pull request #146 from evan108108/issue-83
Issue 83 - overriding default output
2 parents cc1eb7e + 9e9af1a commit cbd0ea5

18 files changed

+1160
-310
lines changed

readme.markdown

Lines changed: 113 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,33 @@ class WorkController extends Controller
720720

721721
```
722722

723+
##Overriding & Adding Default Model attributes
724+
We can change the attributes outputted by our model(s) in any given request. We can do this using the "model.YOUR\_MODEL\_NAME\_HERE.override.attributes" event. Lets say we have a model named "Work" and we would like to add the non-AR property "url" to our JSON output every time that model is referenced.
725+
726+
```php
727+
$this->onRest('model.work.override.attributes', function($model) {
728+
return array_merge($model->attributes, ['url'=>"http://www.mysite.com/media/{$model->id}.jpg"]);
729+
});
730+
```
731+
732+
* You could also use this same event to remove or override default AR attributes
733+
734+
##Post Filtering Render Events
735+
It is possible to post filter the output of the "req.[get,put,post,delete].[resource, resources].render" events. This will allow you to completely change the output as you see fit.
736+
737+
```php
738+
$this->onRest('post.filter.req.get.resources.render', function($json) {
739+
$j = CJSON::decode($json);
740+
$j['data'] = array_map(function($work_data){
741+
$work = Work::model();
742+
$work->setAttributes($work_data);
743+
$work_data['url'] = $work->url;
744+
return $work_data;
745+
}, $j['data'])
746+
return $j;
747+
});
748+
```
749+
723750
##CORS Requests (Cross-Origin Resource Sharing)
724751

725752
Making cross origin requests from Javascript is now possible with RESTFullYii! RESTFullYii has several CORS specific events that help making CORS requests easy.
@@ -792,6 +819,7 @@ $.ajax({
792819

793820

794821

822+
795823
## Events
796824
List of all events and their default event handlers.
797825

@@ -819,15 +847,15 @@ List of all events and their default event handlers.
819847
| [req.param.is.pk](#req.param.is.pk) | [Yes](#pre.filter.req.param.is.pk) | [Yes](#post.filter.req.param.is.pk) | Called when attempting to validate a resources primary key. The default is an integer. Return true to confirm Primary Key; False to deny primary key. |
820848
| [req.is.subresource](#req.is.subresource) | [Yes](#pre.filter.req.is.subresource) | [Yes](#post.filter.req.is.subresource) | Called when trying to determain if the request is for a subresource |
821849
| [req.data.read](#req.data.read) | [Yes](#pre.filter.req.data.read) | [Yes](#post.filter.req.data.read) | Called when reading data on POST & PUT requests |
822-
| [req.get.resource.render](#req.get.resource.render) | [Yes](#pre.filter.req.get.resource.render) | No | Called when a GET request for a single resource is to be rendered |
823-
| [req.get.resources.render](#req.get.resources.render) | [Yes](#pre.filter.req.get.resources.render) | No | Called when a GET request for when a list resources is to be rendered |
824-
| [req.put.resource.render](#req.put.resource.render) | [Yes](#pre.filter.req.put.resource.render) | No | Called when a PUT request for a single resource is to be rendered |
825-
| [req.post.resource.render](#req.post.resource.render) | [Yes](#pre.filter.req.post.resource.render) | No | Called when a POST request is to be rendered |
826-
| [req.delete.resource.render](#req.delete.resource.render) | [Yes](#pre.filter.req.delete.resource.render) | No | Called when a DELETE request is to be rendered |
827-
| [req.get.subresource.render](#req.get.subresource.render) | [Yes](#pre.filter.req.get.subresource.render) | No | Called when a GET request for a single sub-resource is to be rendered |
828-
| [req.get.subresources.render](#req.get.subresources.render) | [Yes](#pre.filter.req.get.subresources.render) | No | Called when a GET request for a list of sub-resources is to be rendered |
829-
| [req.put.subresource.render](#req.put.subresource.render) | [Yes](#pre.filter.req.put.subresource.render) | No | Called when a PUT request for a single sub-resource is to be rendered |
830-
| [req.delete.subresource.render](#req.delete.subresource.render) | [Yes](#pre.filter.req.delete.subresource.render) | No | Called when a DELETE request on a sub-resource is to be rendered |
850+
| [req.get.resource.render](#req.get.resource.render) | [Yes](#pre.filter.req.get.resource.render) | [Yes](#post.filter.req.get.resource.render) | Called when a GET request for a single resource is to be rendered |
851+
| [req.get.resources.render](#req.get.resources.render) | [Yes](#pre.filter.req.get.resources.render) | [Yes](#post.filter.req.get.resource.render) | Called when a GET request for when a list resources is to be rendered |
852+
| [req.put.resource.render](#req.put.resource.render) | [Yes](#pre.filter.req.put.resource.render) | [Yes](#post.filter.req.put.resource.render) | Called when a PUT request for a single resource is to be rendered |
853+
| [req.post.resource.render](#req.post.resource.render) | [Yes](#pre.filter.req.post.resource.render) | [Yes](#post.filter.req.post.resource.render) | Called when a POST request is to be rendered |
854+
| [req.delete.resource.render](#req.delete.resource.render) | [Yes](#pre.filter.req.delete.resource.render) | [Yes](#post.filter.req.delete.resource.render) | Called when a DELETE request is to be rendered |
855+
| [req.get.subresource.render](#req.get.subresource.render) | [Yes](#pre.filter.req.get.subresource.render) | [Yes](#post.filter.req.get.subresource.render) | Called when a GET request for a single sub-resource is to be rendered |
856+
| [req.get.subresources.render](#req.get.subresources.render) | [Yes](#pre.filter.req.get.subresources.render) | [Yes](#post.filter.req.get.subresources.render) | Called when a GET request for a list of sub-resources is to be rendered |
857+
| [req.put.subresource.render](#req.put.subresource.render) | [Yes](#pre.filter.req.put.subresource.render) | [Yes](#post.filter.req.put.subresource.render) | Called when a PUT request for a single sub-resource is to be rendered |
858+
| [req.delete.subresource.render](#req.delete.subresource.render) | [Yes](#pre.filter.req.delete.subresource.render) | [Yes](#post.filter.req.delete.subresource.render) | Called when a DELETE request on a sub-resource is to be rendered |
831859
| [req.render.json](#req.render.json) | [Yes](#pre.filter.req.render.json) | No | NOT CALLED INTERNALLY. The event exists to allow users the ability to easily render arbitrary JSON.|
832860
| [req.exception](#req.exception) | [Yes](#pre.filter.req.exception) | No | Error handler called when an Exception is thrown |
833861
| Model Events |
@@ -1564,6 +1592,16 @@ $this->onRest('pre.filter.req.get.resource.render', function($data, $model_name,
15641592
});
15651593
```
15661594

1595+
####<a name="post.filter.req.get.resource.render"/>post.filter.req.get.resource.render</a>
1596+
```php
1597+
/*
1598+
* @param (JSON String) $json
1599+
*/
1600+
$this->onRest('post.filter.req.get.resource.render', function($json) {
1601+
return $json //Mixed[JSON Sting, ARRAY]
1602+
});
1603+
```
1604+
15671605

15681606

15691607

@@ -1603,6 +1641,15 @@ $this->onRest('pre.filter.req.get.resources.render', function($data, $model_name
16031641
});
16041642
```
16051643

1644+
####<a name="post.filter.req.get.resources.render"/>post.filter.req.get.resources.render</a>
1645+
```php
1646+
/*
1647+
* @param (JSON String) $json
1648+
*/
1649+
$this->onRest('post.filter.req.get.resources.render', function($json) {
1650+
return $json //Mixed[JSON Sting, ARRAY]
1651+
});
1652+
```
16061653

16071654

16081655

@@ -1639,6 +1686,15 @@ $this->onRest('pre.filter.req.req.put.resource.render', function($model, $relati
16391686
});
16401687
```
16411688

1689+
####<a name="post.filter.req.put.resource.render"/>post.filter.req.put.resource.render</a>
1690+
```php
1691+
/*
1692+
* @param (JSON String) $json
1693+
*/
1694+
$this->onRest('post.filter.req.put.resource.render', function($json) {
1695+
return $json //Mixed[JSON Sting, ARRAY]
1696+
});
1697+
```
16421698

16431699

16441700

@@ -1674,6 +1730,15 @@ $this->onRest('pre.filter.req.post.resource.render', function($model, $relations
16741730
});
16751731
```
16761732

1733+
####<a name="post.filter.req.post.resource.render"/>post.filter.req.post.resource.render</a>
1734+
```php
1735+
/*
1736+
* @param (JSON String) $json
1737+
*/
1738+
$this->onRest('post.filter.req.post.resource.render', function($json) {
1739+
return $json //Mixed[JSON Sting, ARRAY]
1740+
});
1741+
```
16771742

16781743

16791744

@@ -1709,6 +1774,15 @@ $this->onRest('pre.filter.req.delete.resource.render', function($model, $visible
17091774
});
17101775
```
17111776

1777+
####<a name="post.filter.req.get.resource.render"/>post.filter.req.get.resource.render</a>
1778+
```php
1779+
/*
1780+
* @param (JSON String) $json
1781+
*/
1782+
$this->onRest('post.filter.req.get.resource.render', function($json) {
1783+
return $json //Mixed[JSON Sting, ARRAY]
1784+
});
1785+
```
17121786

17131787

17141788

@@ -1740,9 +1814,9 @@ $this->onRest('req.get.subresource.render', function($model, $subresource_name,
17401814
});
17411815
```
17421816

1743-
####<a name="pre.filter.req.get.subresource.render">pre.filter.req.get.subresource.render</a>
1817+
####<a name="pre.filter.req.delete.subresource.render">pre.filter.req.delete.subresource.render</a>
17441818
```php
1745-
$this->onRest('pre.filter.req.get.subresource.render', function($model, $subresource_name, $count, $visibleProperties=[], $hiddenProperties=[]) {
1819+
$this->onRest('pre.filter.req.delete.subresource.render', function($model, $subresource_name, $count, $visibleProperties=[], $hiddenProperties=[]) {
17461820
return [$model, $subresource_name, $count, $visibleProperties, $hiddenProperties]; //Array [Object, String, Int, Array[String], Array[String]]
17471821
});
17481822
```
@@ -1787,6 +1861,15 @@ $this->onRest('pre.filter.req.get.subresources.render', function($models, $subre
17871861
```
17881862

17891863

1864+
####<a name="post.filter.req.get.subresources.render"/>post.filter.req.get.subresources.render</a>
1865+
```php
1866+
/*
1867+
* @param (JSON String) $json
1868+
*/
1869+
$this->onRest('post.filter.req.get.subresources.render', function($json) {
1870+
return $json //Mixed[JSON Sting, ARRAY]
1871+
});
1872+
```
17901873

17911874

17921875

@@ -1823,6 +1906,16 @@ $this->onRest('pre.filter.req.put.subresource.render', function($model, $subreso
18231906
});
18241907
```
18251908

1909+
####<a name="post.filter.req.put.subresources.render"/>post.filter.req.put.subresources.render</a>
1910+
```php
1911+
/*
1912+
* @param (JSON String) $json
1913+
*/
1914+
$this->onRest('post.filter.req.put.subresources.render', function($json) {
1915+
return $json //Mixed[JSON Sting, ARRAY]
1916+
});
1917+
```
1918+
18261919

18271920

18281921
###<a name="req.delete.subresource.render">req.delete.subresource.render</a>
@@ -1858,6 +1951,15 @@ $this->onRest('pre.filter.req.delete.subresource.render', function($model, $subr
18581951
});
18591952
```
18601953

1954+
####<a name="post.filter.req.delete.subresources.render"/>post.filter.req.delete.subresources.render</a>
1955+
```php
1956+
/*
1957+
* @param (JSON String) $json
1958+
*/
1959+
$this->onRest('post.filter.req.delete.subresources.render', function($json) {
1960+
return $json //Mixed[JSON Sting, ARRAY]
1961+
});
1962+
```
18611963

18621964

18631965

starship/RestfullYii/actions/EActionRestDELETE.php

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,35 @@ class EActionRestDELETE extends ERestBaseAction
2626
*/
2727
public function run($id=null, $param1=null, $param2=null)
2828
{
29-
$visibleProperties = $this->controller->emitRest(ERestEvent::MODEL_VISIBLE_PROPERTIES);
30-
$hiddenProperties = $this->controller->emitRest(ERestEvent::MODEL_HIDDEN_PROPERTIES);
31-
32-
switch ($this->getRequestActionType($id, $param1, $param2, 'delete')) {
33-
case 'RESOURCES':
34-
throw new CHttpException('405', 'Method Not Allowed');
35-
break;
36-
case 'CUSTOM':
37-
$this->controller->emitRest("req.delete.$id.render", [$param1, $param2]);
38-
break;
39-
case 'SUBRESOURCES':
40-
throw new CHttpException('405', 'Method Not Allowed');
41-
break;
42-
case 'SUBRESOURCE':
43-
$this->controller->emitRest(ERestEvent::REQ_DELETE_SUBRESOURCE_RENDER, [
44-
$this->handleSubresourceDelete($id, $param1, $param2),
45-
$param1,
46-
$param2,
47-
$visibleProperties,
48-
$hiddenProperties,
49-
]);
50-
break;
51-
case 'RESOURCE':
52-
$this->controller->emitRest(ERestEvent::REQ_DELETE_RESOURCE_RENDER, [$this->handleDelete($id), $visibleProperties, $hiddenProperties]);
53-
break;
54-
default:
55-
throw new CHttpException(404, "Resource Not Found");
56-
}
29+
$this->finalRender(
30+
function($visibleProperties, $hiddenProperties) use($id, $param1, $param2) {
31+
switch ($this->getRequestActionType($id, $param1, $param2, 'delete')) {
32+
case 'RESOURCES':
33+
throw new CHttpException('405', 'Method Not Allowed');
34+
break;
35+
case 'CUSTOM':
36+
return $this->controller->emitRest("req.delete.$id.render", [$param1, $param2]);
37+
break;
38+
case 'SUBRESOURCES':
39+
throw new CHttpException('405', 'Method Not Allowed');
40+
break;
41+
case 'SUBRESOURCE':
42+
return $this->controller->emitRest(ERestEvent::REQ_DELETE_SUBRESOURCE_RENDER, [
43+
$this->handleSubresourceDelete($id, $param1, $param2),
44+
$param1,
45+
$param2,
46+
$visibleProperties,
47+
$hiddenProperties,
48+
]);
49+
break;
50+
case 'RESOURCE':
51+
return $this->controller->emitRest(ERestEvent::REQ_DELETE_RESOURCE_RENDER, [$this->handleDelete($id), $visibleProperties, $hiddenProperties]);
52+
break;
53+
default:
54+
throw new CHttpException(404, "Resource Not Found");
55+
}
56+
}
57+
);
5758
}
5859

5960
/**

starship/RestfullYii/actions/EActionRestGET.php

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,36 @@ class EActionRestGET extends ERestBaseAction
2626
*/
2727
public function run($id=null, $param1=null, $param2=null)
2828
{
29-
$visibleProperties = $this->controller->emitRest(ERestEvent::MODEL_VISIBLE_PROPERTIES);
30-
$hiddenProperties = $this->controller->emitRest(ERestEvent::MODEL_HIDDEN_PROPERTIES);
31-
switch ($this->getRequestActionType($id, $param1, $param2, 'get')) {
32-
case 'RESOURCES':
33-
$this->controller->emitRest(ERestEvent::REQ_GET_RESOURCES_RENDER, [
34-
$this->getModel($id), $this->getModelName(), $this->getRelations(), $this->getModelCount($id), $visibleProperties, $hiddenProperties
35-
]);
36-
break;
37-
case 'CUSTOM':
38-
$this->controller->emitRest("req.get.$id.render", [$param1, $param2]);
39-
break;
40-
case 'SUBRESOURCES':
41-
$this->controller->emitRest(ERestEvent::REQ_GET_SUBRESOURCES_RENDER, [
42-
$this->getSubresources($id, $param1), $this->getSubresourceClassName($param1), $this->getSubresourceCount($id, $param1, $param2), $visibleProperties, $hiddenProperties
43-
]);
44-
break;
45-
case 'SUBRESOURCE':
46-
$this->controller->emitRest(ERestEvent::REQ_GET_SUBRESOURCE_RENDER, [
47-
$this->getSubresource($id, $param1, $param2), $this->getSubresourceClassName($param1), $this->getSubresourceCount($id, $param1, $param2), $visibleProperties, $hiddenProperties
48-
]);
49-
break;
50-
case 'RESOURCE':
51-
$this->controller->emitRest(ERestEvent::REQ_GET_RESOURCE_RENDER, [
52-
$this->getModel($id), $this->getModelName(), $this->getRelations(), $this->getModelCount($id), $visibleProperties, $hiddenProperties
53-
]);
54-
break;
55-
default:
56-
throw new CHttpException(404, "Resource Not Found");
57-
}
29+
$this->finalRender(
30+
function($visibleProperties, $hiddenProperties) use($id, $param1, $param2) {
31+
switch ($this->getRequestActionType($id, $param1, $param2, 'get')) {
32+
case 'RESOURCES':
33+
return $this->controller->emitRest(ERestEvent::REQ_GET_RESOURCES_RENDER, [
34+
$this->getModel($id), $this->getModelName(), $this->getRelations(), $this->getModelCount($id), $visibleProperties, $hiddenProperties
35+
]);
36+
break;
37+
case 'CUSTOM':
38+
return $this->controller->emitRest("req.get.$id.render", [$param1, $param2]);
39+
break;
40+
case 'SUBRESOURCES':
41+
return $this->controller->emitRest(ERestEvent::REQ_GET_SUBRESOURCES_RENDER, [
42+
$this->getSubresources($id, $param1), $this->getSubresourceClassName($param1), $this->getSubresourceCount($id, $param1, $param2), $visibleProperties, $hiddenProperties
43+
]);
44+
break;
45+
case 'SUBRESOURCE':
46+
return $this->controller->emitRest(ERestEvent::REQ_GET_SUBRESOURCE_RENDER, [
47+
$this->getSubresource($id, $param1, $param2), $this->getSubresourceClassName($param1), $this->getSubresourceCount($id, $param1, $param2), $visibleProperties, $hiddenProperties
48+
]);
49+
break;
50+
case 'RESOURCE':
51+
return $this->controller->emitRest(ERestEvent::REQ_GET_RESOURCE_RENDER, [
52+
$this->getModel($id), $this->getModelName(), $this->getRelations(), 1, $visibleProperties, $hiddenProperties
53+
]);
54+
break;
55+
default:
56+
throw new CHttpException(404, "Resource Not Found");
57+
}
58+
}
59+
);
5860
}
5961
}

starship/RestfullYii/actions/EActionRestPOST.php

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,29 @@ class EActionRestPOST extends ERestBaseAction
2626
*/
2727
public function run($id=null, $param1=null, $param2=null)
2828
{
29-
$visibleProperties = $this->controller->emitRest(ERestEvent::MODEL_VISIBLE_PROPERTIES);
30-
$hiddenProperties = $this->controller->emitRest(ERestEvent::MODEL_HIDDEN_PROPERTIES);
31-
32-
switch ($this->getRequestActionType($id, $param1, $param2, 'post')) {
33-
case 'RESOURCES':
34-
$this->controller->emitRest(ERestEvent::REQ_POST_RESOURCE_RENDER, [$this->handlePost(), $this->getRelations(), $visibleProperties, $hiddenProperties]);
35-
break;
36-
case 'CUSTOM':
37-
$this->controller->emitRest("req.post.$id.render", [$this->controller->emitRest(ERestEvent::REQ_DATA_READ), $param1, $param2]);
38-
break;
39-
case 'SUBRESOURCES':
40-
throw new CHttpException('405', 'Method Not Allowed');
41-
break;
42-
case 'SUBRESOURCE':
43-
throw new CHttpException('405', 'Method Not Allowed');
44-
break;
45-
case 'RESOURCE':
46-
throw new CHttpException('405', 'Method Not Allowed');
47-
break;
48-
default:
49-
throw new CHttpException(404, "Resource Not Found");
50-
}
29+
$this->finalRender(
30+
function($visibleProperties, $hiddenProperties) use($id, $param1, $param2) {
31+
switch ($this->getRequestActionType($id, $param1, $param2, 'post')) {
32+
case 'RESOURCES':
33+
return $this->controller->emitRest(ERestEvent::REQ_POST_RESOURCE_RENDER, [$this->handlePost(), $this->getRelations(), $visibleProperties, $hiddenProperties]);
34+
break;
35+
case 'CUSTOM':
36+
return $this->controller->emitRest("req.post.$id.render", [$this->controller->emitRest(ERestEvent::REQ_DATA_READ), $param1, $param2]);
37+
break;
38+
case 'SUBRESOURCES':
39+
throw new CHttpException('405', 'Method Not Allowed');
40+
break;
41+
case 'SUBRESOURCE':
42+
throw new CHttpException('405', 'Method Not Allowed');
43+
break;
44+
case 'RESOURCE':
45+
throw new CHttpException('405', 'Method Not Allowed');
46+
break;
47+
default:
48+
throw new CHttpException(404, "Resource Not Found");
49+
}
50+
}
51+
);
5152
}
5253

5354
/**

0 commit comments

Comments
 (0)