Examples
Routing, controllers, middleware, and other components in focused technical articles.
Install the framework, add a route and controller, then check the response in your browser.
Create a Composer project with PSR-4 autoloading and install the framework package. The final command prepares the directories used below.
mkdir lemonade-demo
cd lemonade-demo
composer init --name=demo/lemonade-app --type=project --autoload=app/ --no-interaction
composer require johnnyxlemonade/framework
mkdir -p app/Config app/Controllers publicThis is the minimal application you will have at the end of the guide. Create public/index.php from the second example now; the route and controller follow in the next steps.
lemonade-demo/
├── app/
│ ├── Config/
│ │ └── Routing.php
│ └── Controllers/
│ └── HomeController.php
├── public/
│ └── index.php
├── composer.json
└── vendor/<?php
declare(strict_types=1);
use Lemonade\Framework\Core\Context\ApplicationContextFactory;
use Lemonade\Framework\Core\KernelFactory;
require dirname(__DIR__) . '/vendor/autoload.php';
$context = (new ApplicationContextFactory())->fromGlobals(dirname(__DIR__));
$kernel = (new KernelFactory())->create($context);
$kernel->handle();Add a named GET route for / to app/Config/Routing.php. It points to the controller’s public index method.
<?php
declare(strict_types=1);
use Demo\LemonadeApp\Controllers\HomeController;
use Lemonade\Framework\Routing\ControllerAction;
use Lemonade\Framework\Routing\Router;
return static function (Router $router): void {
$router->getNamed('home', '/', ControllerAction::for(HomeController::class, 'index'));
};Save the class as app/Controllers/HomeController.php. Composer loads it, the framework injects Responses, and the public index method handles the route.
<?php
declare(strict_types=1);
namespace Demo\LemonadeApp\Controllers;
use Lemonade\Framework\Http\Response\Responses;
use Psr\Http\Message\ResponseInterface;
final class HomeController
{
public function __construct(private readonly Responses $responses)
{
}
public function index(): ResponseInterface
{
return $this->responses->html('<h1>Hello from Lemonade!</h1>');
}
}Start PHP’s built-in server from the project root and open http://localhost:8000/. You should see HTTP 200 and the heading “Hello from Lemonade!”.
php -S localhost:8000 -t public