Skip to content

Commit

Permalink
added config parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
saibotd committed Aug 22, 2017
1 parent 7cbfb8f commit a930e8c
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 67 deletions.
22 changes: 11 additions & 11 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -60,24 +60,24 @@ 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**
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

Expand Down
6 changes: 5 additions & 1 deletion src/ContaoContentApiBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
5 changes: 0 additions & 5 deletions src/ContaoManager/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/**
Expand Down
16 changes: 12 additions & 4 deletions src/Controller/ContentApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down Expand Up @@ -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);
}


}
21 changes: 21 additions & 0 deletions src/DependencyInjection/ContentApiExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
namespace DieSchittigs\ContaoContentApiBundle\DependencyInjection;

use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Definition\Processor;

class ContentApiExtension extends Extension
{

/**
* {@inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('parameters.yml');
}
}
3 changes: 3 additions & 0 deletions src/Resources/config/parameters.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
parameters:
content_api_readers:
news: NewsModel
19 changes: 9 additions & 10 deletions src/Resources/contao/classes/FrontendApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class FrontendApi

public function __construct($readers = null)
{
if($readers) $this->readers = $readers;
$apiUser = new ApiUser;
$this->user = $apiUser->getUser();
}
Expand Down Expand Up @@ -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(
Expand Down
36 changes: 0 additions & 36 deletions src/Resources/contao/dca/tl_contentapi.php

This file was deleted.

0 comments on commit a930e8c

Please sign in to comment.