filarichard/mvc_micro_framework

Simple MVC Micro Framework

0.4 2019-08-03 07:36 UTC

This package is not auto-updated.

Last update: 2024-06-02 07:36:19 UTC


README

Simple MVC Micro Framework

mvc_micro_framework is simple and lightweight PHP micro framework, that allows you easy and fast build of MVC applications. It's simple and extensible!

Minimum requires

mvc_micro_framework requires PHP 7.1 or grater.

Installation

  1. Download

With Composer:
composer require filarichard/mvc_micro_framework

Without Composer:
Download files and extract them directly to web directory.

  1. Create App and call run function
$application = new \App\App();
$application->run();
  1. Create config.json file in /config directory
{
  "routes": {
    "/third_parties_components": {
      "controller": "Controllers\\ThirdPartiesController",
      "action": "tpc"
    }
  },
  "modules": [
    "mvc"
  ],
  "diContainer": "config/diContainer.php"
}

Possible config values are:

  • routes - list of all routes used in the app, that doesn't use modules.
  • modules - list of all modules used in the app.
  • router - path to PHP fith configured router.
  • diContainer - path to PHP fith configured DI Container.
  • request - path to PHP fith configured HTTP Request.

Routing

For fully working routing, use followed directory:

  • config
  • src
    • config
      • All configuration files for modules
      • modelname.config.json
    • controllers
      • All controllers
      • If defined, use Modul name
        • Modul Controllers
    • models
    • views

For Router setup, you can use on of the following choices:

  1. Define routes in config file

All routes, that aren't included in modules, create in config.json file.
All routes, that are in modules, create in modulname.config.json files.

Structure of route definition:

"/nameOfRoute": {
    "controller": "Controllers\\NameOfController",
    "action": "nameofFunction"
  }
  1. Define own router with all routes
$router = new \MVC\Router();

$router->addRoute(new \MVC\Route("/nameOfRoute", \Controllers\NameOfController::class, "nameOfFunction"));

return $router;

Template Engine

mvc_micro_framework have built-in Template Engine. Just follow these few steps:

  1. Set template
$this->setView();
  1. Assign variables
$this->view->assignValue("nameOfVariable", $value);

$this->view->assignValue("title1", $titleValue);
$this->view->assignValue("subtitle1", $subtitleValue);
$this->view->assignValue("content1", $contentValue);
  1. Use those variables in HTML code
<h1>{title1}</h1>
<h2>{subtitle1}</h2>
<p>
  {content1}
</p>
  1. Render View
$this->view->render("pathToFile.html");

Function render can take second parameter that define if path is full or relative to /src/views.

Event Dispatcher

With following steps, you can use our Event Dispatcher:

  1. Create new Dispatcher
$this->eventDispatcher = new Dispatcher();
  1. Create new Event
$this->eventName = new Event("eventName");
  1. Attach him to Listener
$this->eventDispatcher->attach($this->eventName, array($this, 'listenerName'));

Note: Listener can be any Callable object.

  1. Dispatch Event
$this->eventDispatcher->dispatch($this->eventName);

You can also remove Event

$this->eventDispatcher->detach($this->eventName, array($this, 'listenerName'));

or stop event propagation

$this->eventName->setPropagationStopped(true);

Third Parties Components

mvc_micro_framework support following components from third parties:

  • HTTP Foundation/Symfony
  • Eloquent (ORM)/Laravel
  • Pimple (DI Container)
  • Monolog (Logger)

License

Flight is released under the MIT license.