Skip to content

Commit

Permalink
set language more reliable and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
saibotd committed Apr 21, 2017
1 parent 59b5425 commit f5d3790
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 7 deletions.
93 changes: 90 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
@@ -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:

<?php

Expand All @@ -18,7 +41,71 @@ Paste the following contents into it:
$response = $api->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 :)
19 changes: 16 additions & 3 deletions src/FrontendApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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':
Expand Down
1 change: 0 additions & 1 deletion src/PageHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/ReaderHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Symfony\Component\HttpFoundation\Response;
use Contao\ContentModel;
use Contao\Controller;
use Contao\ModuleNewsReader;

class ReaderHelper
{
Expand Down

0 comments on commit f5d3790

Please sign in to comment.