MVC Framework
A simple and lightweight PHP MVC Framework
Quick Start
1. Configure Environment
Copy env.example to .env and configure:
# Database (optional)
host=localhost
dbname=your_database
dbuser=root
dbpass=
# Application
APP_ENV=development
BASE_PATH=
MAINTENANCE_MODE=false
2. Install Dependencies
composer install
3. Start Building
Create your controllers, models, and views!
Project Structure
Core Files
index.php- Entry pointconfig.php- Configurationroutes.php- Route definitions.env- Environment variables
App Directory
app/Controllers/- Controllersapp/Models/- Modelsapp/Views/- Twig templatesapp/Core/- Core classesapp/Metafiles/- Translations
Assets
assets/css/- Stylesheetsassets/js/- JavaScriptassets/images/- Images
Other
vendor/- Composer dependenciescache/- Cache fileslogs/- Log files
Creating a Controller
Create a new controller in app/Controllers/:
<?php
namespace App\Controllers;
use App\Core\Controller;
class MyController extends Controller
{
public function index()
{
$this->loadPageTranslations('mypage');
$this->data += [
'viewName' => 'pages/mypage.html.twig',
'page' => [
'titulo' => 'My Page',
'description' => 'Page description',
'keywords' => 'keywords'
],
'customData' => 'Your custom data here'
];
echo $GLOBALS['twig']->render('templates/mainTemplate.html.twig', $this->data);
}
}
Creating Routes
Add routes in routes.php:
// Simple route
$router->get('/', 'App\Controllers\HomeController@index');
// Route with parameter
$router->get('/user/{id}', 'App\Controllers\UserController@show');
// POST route
$router->post('/submit', 'App\Controllers\FormController@submit');
Creating Views
Create Twig templates in app/Views/pages/:
<div class="container mt-5">
<h1>Framework Demo & Documentation</h1>
<p>Learn how to use the MVC Framework</p>
<p>Base URL: https://omeupredio.com/</p>
</div>
Using Translations
The framework includes a built-in translation system using JSON metafiles. Here's how to use it:
1. Create Translation Files
Create JSON files in app/Metafiles/{lang}/:
Example: app/Metafiles/pt/mypage.json
{
"titulo": "Minha Página",
"description": "Descrição da página",
"keywords": "palavras, chave",
"t": {
"welcome": "Bem-vindo",
"title": "Título da Página",
"button_save": "Guardar"
}
}
2. Load Translations in Controller
Use loadPageTranslations() method:
<?php
namespace App\Controllers;
use App\Core\Controller;
class MyController extends Controller
{
public function index()
{
// Load page-specific translations
$this->loadPageTranslations('mypage');
$this->data += [
'viewName' => 'pages/mypage.html.twig',
'page' => [
'titulo' => 'My Page'
]
];
echo $GLOBALS['twig']->render(
'templates/mainTemplate.html.twig',
$this->data
);
}
}
3. Use Translations in Views
Access translations in Twig templates:
<!-- Page translations -->
<h1></h1>
<button></button>
<!-- Global translations -->
<nav>
<a href="#"></a>
<a href="#"></a>
</nav>
<!-- Current language -->
<p>Current language: pt</p>
4. Translation File Structure
Each translation file should contain:
titulo- Page titledescription- Meta descriptionkeywords- Meta keywordst- Translation object with key-value pairs
Global translations:
Store common translations in app/Metafiles/{lang}/global.json
Translation Tips
- Global translations are automatically loaded in all controllers
- Page translations must be loaded explicitly with
loadPageTranslations() - Language is stored in session:
$_SESSION['lang'] - Default language is
'pt'if not set - Use the language selector in the header to switch languages
5. Language Switching
The framework includes a language switcher in the header. Users can switch between languages, and the selection is saved in the session.
// Language is automatically detected from session
// Current language: pt
// To manually set language in code:
$_SESSION['lang'] = 'en'; // or 'pt'
// Language route (already configured):
// GET /lang/{lang} → LanguageController@switch
Database Usage (Optional)
The framework works with or without a database. To use a database:
1. Configure .env
host=localhost
dbname=your_database
dbuser=root
dbpass=your_password
2. Create a Model
<?php
namespace App\Models;
use App\Core\Model;
class MyModel extends Model
{
public function getData()
{
$this->requireDatabase();
$stmt = $this->db->prepare("SELECT * FROM table");
$stmt->execute();
return $stmt->fetchAll();
}
}
Framework Features
Twig Templates
Powerful templating engine for clean views
Optional Database
Works with or without database connection
Email Service
PHPMailer integration for sending emails
Translations
Multi-language support with JSON metafiles
Routing
Simple and flexible URL routing system
Responsive
Modern, mobile-first responsive design
Code Examples
// routes.php
$router->get('/about', 'App\Controllers\AboutController@index');
// AboutController.php
public function index() {
$this->data += [
'viewName' => 'pages/about.html.twig',
'page' => ['titulo' => 'About Us']
];
echo $GLOBALS['twig']->render('templates/mainTemplate.html.twig', $this->data);
}
// routes.php
$router->get('/user/{id}', 'App\Controllers\UserController@show');
// UserController.php
public function show($id) {
// $id contains the parameter from URL
$this->data += [
'viewName' => 'pages/user.html.twig',
'userId' => $id
];
echo $GLOBALS['twig']->render('templates/mainTemplate.html.twig', $this->data);
}
// routes.php
$router->get('/contact', 'App\Controllers\ContactController@form');
$router->post('/contact', 'App\Controllers\ContactController@submit');
// ContactController.php
public function submit() {
$name = $_POST['name'] ?? '';
$email = $_POST['email'] ?? '';
// Process form data...
header('Location: ' . BASE_URL . 'contact?success=1');
}
// 1. Create app/Metafiles/pt/products.json
{
"titulo": "Produtos",
"t": {
"title": "Nossos Produtos",
"add_to_cart": "Adicionar ao Carrinho"
}
}
// 2. In ProductsController.php
public function index() {
$this->loadPageTranslations('products');
$this->data += [
'viewName' => 'pages/products.html.twig'
];
echo $GLOBALS['twig']->render('templates/mainTemplate.html.twig', $this->data);
}
// 3. In products.html.twig
<h1></h1>
<button></button>
Tips & Best Practices
- Use translations: Store page content in
app/Metafiles/{lang}/for easy localization - Check database: Use
$this->requireDatabase()in models before database operations - Environment variables: Store sensitive data in
.envfile - Template inheritance: Use
mainTemplate.html.twigfor consistent layout - Error handling: Framework handles 404 errors automatically
- Session management: Sessions are started automatically in
index.php
Resources
Documentation
Example Files
app/Controllers/HomeController.phpapp/Controllers/AuthController.phpapp/Views/pages/home.html.twigroutes.php