2017 © Pedro Peláez
 

library mikro-php

PHP 5.6 MVC Micro Framework

image

emilianozublena/mikro-php

PHP 5.6 MVC Micro Framework

  • Monday, October 16, 2017
  • by emilianozublena
  • Repository
  • 2 Watchers
  • 3 Stars
  • 3 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

mikro-php

Installation

The easiest way to use Mikro-PHP is by just cloning/forking this repo, (*1)

It's also available through Composer., (*2)

You can edit your composer.json file or just hit this command in your terminal, (*3)

composer require emilianozublena/mikro-php

Keep in mind that installing it via composer is for production use, and not for testing. Read the introduction to have more details about this., (*4)

Introduction

Mikro works as a Micro Framework, it uses but its not limited to the following packages:, (*5)

Eloquent ORM ("illuminate/database": "^5.4"), (*6)

Symfony's Http Foundation ("symfony/http-foundation": "~3.2"), (*7)

Zend Framework's Service Manager ("zendframework/zend-servicemanager"), (*8)

Nikic Fast Route ("nikic/fast-route": "^1.2"), (*9)

It features a Kernel object that, altogether with the IoC implementation in Service Manager, handles the use of every library/service this micro framework has., (*10)

This framework is meant to work under MVC architecture pattern., (*11)

If you'd like to use in a different path (or installing it via composer instead of cloning/forking this repo), you should pay attention to the .htaccess file in the root. This .htaccess redirects ALL traffic to /src where the framework actually is. From there, the .htaccess will use the index.php as the single entry point, (*12)

Basic Config

The main and basic configuration for your app is under config/app.php. Here you need to define the access to your database and also the desired baseUrl. This is importante since its used to populate url's inside views., (*13)

return [
    'baseUrl' => 'http://localhost/mikro',
    'databases' => [
        'config' => [
            'mysql' => [
                'driver'      => 'mysql',
                'host'        => '127.0.0.1',
                'port'        => '3306',
                'database'    => 'mikro',
                'username'    => 'root',
                'password'    => 'root',
                'unix_socket' => '',
                'charset'     => 'utf8',
                'collation'   => 'utf8_unicode_ci',
                'prefix'      => '',
                'strict'      => true,
                'engine'      => null,
            ],
        ]
    ]
];

Service Manager / Container / IoC implementation

This fw works with Zend's Service Manager. If you need to register a new service/library you can do that in config/services.php For further use, refer to Zend's Service Manager documentation, (*14)

Kernel

This framework gets initialized through its Kernel object. This object handles every request and delivers it to whom it corresponds (may it be a controller, or a closure) It also is responsible for processing routes, asking the container for the core libraries., (*15)

$container = (require 'config/services.php');
$kernel = $container->get('kernel');
$kernel->initCore();

require 'config/routes.php';

$kernel->processRoutes();

$kernel->run();

The run() method should always be the last thing you call., (*16)

Response

All of the Request's and Response's are handled through HttpFoundation. This fw comes with a small factory for creating specific responses (html, json, empty, etc) Comes very handy when you need different responses inside one controller (ie ajax calls and traditional requests) If you need it, ask for it to the container and then you only have a create() method that returns the desired Response, (*17)

$container = (require 'config/services.php');
$container->get('response');
$exampleResponse = $esponse->create(
    JsonResponse::class,
    ['message' => 'OK!'],
    Response::HTTP_OK
);

Routing

Routes are defined within /config/routes.php. Every route is defined through the Kernel object (previously instantiated by the container inside our entry point) Methods for defining routes are self explanatory and each one correspond to the main Http Methods. Router class just handles the adding of routes to a priv property. After that, the kernel may use this router altogether with FastRoute to proper implement the routes, (*18)

$kernel->post('books', [
    '\Mikro\Http\Controllers\Books',
    'store'
]);
$kernel->get('books/{id:\d+}/update', [
    '\Mikro\Http\Controllers\Books',
    'update'
]);
$kernel->put('books/{id:\d+}', [
    '\Mikro\Http\Controllers\Books',
    'put'
]);
$kernel->patch('books/{id:\d+}', [
    '\Mikro\Http\Controllers\Books',
    'patch'
]);
$kernel->match(['PUT', 'PATCH'], 'books/{id:\d+}', [
    '\Mikro\Http\Controllers\Books',
    'patch'
]);

Controllers

Routes define a pattern to be matched and also a handler. The handler is a callable, meaning it can be a closure or a class As an MVC fw, Mikro comes with a small implementation of Controllers If you need to create a new controller, you just need to extend its base Controller Every controller needs to return ALWAYS a valid HttpFoundation Response object. Response objects may be accessed through the container or the custom Response factory, (*19)

container->get('response');

        return $response->create(
           JsonResponse::class,
           [
               'message' => 'Some message'
           ],
           Response::HTTP_OK
       );
    }
}
```

## Views
Controllers may return a Json response (if you're handling requests through ajax or putting together a RESTful API this is quite helpful)
But it also may return plain HTML.
For this, we have the View object, this object allows to set up a plain php file, and gives us the ability to populate it with our custom variables and render it when and if we want
The view also comes with a helpful function "getResponse" that aids to get a valid Response object (HtmlResponse) from the view created
```
container->get('view');

        return $view->file('book')->render([
            'hola' => 'chau'
        ])->getResponse();
    }
}
```
And views are just plain php files, every view (and the whole presentation layer css/js/html) are inside resources folder
```

Mikro Php Books

The Versions

16/10 2017

dev-master

9999999-dev

PHP 5.6 MVC Micro Framework

  Sources   Download

The Requires

 

The Development Requires

by Emiliano Zublena

16/10 2017

v0.0.1

0.0.1.0

PHP 5.6 MVC Micro Framework

  Sources   Download

The Requires

 

The Development Requires

by Emiliano Zublena