symplify/symbiotic-controller

[READ-ONLY] Invocable, single-action and framework-independent presenters in Nette


Keywords
adr, controller, invocable-controller, mvc, mvp, nette, php71, presenter
License
MIT

Documentation

Symbiotic Controller

Build Status Downloads Subscribe

Write controller once and let many other frameworks use it.

This repository is maintained by @Lexinek.

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 :)