From a930e8cce85581df262db93c539790b8af03c872 Mon Sep 17 00:00:00 2001 From: Tobias Duehr Date: Tue, 22 Aug 2017 11:55:42 +0200 Subject: [PATCH] added config parameters --- readme.md | 22 ++++++------ src/ContaoContentApiBundle.php | 6 +++- src/ContaoManager/Plugin.php | 5 --- src/Controller/ContentApiController.php | 16 ++++++--- .../ContentApiExtension.php | 21 +++++++++++ src/Resources/config/parameters.yml | 3 ++ src/Resources/contao/classes/FrontendApi.php | 19 +++++----- src/Resources/contao/dca/tl_contentapi.php | 36 ------------------- 8 files changed, 61 insertions(+), 67 deletions(-) create mode 100644 src/DependencyInjection/ContentApiExtension.php create mode 100644 src/Resources/config/parameters.yml delete mode 100644 src/Resources/contao/dca/tl_contentapi.php diff --git a/readme.md b/readme.md index be58696..8530b6c 100644 --- a/readme.md +++ b/readme.md @@ -23,9 +23,9 @@ Please note that the API is **not compatible with Contao 3.x**. Install [composer](https://getcomposer.org) if you haven't already, enter this command in the main directory of your Contao installation: - composer require dieschittigs/contao-content-api + composer require dieschittigs/contao-content-api-bundle -Contao Content API is now installed. +Contao Content API is now installed and ready to use. ## Usage @@ -37,15 +37,15 @@ Gets the sitemap (=all pages below root). ##### /api/page?url=/about/team.html -Gets the page, including all articles and contents at this URL. +Gets the page, including all articles and contents at the `url`. ##### /api/news?url=/news/new-website.html -Gets the news reader content from the URL +Gets the news reader content from the `url` ##### /api/?url=/page/or/newsarticle.html -Tries to get the page at this URL, or content from a reader +Tries to get the page at the `url`, and contents from any reader ##### /api/user @@ -60,16 +60,16 @@ have a multilingual website. ## Custom readers -**Warning: Doesn't work right now** - Contao has the concept of Reader Module (e.g. News Reader). These can be inserted anywhere inside of an article where they read the URL to display their contents. If you want to add additional Reader Modules, you can do -so by adding them in your `api.php`. +so by adding them in your `parameters.yml`. + parameters: ... - $api = new FrontendApi($loader); - $api->addReader('blog', 'BlogModel'); + content_api_readers: + news: NewsModel + blog: BlogModel ... Please note that the second parameter is the **model** class, **not the module** @@ -77,7 +77,7 @@ class. The new reader is now available at ##### /api/blog?url=/blog/detail/on-the-topic.html -or, even more convenient, at +or, if you want to include the articles, at ##### /api?url=/blog/detail/on-the-topic.html diff --git a/src/ContaoContentApiBundle.php b/src/ContaoContentApiBundle.php index 5d21125..aaaae21 100644 --- a/src/ContaoContentApiBundle.php +++ b/src/ContaoContentApiBundle.php @@ -3,8 +3,12 @@ namespace DieSchittigs\ContaoContentApiBundle; use Symfony\Component\HttpKernel\Bundle\Bundle; - +use DieSchittigs\ContaoContentApiBundle\DependencyInjection\ContentApiExtension; class ContaoContentApiBundle extends Bundle { + public function getContainerExtension() + { + return new ContentApiExtension(); + } } diff --git a/src/ContaoManager/Plugin.php b/src/ContaoManager/Plugin.php index bf6c6b9..c01fd5e 100644 --- a/src/ContaoManager/Plugin.php +++ b/src/ContaoManager/Plugin.php @@ -9,11 +9,6 @@ use Symfony\Component\Config\Loader\LoaderResolverInterface; use Symfony\Component\HttpKernel\KernelInterface; -/** - * Plugin for the Contao Manager. - * - * @author Glen Langer (BugBuster) - */ class Plugin implements BundlePluginInterface, RoutingPluginInterface { /** diff --git a/src/Controller/ContentApiController.php b/src/Controller/ContentApiController.php index 3a8678a..f9a9b24 100644 --- a/src/Controller/ContentApiController.php +++ b/src/Controller/ContentApiController.php @@ -14,11 +14,9 @@ class ContentApiController extends Controller { - public function __construct(){ - $this->frontendApi = new FrontendApi(); - } - private function handle(Request $request){ + $readers = $this->getParameter('content_api_readers'); + $this->frontendApi = new FrontendApi($readers); $this->container->get('contao.framework')->initialize(); return $this->json($this->frontendApi->handle($request)); } @@ -73,5 +71,15 @@ public function indexAction(Request $request) return $this->handle($request); } + /** + * @return Response + * + * @Route("/{reader}", name="content_api_reader") + */ + public function readerAction(Request $request) + { + return $this->handle($request); + } + } diff --git a/src/DependencyInjection/ContentApiExtension.php b/src/DependencyInjection/ContentApiExtension.php new file mode 100644 index 0000000..d6a73c7 --- /dev/null +++ b/src/DependencyInjection/ContentApiExtension.php @@ -0,0 +1,21 @@ +load('parameters.yml'); + } +} diff --git a/src/Resources/config/parameters.yml b/src/Resources/config/parameters.yml new file mode 100644 index 0000000..7be1d0e --- /dev/null +++ b/src/Resources/config/parameters.yml @@ -0,0 +1,3 @@ +parameters: + content_api_readers: + news: NewsModel diff --git a/src/Resources/contao/classes/FrontendApi.php b/src/Resources/contao/classes/FrontendApi.php index 8c3ca67..32474a9 100644 --- a/src/Resources/contao/classes/FrontendApi.php +++ b/src/Resources/contao/classes/FrontendApi.php @@ -20,6 +20,7 @@ class FrontendApi public function __construct($readers = null) { + if($readers) $this->readers = $readers; $apiUser = new ApiUser; $this->user = $apiUser->getUser(); } @@ -85,28 +86,26 @@ public function handle(Request $request) true ); foreach ($this->readers as $reader => $readerClass) { - $readerResult = $this->handleReaders( + $readerResult = $this->handleReader( $reader, $request->query->get('url', null) ); if ($readerResult) { $result->{$reader} = $readerResult; - break; } } - if (!$result) { - return new Response( - json_encode(['error' => Response::HTTP_NOT_FOUND]), - Response::HTTP_NOT_FOUND, - ['Content-Type', 'application/json'] - ); - } break; + default: + $reader = substr($request->getPathInfo(), 5); + $result = $this->handleReader( + $reader, + $request->query->get('url', null) + ); } return $result; } - private function handleReaders($reader, $url) + private function handleReader($reader, $url) { if (array_key_exists($reader, $this->readers)) { return ReaderHelper::handle( diff --git a/src/Resources/contao/dca/tl_contentapi.php b/src/Resources/contao/dca/tl_contentapi.php deleted file mode 100644 index adacb14..0000000 --- a/src/Resources/contao/dca/tl_contentapi.php +++ /dev/null @@ -1,36 +0,0 @@ - array - ( - 'dataContainer' => 'Table', - 'ctable' => array('tl_contentapi'), - 'closed' => true, - 'sql' => array - ( - 'keys' => array - ( - 'id' => 'primary' - ) - ) - ), - - // Fields - 'fields' => array - ( - 'id' => array - ( - 'sql' => "int(10) unsigned NOT NULL auto_increment" - ), - 'tstamp' => array - ( - 'sql' => "int(10) unsigned NOT NULL default '0'" - ) - ) -);