How to Write Microsites

Imagine that you need to quickly create a small website for an upcoming event of your company. It should be simple, fast, and without unnecessary complications. You might think that such a small project doesn't require a robust framework. But what if using the Nette framework can significantly simplify and speed up this process?

Even when creating simple websites, you don't want to give up comfort. You don't want to reinvent the wheel. Feel free to be lazy and pamper yourself. The Nette Framework can also be excellently used as a micro framework.

What might such a microsite look like? For example, the entire code of the website can be placed in a single index.php file in the public folder:

<?php

require __DIR__ . '/../vendor/autoload.php';

$configurator = new Nette\Bootstrap\Configurator;
$configurator->enableTracy(__DIR__ . '/../log');
$configurator->setTempDirectory(__DIR__ . '/../temp');

// create a DI container based on the configuration in config.neon
$configurator->addConfig(__DIR__ . '/../app/config.neon');
$container = $configurator->createContainer();

// set up routing
$router = new Nette\Application\Routers\RouteList;
$container->addService('router', $router);

// route for URL https://example.com/
$router->addRoute('', function ($presenter, Nette\Http\Request $httpRequest) {
	// detect browser language and redirect to URL /en or /de etc.
	$supportedLangs = ['en', 'de', 'cs'];
	$lang = $httpRequest->detectLanguage($supportedLangs) ?: reset($supportedLangs);
	$presenter->redirectUrl("/$lang");
});

// route for URL https://example.com/cs nebo https://example.com/en
$router->addRoute('<lang cs|en>', function ($presenter, string $lang) {
	// display the appropriate template, for example ../templates/en.latte
	$template = $presenter->createTemplate()
		->setFile(__DIR__ . '/../templates/' . $lang . '.latte');
	return $template;
});

// run the application!
$container->getByType(Nette\Application\Application::class)->run();

Everything else will be templates stored in the parent /templates folder.

The PHP code in index.php first sets up the environment, then defines routes, and finally runs the application. The advantage is that the second parameter of the addRoute() function can be a callable that is executed when the corresponding page is opened.

Why use Nette for Microsites?

  • Developers who have ever tried Tracy can't imagine coding without it today.
  • But above all, you will utilize the templating system Latte, because from just 2 pages, you will want to separate layout and content.
  • And you definitely want to rely on automatic escaping to prevent XSS vulnerabilities.
  • Nette also ensures that in case of an error, PHP error messages will never be displayed, but instead, a user-friendly page will.
  • If you want to get feedback from users, for example in the form of a contact form, you can also add forms and database.
  • You can also easily have filled-out forms sent by email.
  • Sometimes you might find caching useful, for instance, when downloading and displaying feeds.

In today's age, where speed and efficiency are key, it's important to have tools that allow you to achieve results without unnecessary delays. The Nette framework offers just that – fast development, security, and a wide range of tools like Tracy and Latte that simplify the process. Just install a few Nette packages, and building such a microsite becomes a breeze. And you know there are no hidden security flaws.