1. Go to this page and download the library: Download sigawa/mvc-core library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
namespace App\Controllers;
use sigawa\mvccore\Request;
use sigawa\mvccore\Response;
use sigawa\mvccore\Controller;
class HomeController extends Controller
{
public function index(Request $request, Response $response)
{
$this->setLayout('main');
return $this->render('home');
}
}
namespace App\Models;
use sigawa\mvccore\db\DbModel;
class User extends DbModel
{
public string $name = '';
public string $email = '';
public static function tableName(): string
{
return 'users';
}
public function attributes(): array
{
return ['name', 'email'];
}
public function rules(): array
{
return [
'name' => [self::RULE_REQUIRED],
'email' => [self::RULE_REQUIRED, self::RULE_EMAIL],
];
}
}
use sigawa\mvccore\db\CRUD;
$crud = new CRUD($databaseConnection);
$data = $crud->getAll('tableName', '*', []);
namespace MyNamspace\Vendor\models;
use sigawa\mvccore\db\DbModel;
class Itinerary extends DbModel
{
public $id;
public $client_id;
public $agent_id;
public $start_date;
public $end_date;
public $total_cost;
public $profit_margin;
public $created_at;
public static function tableName(): string
{
return 'itineraries';
}
public function attributes(): array
{
return [];
}
public function labels(): array
{
return [];
}
public static function getItineraryDetails(int $id)
{
$query = "SELECT i.*, c.name AS client_name, a.name AS agent_name
FROM itineraries i
LEFT JOIN clients c ON i.client_id = c.id
LEFT JOIN agents a ON i.agent_id = a.id
WHERE i.id = :id";
return self::findOneByQuery($query, ['id' => $id]);
}
public static function getAllItineraries(): array
{
$query = "SELECT i.*, c.name AS client_name, a.name AS agent_name
FROM itineraries i
LEFT JOIN clients c ON i.client_id = c.id
LEFT JOIN agents a ON i.agent_id = a.id";
return self::findByQuery($query);
}
public function rules(): array
{
return [
'client_id' => [self::RULE_REQUIRED],
'start_date' => [self::RULE_REQUIRED],
'end_date' => [self::RULE_REQUIRED],
'total_cost' => [self::RULE_REQUIRED, self::RULE_DIGIT],
'profit_margin' => [self::RULE_DIGIT],
];
}
public function save(): bool
{
$this->created_at = date('Y-m-d H:i:s');
return parent::save();
}
}
namespace Sigawa\Trekafrica\controllers;
use sigawa\mvccore\Request;
use sigawa\mvccore\Response;
use sigawa\mvccore\Controller;
use Sigawa\Trekafrica\models\Clients;
class ClientsController extends Controller
{
public function index(Request $request, Response $response)
{
if($request->isGet()){
$this->setLayout('authenticated');
return $this->render('clients', [
'datalist' => Clients::allClients(),
]);
}
else{
return $response->redirect('/');
}
}
public function create(Request $request, Response $response)
{
if ($request->isPost()) {
$file = $_FILES['file_path'] ?? null;
$allowedFileTypes = [
'application/pdf',
'image/jpeg',
'image/jpg',
'image/png'
];
$uploadedFilePath = null;
if ($file && isset($file['name']) && !empty($file['name'])) {
if ($file['error'] !== UPLOAD_ERR_OK) {
$uploadErrors = [
UPLOAD_ERR_INI_SIZE => 'File exceeds the upload_max_filesize directive.',
UPLOAD_ERR_FORM_SIZE => 'File exceeds the MAX_FILE_SIZE directive.',
UPLOAD_ERR_PARTIAL => 'File was only partially uploaded.',
UPLOAD_ERR_NO_FILE => 'No file was uploaded.',
UPLOAD_ERR_NO_TMP_DIR => 'Missing temporary folder.',
UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk.',
UPLOAD_ERR_EXTENSION => 'A PHP extension stopped the file upload.'
];
return $response->json([
'success' => false,
'message' => $uploadErrors[$file['error']] ?? 'Unknown upload error.'
]);
}
// Validate file type using finfo_file
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$fileType = finfo_file($finfo, $file['tmp_name']);
finfo_close($finfo);
if (!in_array($fileType, $allowedFileTypes)) {
return $response->json([
'success' => false,
'message' => 'Invalid file type. Only images or PDFs are allowed.'
]);
}
// Define upload directory securely
$uploadDir = __DIR__ . '/../uploads/files/'; //this can be improved to take care of directory traversal
// unrelated example : $file = basename($request->getBody()['file']); // Prevent basic directory traversal
if (!is_dir($uploadDir) && !mkdir($uploadDir, 0755, true) && !is_dir($uploadDir)) {
return $response->json([
'success' => false,
'message' => 'Failed to create upload directory.'
]);
}
// Generate a unique filename
$uniqueFilename = time() . '-' . md5(uniqid()) . '-' . basename($file['name']);
$uploadedFilePath = $uploadDir . '/' . $uniqueFilename;
if (!move_uploaded_file($file['tmp_name'], $uploadedFilePath)) {
return $response->json([
'success' => false,
'message' => 'Failed to upload file. Ensure file permissions are set correctly.'
]);
}
}
// Save client details to database
$client = new Clients();
$client->loadData($request->getBody());
$client->file_path = $uploadedFilePath ? basename($uploadedFilePath) : null;
try {
if ($client->validate() && $client->save()) {
return $response->json([
'success' => true,
'message' => 'Client created successfully!',
'data' => Clients::allClients(),
]);
} else {
return $response->json([
'errors' => $client->getErrorMessages(),
]);
}
} catch (\Exception $th) {
return $response->json([
'message' => 'An error occurred while saving the client.',
'errors' => $th->getMessage(),
]);
}
}
return $response->json([
'success' => false,
'error' => 'Invalid request method',
]);
}
public function update(Request $request, Response $response, $id)
{
if ($request->isPost()) {
$id ??= $request->getParam('id');
if (!$id) {
return $response->json([
'success' => false,
'message' => 'Client ID is if (!$request->isDelete()) {
return $response->json([
'success' => false,
'message' => 'Invalid request method!',
]);
}
$id ??= $request->getParam('id');
$client = Clients::getClientById($id);
if (empty($client)) {
$response->statusCode(404);
return $response->json([
'success' => false,
'message' => 'client not found!',
]);
}
if (Clients::softDeleteClientById($id)) {
return $response->json([
'success' => true,
'message' => 'client deleted successfully!',
'data' => Clients::allClients(),
]);
}
return $response->json([
'success' => false,
'message' => 'Failed to delete client!',
]);
}
}
bash
php mcconsole create:project
bash
php mcconsole serve
bash
php mcconsole make:controller ControllerName
bash
php mcconsole make:model ModelName
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.