Symbiotic Controller
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
app/Presenters/ContactPresenter.php
Presenter with __invoke()
Method
1. Create 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!');
}
}
app/templates/Contact.latte
template
2. Create 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 :)