pfcode/megumin-framework

A simple MVC PHP framework

1.4.0 2023-03-02 20:13 UTC

This package is auto-updated.

Last update: 2024-04-30 00:33:29 UTC


README

A simple MVC framework written in PHP. Name was inspired by Megumin from Kono Subarashii Sekai ni Shukufuku wo! anime :)

Name inspired by Megumin from Kono Subarashii Sekai ni Shukufuku wo!

Overview

A main purpose of this project is to easily start a new project codebase using set of base classes and tools that are intended to make your work faster and keep the code clean.

Despite of mainstream PHP frameworks, this one provides only the most common components, such as:

  • Dispatcher - performs routing to the controllers (with friendly URLs support)
  • ModelFactory - manipulates Model storage by queries (SQL databases)
  • Model - represents some data in project, such as UserModel, PostModel or so..
  • Controller - handles data from inputs, manipulates on Models and presents results to user
  • View - presents data from isolated variable scope on page

There are also basic implementations of some classes:

  • MySQLModelFactory - ModelFactory that handles custom MySQL queries to retreive and manipulate data
  • ScopeDebugView - View that displays informations about controller - can be used for debugging

Installation

You only need to add new dependency to your composer.json file:

  "require": {
    "pfcode/megumin-framework": ">=1.0.0"
  }, 

...and update your project (Composer should automatically download latest package):

composer update

Quick Start

There's a sample index.php in examples directory that uses MeguminFramework and performs sample routing. Here is the basic part of it (assuming that sample controllers have been defined and autoloaded)

<?php

// Use MeguminFramework namespace to gain direct access to its components
use pfcode\MeguminFramework\Dispatcher;
use pfcode\MeguminFramework\View;

// Use PSR-4 autoloader generated by composer
require __DIR__ . "/../vendor/autoload.php";

// Set global View parameters (page title doesn't have to be honored by chosen View)
View::setPageTitlePrefix("Website Title");
View::setPageTitleSeparator(" : ");

// Specify namespace that your controllers belongs to 
Dispatcher::setControllersNamespace(__NAMESPACE__);

// Set controllers that should be called when routing fails
Dispatcher::setFallbackControllers(DashboardController::class, NotFoundController::class);

// Specify routing table for the dispatcher 
// (you can omit this call when you don't want to use friendly URLs)
Dispatcher::setUrlPatterns(array(
    "/test/{id}" => TestController::class,
    "/test" => TestController::class,
    "/" => DashboardController::class
));

// Perform routing and execute proper controller
Dispatcher::explosion();