Php-Slim with Twig Sample

Title: Building a Simple Web Application with SlimPHP and Twig on Fedora Linux


Introduction

SlimPHP is a lightweight PHP framework that makes it easy to build web applications. Twig is a flexible, fast, and secure template engine for PHP. Together, they provide a powerful and efficient way to develop web applications. In this guide, we’ll walk you through setting up a SlimPHP application with Twig templating on Fedora Linux.


Step 1: Install PHP and Composer

First, ensure you have PHP and Composer installed on your Fedora system. Open a terminal and run:

sudo dnf install -y php php-cli php-common php-mbstring php-xml
sudo dnf install -y composer

Verify the installations:

php --version
composer --version

Step 2: Create a New SlimPHP Project

Create a new directory for your SlimPHP project and navigate into it:

mkdir my-slim-app
cd my-slim-app

Use Composer to create a new SlimPHP project:

composer require slim/slim "^4.0"
composer require slim/psr7
composer require nyholm/psr7
composer require slim/twig-view

Step 3: Set Up the Project Structure

Create the following directory structure within your project:

my-slim-app/
├── public/
│   └── index.php
├── templates/
│   └── home.twig
└── src/
    └── dependencies.php

Step 4: Configure SlimPHP with Twig

Edit the index.php file in the public directory:

<?php

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

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
use Slim\Views\Twig;
use Slim\Views\TwigMiddleware;

$app = AppFactory::create();

$twig = Twig::create('../templates', ['cache' => false]);
$app->add(TwigMiddleware::createFromContainer($app, Twig::class));

$app->get('/', function (Request $request, Response $response, $args) {
    $view = Twig::fromRequest($request);
    return $view->render($response, 'home.twig');
});

$app->run();

Step 5: Create a Twig Template

Create a home.twig file in the templates directory with the following content:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My Slim App</title>
</head>
<body>
    <h1>Welcome to My SlimPHP Application!</h1>
    <p>This is a simple web application using SlimPHP and Twig.</p>
</body>
</html>

Step 6: Serve Your Application

To serve your SlimPHP application, use the built-in PHP server. Navigate to your project directory and run:

php -S localhost:8080 -t public

Open your web browser and visit http://localhost:8080. You should see your Twig template rendered by SlimPHP.


Step 7: Add More Routes and Templates

You can add more routes and templates to expand your application. For example, create an about.twig template in the templates directory:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>About Us</title>
</head>
<body>
    <h1>About Us</h1>
    <p>This is the about page.</p>
</body>
</html>

Then, update index.php to include a new route for the “About” page:

<?php

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

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
use Slim\Views\Twig;
use Slim\Views\TwigMiddleware;

$app = AppFactory::create();

$twig = Twig::create('../templates', ['cache' => false]);
$app->add(TwigMiddleware::createFromContainer($app, Twig::class));

$app->get('/', function (Request $request, Response $response, $args) {
    $view = Twig::fromRequest($request);
    return $view->render($response, 'home.twig');
});

$app->get('/about', function (Request $request, Response $response, $args) {
    $view = Twig::fromRequest($request);
    return $view->render($response, 'about.twig');
});

$app->run();

Conclusion

You have successfully set up a SlimPHP application with Twig templating on Fedora Linux. This setup provides a robust foundation for building scalable and maintainable web applications. Feel free to expand your application by adding more routes, templates, and functionality as needed. Happy coding!


Leave a Reply

Your email address will not be published. Required fields are marked *