This boilerplate provides a foundation for building fast, flexible, and modern websites using the Slim 4 micro-framework, Twig templating engine, and Tailwind CSS for styling. It is designed to be easily integrated with Contentful CMS, offering a powerful headless CMS solution for managing content. This stack is ideal for simple websites, blogs, portfolios, and landing pages that require a balance of performance, customization, and ease of content management.
This boilerplate leverages the following technologies:
.env
files..htaccess
file configured with security best practices..env
files to manage sensitive configuration details.mbstring
dom
json
git clone [repository-url]
cd [repository-directory]
Copy the .env.example
file to .env
:
cp .env.example .env
Open the .env
file and configure your Contentful credentials:
CONTENTFUL_SPACE_ID=your_space_id
CONTENTFUL_ACCESS_TOKEN=your_content_delivery_api_access_token
CONTENTFUL_ENVIRONMENT=master
Run Composer to install the project dependencies:
composer install
mod_rewrite
is enabled.AllowOverride
directive to All
for the public
directory.server {
listen 80;
server_name your-domain.local;
root /path/to/your/project/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
Use PHP's built-in web server for development:
php -S localhost:8080 -t public
Then, access your application in your browser at http://localhost:8080
.
Example: Fetching Blog Posts
<?php
use Contentful\Delivery\Client;
$client = new Client(
$_ENV['CONTENTFUL_ACCESS_TOKEN'],
$_ENV['CONTENTFUL_SPACE_ID'],
$_ENV['CONTENTFUL_ENVIRONMENT']
);
$entries = $client->getEntries(['content_type' => 'blogPost']);
foreach ($entries as $entry) {
$title = $entry->get('title');
$body = $entry->get('body');
}
Example: Displaying Blog Post in Twig Template
{% extends 'layouts/base.html.twig' %}
{% block content %}
<article>
<h1>{{ post.title }}</h1>
<div class="blog-body">
{{ post.body|raw }}
</div>
</article>
{% endblock %}
Example (Controller):
<?php
use Contentful\RichText\Parser;
use Contentful\RichText\Renderer;
$parser = new Parser();
$document = $parser->parse($richTextData);
$renderer = new Renderer();
$renderedHtml = $renderer->render($document);
Example Route Definition:
$app->get('/', [HomeController::class, 'index'])->setName('home');
$app->group('/blog', function (RouteCollectorProxy $group) {
$group->get('', [BlogController::class, 'index'])->setName('blog.index');
$group->get('/{slug}', [BlogController::class, 'show'])->setName('blog.show');
});
.htaccess
Security for Apache.This README provides a comprehensive technical overview and setup guide for the Slim 4, Twig, and Tailwind CSS boilerplate with Contentful integration. Use this as a starting point and customize it further to reflect the specific features and functionalities of your project.