2.9 KiB
Executable File
2.9 KiB
Executable File
LightMVC - Lightweight PHP MVC Framework
A minimal, PSR-4 compliant PHP MVC framework designed for simplicity and scalability.
Directory Structure
├── app/
│ ├── Controllers/ # Application logic (Controllers)
│ ├── Models/ # Data structures (Models)
│ └── Views/ # UI templates (Views)
├── core/ # Framework engine
│ ├── Application.php
│ ├── Controller.php
│ ├── Request.php
│ ├── Response.php
│ ├── Router.php
│ └── View.php
├── public/ # Entry point and static assets
│ ├── css/ # Custom CSS files
│ ├── js/ # Custom JS files
│ └── images/ # Static images
├── node_modules/ # Frontend dependencies
├── vendor/ # Composer dependencies
Asset Management & npm
This project uses npm for frontend dependencies (e.g., Bootstrap).
- Initialize/Install:
npm install - Add custom CSS/JS in
public/css/app.cssandpublic/js/app.js. - Reference assets in
app/Views/layouts/main.php.
How It Works
- Request Flow: Requests hit
public/index.php. - Initialization:
Core\ApplicationinitializesRequest,Response,Router, andView. - Routing: The
Routermatches theRequestpath against registered routes. - Dispatching: If a match is found, the
Routerinstantiates the controller and invokes the requested method. - Controller Action: The controller handles business logic and returns a
Viewusing$this->render(). - Response:
Viewloads the layout fromapp/Views/layouts/main.php, injects the view content into the{{content}}placeholder, and returns the response.
Adding a New Route
In public/index.php, register a route mapping a path to a controller method:
$app->router->get('/path', [ControllerClass::class, 'methodName']);
API Routing
API routes are registered in public/index.php with the /api/ prefix to keep them separate from regular page routes.
Example API Route
$app->router->get('/api/ping', [App\Controllers\ApiController::class, 'ping']);
API Controller
The ApiController class lives in app/Controllers/ApiController.php and should contain methods for API endpoints.
Example:
<?php
namespace App\Controllers;
use Core\Controller;
class ApiController extends Controller
{
public function ping()
{
// Simple health-check endpoint
$this->response->setHeader('Content-Type', 'application/json');
$this->response->setBody(json_encode(['status' => 'ok', 'time' => microtime(true)]));
}
}
Testing the API
You can test the /api/ping endpoint with:
curl http://localhost/api/ping
Expected response:
{"status":"ok","time":1718409600.123}