symplify/symbiotic-controller

v2.2.0 2017-07-25 22:47 UTC

README

Build Status Downloads

Write controller once and let many other frameworks use it.

Install

composer require symplify/symbiotic-controller

Usage in Nette

Register Extensions to you App

# app/config/config.neon

extensions:
    - Symplify\SymbioticController\Adapter\Nette\DI\SymbioticControllerExtension
    - Contributte\EventDispatcher\DI\EventDispatcherExtension

1. Create app/Presenters/ContactPresenter.php Presenter with __invoke() Method

A. Classic Render Action

This is what you use the most often.

namespace App\Presenter;

use Symplify\SymbioticController\Contract\Template\TemplateRendererInterface;

final class StandalonePresenter
{
    /**
     * @var TemplateRendererInterface
     */
    private $templateRenderer;

    public function __construct(TemplateRendererInterface $templateRenderer)
    {
        $this->templateRenderer = $templateRenderer;
    }

    public function __invoke(): string
    {
        return $this->templateRenderer->renderFileWithParameters(
            __DIR__ . '/templates/Contact.latte'
        );
    }
}

B. The Simplest Response

namespace App\Presenters;

use Nette\Application\Responses\TextResponse;

final class ContactPresenter
{
    public function __invoke(): TextResponse
    {
        return new TextResponse('Hi!');
    }
}

C. Or Json Response

namespace App\Presenters;

use Nette\Application\Responses\JsonResponse;

final class ContactPresenter
{
    public function __invoke(): TextResponse
    {
        return new JsonResponse('Hi!');
    }
}

2. Create app/templates/Contact.latte template

Hey :-)

3. Register Presenter Route

# app/Router/RouterFactory.php

namespace App\Router;

final class RouterFactory
{
    public function create(): RouteList
    {
        $routes = new RouteList;
        $routes[] = new PresenterRoute('/contact', ContactPresenter::class);
        $routes[] = new Route('<presenter>/<action>', 'Homepage:default');

        return $routes;
    }
}

4. Open page in Browser and Enjoy

That's all :)