Skip to content

Commit 1e0ceb5

Browse files
committed
[TASK] Get rid of the AbstractController (#176)
It is confusing to ppl, why our controllers do not extend the Actioncontroller Releases: main, 13.4 (cherry picked from commit dc4d518)
1 parent 3ea96ed commit 1e0ceb5

File tree

4 files changed

+99
-100
lines changed

4 files changed

+99
-100
lines changed

Classes/Controller/AbstractController.php

Lines changed: 0 additions & 97 deletions
This file was deleted.

Classes/Controller/BlogController.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@
2727
use TYPO3\CMS\Core\Type\ContextualFeedbackSeverity;
2828
use TYPO3\CMS\Extbase\Annotation\IgnoreValidation;
2929
use TYPO3\CMS\Extbase\Annotation\Validate;
30+
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
3031
use TYPO3\CMS\Extbase\Pagination\QueryResultPaginator;
32+
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
3133

3234
/**
3335
* The blog controller for the BlogExample extension
3436
*/
35-
class BlogController extends AbstractController
37+
class BlogController extends ActionController
3638
{
3739
/**
3840
* BlogController constructor.
@@ -180,4 +182,34 @@ public function showBlogAjaxAction(Blog $blog): ResponseInterface
180182
$jsonOutput = json_encode($blog);
181183
return $this->jsonResponse($jsonOutput);
182184
}
185+
/**
186+
* Override getErrorFlashMessage to present
187+
* nice flash error messages.
188+
*/
189+
protected function getErrorFlashMessage(): string
190+
{
191+
$defaultFlashMessage = parent::getErrorFlashMessage();
192+
$locallangKey = sprintf(
193+
'error.%s.%s',
194+
$this->request->getControllerName(),
195+
$this->actionMethodName,
196+
);
197+
return LocalizationUtility::translate($locallangKey, 'BlogExample') ?? $defaultFlashMessage;
198+
}
199+
200+
protected function hasBlogAdminAccess(): bool
201+
{
202+
// TODO access protection
203+
return true;
204+
}
205+
206+
/**
207+
* @throws NoBlogAdminAccessException
208+
*/
209+
protected function checkBlogAdminAccess(): void
210+
{
211+
if (!$this->hasBlogAdminAccess()) {
212+
throw new NoBlogAdminAccessException();
213+
}
214+
}
183215
}

Classes/Controller/CommentController.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
use T3docs\BlogExample\Exception\NoBlogAdminAccessException;
1313
use T3docs\BlogExample\Property\TypeConverters\HiddenCommentConverter;
1414
use TYPO3\CMS\Core\Type\ContextualFeedbackSeverity;
15+
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
1516
use TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException;
17+
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
1618

1719
/*
1820
* This file is part of the TYPO3 CMS project.
@@ -30,7 +32,7 @@
3032
/**
3133
* The comment controller for the BlogExample extension
3234
*/
33-
class CommentController extends AbstractController
35+
class CommentController extends ActionController
3436
{
3537
/**
3638
* CommentController constructor.
@@ -109,4 +111,34 @@ public function deleteAllAction(Post $post): ResponseInterface
109111
['post' => $post, 'blog' => $post->getBlog()],
110112
);
111113
}
114+
/**
115+
* Override getErrorFlashMessage to present
116+
* nice flash error messages.
117+
*/
118+
protected function getErrorFlashMessage(): string
119+
{
120+
$defaultFlashMessage = parent::getErrorFlashMessage();
121+
$locallangKey = sprintf(
122+
'error.%s.%s',
123+
$this->request->getControllerName(),
124+
$this->actionMethodName,
125+
);
126+
return LocalizationUtility::translate($locallangKey, 'BlogExample') ?? $defaultFlashMessage;
127+
}
128+
129+
protected function hasBlogAdminAccess(): bool
130+
{
131+
// TODO access protection
132+
return true;
133+
}
134+
135+
/**
136+
* @throws NoBlogAdminAccessException
137+
*/
138+
protected function checkBlogAdminAccess(): void
139+
{
140+
if (!$this->hasBlogAdminAccess()) {
141+
throw new NoBlogAdminAccessException();
142+
}
143+
}
112144
}

Classes/Controller/PostController.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
use TYPO3\CMS\Core\Type\ContextualFeedbackSeverity;
1919
use TYPO3\CMS\Extbase\Annotation\IgnoreValidation;
2020
use TYPO3\CMS\Extbase\Http\ForwardResponse;
21+
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
2122
use TYPO3\CMS\Extbase\Pagination\QueryResultPaginator;
2223
use TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException;
2324
use TYPO3\CMS\Extbase\Property\Exception;
2425
use TYPO3\CMS\Extbase\Property\PropertyMapper;
26+
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
2527

2628
/*
2729
* This file is part of the TYPO3 CMS project.
@@ -39,7 +41,7 @@
3941
/**
4042
* The post controller for the BlogExample extension
4143
*/
42-
class PostController extends AbstractController
44+
class PostController extends ActionController
4345
{
4446
/**
4547
* PostController constructor.
@@ -235,4 +237,34 @@ public function deleteAction(
235237
$this->addFlashMessage('The post has been deleted.', 'Deleted', ContextualFeedbackSeverity::INFO);
236238
return $this->redirect('index', null, null, ['blog' => $blog]);
237239
}
240+
/**
241+
* Override getErrorFlashMessage to present
242+
* nice flash error messages.
243+
*/
244+
protected function getErrorFlashMessage(): string
245+
{
246+
$defaultFlashMessage = parent::getErrorFlashMessage();
247+
$locallangKey = sprintf(
248+
'error.%s.%s',
249+
$this->request->getControllerName(),
250+
$this->actionMethodName,
251+
);
252+
return LocalizationUtility::translate($locallangKey, 'BlogExample') ?? $defaultFlashMessage;
253+
}
254+
255+
protected function hasBlogAdminAccess(): bool
256+
{
257+
// TODO access protection
258+
return true;
259+
}
260+
261+
/**
262+
* @throws NoBlogAdminAccessException
263+
*/
264+
protected function checkBlogAdminAccess(): void
265+
{
266+
if (!$this->hasBlogAdminAccess()) {
267+
throw new NoBlogAdminAccessException();
268+
}
269+
}
238270
}

0 commit comments

Comments
 (0)