From f5d3790adbaedfab7b4da44aec4f2ebc7eaf29d3 Mon Sep 17 00:00:00 2001 From: Tobias Duehr Date: Fri, 21 Apr 2017 14:46:36 +0200 Subject: [PATCH] set language more reliable and readme --- readme.md | 93 ++++++++++++++++++++++++++++++++++++++++++-- src/FrontendApi.php | 19 +++++++-- src/PageHelper.php | 1 - src/ReaderHelper.php | 1 + 4 files changed, 107 insertions(+), 7 deletions(-) diff --git a/readme.md b/readme.md index be36d55..c127eb2 100644 --- a/readme.md +++ b/readme.md @@ -1,9 +1,32 @@ +# Contao Content API + +We at [Die Schittigs](http://www.dieschittigs.de) love +[Contao](https://contao.org/de/), but the web moves +forward and static HTML templating just doesn't cut it anymore. Thus we came up +with an easily digestible JSON-API to access Contao content via JavaScript +(or anything else that can handle JSON). + +With the Contao Content API it is possible to write the entire Frontend of your +website in [React.js](https://facebook.github.io/react/), [Angular](https://angular.io/), [vue](https://vuejs.org/), or any other +JS-framework. All while still using the great Contao Backend. + +Now that Contao 4 matures, we decided to Open Source our efforts. Mainly to get +feedback and maybe even contribution from the community. + +## Requirements + +You'll need an up-and-running **Contao 4.3.x** installation. +Please note that the API is **not compatible with Contao 3.x**. + ## Installation +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 -Put a file called "api.php" into your /web folder. -Paste the following contents into it: +Contao Content API is now installed. Next, put a file called `api.php` into your +`/web` folder. Paste the following contents into it: handle($request); $response->send(); +Alternatively copy the file `api.php` from +`vendor/dieschittigs/contao-content-api/` into your `/web` folder. ## Usage -TODO +Once setup, the following routes are available: + +##### /api.php/sitemap + +Gets the sitemap (=all pages below root) for the given language. + +##### /api.php/page?url=/en/about/team.html + +Gets the page, including all articles and contents at this URL. + +##### /api.php/news?url=/en/news/new-website.html + +Gets the news reader content from the URL + +##### /api.php/?url=/page/or/newsarticle.html + +Tries to get the page at this URL, or content from a reader + +##### /api.php/user + +Gets the logged-in frontend user, if available. + +## Custom readers + +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`. + + ... + $api = new FrontendApi($loader); + $api->addReader('blog', 'BlogModel'); + ... + +Please note that the second parameter is the **model** class, **not the module** +class. The new reader is now available at + +##### /api.php/blog?url=/en/blog/detail/on-the-topic.html + +or, even more convenient, at + +##### /api.php/url=/en/blog/detail/on-the-topic.html + +Internally the API tries to instantiate the model with the alias found in the url. +It also tries to add all `ContentModels` it can find. + +## Tips and tricks + +If you are using a router in JavaScript (e.g. react-router) you may want to redirect +all frontend traffic to a single file. We found the easiest way to do so is to +create a new `app.html` in the `/web` folder and change the redirect in `.htaccess` +like so: + + ... + RewriteRule ^contao %{ENV:BASE}/app.php [L] + RewriteRule ^ %{ENV:BASE}/app.html [L] + ... + +This way all the traffic goes to your JS App, while `/contao` still works. + +## Contribution + +Bug reports and pull requests are very welcome :) diff --git a/src/FrontendApi.php b/src/FrontendApi.php index d64c13c..ad0a138 100644 --- a/src/FrontendApi.php +++ b/src/FrontendApi.php @@ -5,6 +5,8 @@ use Contao\ManagerBundle\ContaoManager\Plugin as ManagerBundlePlugin; use Contao\ManagerBundle\HttpKernel\ContaoCache; use Contao\ManagerBundle\HttpKernel\ContaoKernel; +use Contao\Environment; +use Contao\System; use Doctrine\Common\Annotations\AnnotationRegistry; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -34,11 +36,22 @@ public function addReader($reader, $class) $this->readers[$reader] = $class; } + private function setLang(Request $request){ + $globalLang = $request->query->get('lang', Helper::defaultLang()); + $GLOBALS['TL_LANGUAGE'] = $globalLang; + $_SESSION['TL_LANGUAGE'] = $globalLang; + System::loadLanguageFile('default', $globalLang); + } + + private function overrideRequestUri(Request $request){ + $_SERVER["REQUEST_URI"] = $request->query->get('url', $_SERVER["REQUEST_URI"]); + } + public function handle(Request $request) { - if ($request->query->get('url')) { - $_SERVER["REQUEST_URI"] = $request->query->get('url'); - } + $this->overrideRequestUri($request); + $this->setLang($request); + $result = null; switch ($request->getPathInfo()) { case '/sitemap': diff --git a/src/PageHelper.php b/src/PageHelper.php index f07bb48..7fa1ff8 100644 --- a/src/PageHelper.php +++ b/src/PageHelper.php @@ -66,7 +66,6 @@ public static function getPage($url) return; } Controller::setStaticUrls($page); - $pageUrl = Controller::generateFrontendUrl($page->row()); $_page = self::parsePage($page); $_page->articles = ArticleHelper::pageArticles($page->id); return $_page; diff --git a/src/ReaderHelper.php b/src/ReaderHelper.php index a1b67f9..1d9ad9f 100644 --- a/src/ReaderHelper.php +++ b/src/ReaderHelper.php @@ -10,6 +10,7 @@ use Symfony\Component\HttpFoundation\Response; use Contao\ContentModel; use Contao\Controller; +use Contao\ModuleNewsReader; class ReaderHelper {