Introduction
Are you looking to build a custom Content Management System (CMS) using Laravel 12 with SQLite? This guide will walk you through setting up a Laravel project, implementing the Singleton pattern, using interfaces, and creating CRUD operations.
Steps to Build a Custom CMS in Laravel 12
1. Install Laravel 12
Run the following command to create a new Laravel project:
composer create-project --prefer-dist laravel/laravel cms
2. Configure SQLite Database
Update the .env
file with SQLite settings:
DB_CONNECTION=sqlite
DB_DATABASE=/absolute/path/to/database.sqlite
Then, create the SQLite database file:
touch database/database.sqlite
3. Implement Singleton Pattern
Create a singleton service for managing CMS settings:
namespace App\Services;
class CMSConfig {
private static ?CMSConfig $instance = null;
private array $config = [];
private function __construct() {}
public static function getInstance(): CMSConfig {
if (self::$instance === null) {
self::$instance = new CMSConfig();
}
return self::$instance;
}
public function set(string $key, $value): void {
$this->config[$key] = $value;
}
public function get(string $key) {
return $this->config[$key] ?? null;
}
}
4. Use Interfaces
Create an interface for CMS repositories:
namespace App\Repositories;
interface PageRepositoryInterface {
public function getAll();
public function find($id);
public function create(array $data);
public function update($id, array $data);
public function delete($id);
}
5. Create CRUD Operations
Generate a model and migration for CMS pages:
php artisan make:model Page -m
Define basic fields in the migration:
schema::create('pages', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('content');
$table->timestamps();
});
Run the migration:
php artisan migrate
Create a repository implementation:
namespace App\Repositories;
use App\Models\Page;
class PageRepository implements PageRepositoryInterface {
public function getAll() {
return Page::all();
}
public function find($id) {
return Page::find($id);
}
public function create(array $data) {
return Page::create($data);
}
public function update($id, array $data) {
return Page::where('id', $id)->update($data);
}
public function delete($id) {
return Page::destroy($id);
}
}
6. Register Repository in Service Provider
Bind the interface and implementation in AppServiceProvider.php
:
use App\Repositories\PageRepository;
use App\Repositories\PageRepositoryInterface;
public function register() {
$this->app->bind(PageRepositoryInterface::class, PageRepository::class);
}
7. Create a Controller
Generate a controller for managing pages:
php artisan make:controller PageController --resource
8. Define Routes
Add routes in routes/web.php
:
Route::resource('/admin/pages', PageController::class);
9. Create Views
Set up Blade templates for managing pages.
10. Run and Test
Start the Laravel server:
php artisan serve
Conclusion
With Laravel 12, SQLite, Singleton Pattern, and Interfaces, you can build a scalable CMS efficiently. Follow these steps to create your own CMS.
0 Comments