Skip to content

Commit 98a2249

Browse files
committed
Merge pull request #117 from symfony-cmf/make-rest-controller-generic
make rest controller more generic to support additional workflows
2 parents c12d644 + 545ad44 commit 98a2249

File tree

2 files changed

+24
-35
lines changed

2 files changed

+24
-35
lines changed

Controller/RestController.php

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,24 @@ protected function getModelBySubject(Request $request, $subject)
8686
}
8787

8888
/**
89-
* Handle document PUT (update)
89+
* Handle arbitrary methods with the RestHandler.
90+
*
91+
* Except for the PUT operation to update a document, operations are
92+
* registered as workflows.
9093
*
9194
* @param Request $request
92-
* @param string $subject URL of the subject, ie: cms/simple/news/news-name
95+
* @param string $subject URL of the subject, ie: /cms/simple/news/news-name
9396
*
9497
* @return Response
9598
*
9699
* @throws AccessDeniedException If the action is not allowed by the access
97100
* checker.
101+
*
102+
* @see RestService::run
103+
*
104+
* @since 1.2
98105
*/
99-
public function putDocumentAction(Request $request, $subject)
106+
public function updateDocumentAction(Request $request, $subject)
100107
{
101108
if (!$this->accessChecker->check($request)) {
102109
throw new AccessDeniedException();
@@ -105,7 +112,7 @@ public function putDocumentAction(Request $request, $subject)
105112
$model = $this->getModelBySubject($request, $subject);
106113
$type = $this->typeFactory->getTypeByObject($model);
107114

108-
$result = $this->restHandler->run($request->request->all(), $type, null, RestService::HTTP_PUT);
115+
$result = $this->restHandler->run($request->request->all(), $type, null, strtolower($request->getMethod()));
109116
$view = View::create($result)->setFormat('json');
110117

111118
return $this->viewHandler->handle($view, $request);
@@ -138,33 +145,23 @@ public function postDocumentAction(Request $request)
138145
return $this->viewHandler->handle($view, $request);
139146
}
140147

141-
return Response::create('The document could not be created', 500);
148+
return Response::create('The document was not created', 500);
142149
}
143150

144151
/**
145-
* Handle document deletion.
146-
*
147-
* @param Request $request
148-
* @param string $subject URL of the subject, ie: cms/simple/news/news-name
149-
*
150-
* @return Response
151-
*
152-
* @throws AccessDeniedException If the action is not allowed by the access
153-
* checker.
152+
* @deprecated Use updateDocumentAction
154153
*/
155-
public function deleteDocumentAction(Request $request, $subject)
154+
public function putDocumentAction(Request $request, $subject)
156155
{
157-
if (!$this->accessChecker->check($request)) {
158-
throw new AccessDeniedException();
159-
}
160-
161-
$model = $this->getModelBySubject($request, $subject);
162-
$type = $this->typeFactory->getTypeByObject($model);
163-
164-
$result = $this->restHandler->run($request->request->all(), $type, $subject, RestService::HTTP_DELETE);
165-
$view = View::create($result)->setFormat('json');
156+
return self::updateDocumentAction($request, $subject);
157+
}
166158

167-
return $this->viewHandler->handle($view, $request);
159+
/**
160+
* @deprecated Use updateDocumentAction
161+
*/
162+
public function deleteDocumentAction(Request $request, $subject)
163+
{
164+
return self::updateDocumentAction($request, $subject);
168165
}
169166

170167
/**

Resources/config/routing/rest.xml

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,16 @@
44
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
55
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
66

7-
<route id="cmf_create_put_document" pattern="/{_locale}/symfony-cmf/create/document/{subject}">
8-
<default key="_controller">cmf_create.rest.controller:putDocumentAction</default>
9-
<default key="_format">json</default>
10-
<requirement key="subject">.+</requirement>
11-
<requirement key="_method">PUT</requirement>
12-
</route>
13-
147
<route id="cmf_create_post_document" pattern="/{_locale}/symfony-cmf/create/document/{subject}">
158
<default key="_controller">cmf_create.rest.controller:postDocumentAction</default>
169
<default key="_format">json</default>
1710
<requirement key="_method">POST</requirement>
1811
</route>
1912

20-
<route id="cmf_create_delete_document" pattern="/{_locale}/symfony-cmf/create/document/{subject}">
21-
<default key="_controller">cmf_create.rest.controller:deleteDocumentAction</default>
13+
<route id="cmf_create_put_document" pattern="/{_locale}/symfony-cmf/create/document/{subject}">
14+
<default key="_controller">cmf_create.rest.controller:updateDocumentAction</default>
2215
<default key="_format">json</default>
2316
<requirement key="subject">.+</requirement>
24-
<requirement key="_method">DELETE</requirement>
2517
</route>
2618

2719
<route id="cmf_create_workflows" pattern="/{_locale}/symfony-cmf/create/workflows/{subject}">

0 commit comments

Comments
 (0)