Bolt is a fast and lightweight PHP framework. Its simple and flexible structure makes it ideal for small to medium-sized projects.
- MVC (Model-View-Controller) architecture
- Simple routing system
- Easy and fast dependency injection
- View rendering support
- Create a new project using Composer:
composer create-project bayrameker/my-bolt-framework new-project
cd new-project
- Install the necessary dependencies:
composer install
- Create the
.env
file and configure the necessary settings:
cp .env.example .env
- Start the server:
php bolt serve
The available commands for the Bolt framework are:
php bolt migrate
- Runs the database migrations.php bolt create:migration {name}
- Creates a new database migration.php bolt seed
- Runs the database seeders.php bolt controller {name} [-v]
- Creates a new controller. Use the-v
option to also add a view and route.php bolt model {name}
- Creates a new model.php bolt service {name}
- Creates a new service.php bolt repository {name}
- Creates a new repository.php bolt dump-autoload
- Updates Composer autoload files.php bolt serve
- Starts the application.
You can add new routes in the routes/web.php
file:
$router->get('/home', [App\Controllers\HomeController::class, 'index']);
To create a new controller, create a new PHP file in the app/Controllers
directory:
<?php
namespace App\Controllers;
use Core\Controller;
use Core\Request;
use Core\Response;
use Core\ViewRenderer;
use App\Services\HomeService;
class HomeController extends Controller
{
protected $homeService;
public function __construct(HomeService $homeService)
{
$this->homeService = $homeService;
}
public function index(Request $request, Response $response)
{
$homeData = $this->homeService->getHomeData();
$viewRenderer = new ViewRenderer('home/index', [
'title' => $homeData->title,
'message' => $homeData->message,
'layout' => 'layout'
]);
$viewRenderer->render();
}
}
Services control the business logic. Create a new PHP file in the app/Services
directory:
<?php
namespace App\Services;
use App\Repositories\HomeRepository;
class HomeService
{
protected $homeRepository;
public function __construct(HomeRepository $homeRepository)
{
$this->homeRepository = $homeRepository;
}
public function getHomeData()
{
return $this->homeRepository->getHomeData();
}
}
Repositories control the data access layer. Create a new PHP file in the app/Repositories
directory:
<?php
namespace App\Repositories;
use App\Models\Home;
class HomeRepository
{
public function getHomeData()
{
return new Home('Home Page', 'Welcome to My Bolt Framework!');
}
}
Models represent the data structure. Create a new PHP file in the app/Models
directory:
<?php
namespace App\Models;
class Home
{
public $title;
public $message;
public function __construct($title, $message)
{
$this->title = $title;
$this->message = $message;
}
}
View files control the HTML content displayed to the user. Create new PHP files in the app/Views
directory:
<main>
<h1><?= $title ?? 'Default Title' ?></h1>
<p><?= $message ?? 'Default Message' ?></p>
</main>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?= $title ?? 'Default Title' ?></title>
</head>
<body>
<?= $content ?>
</body>
</html>
We welcome contributions! Please open an issue first to discuss any changes you would like to make.
- Fork the repository
- Create a new branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
This project is licensed under the MIT License. See the LICENSE
file for more information.