DEV Community

Cover image for How to create simple microservice using Mezon Framework
alexdodonov
alexdodonov

Posted on • Updated on

How to create simple microservice using Mezon Framework

Hi! In this article I shall show you how to create simple micro service with Mezon Framework

To be honest it is quite simple. First of all you need to create htaccess (if your are using Apache, or just skip this step otherwise):

# use mod_rewrite for pretty URL support
RewriteEngine on

RewriteRule ^(.*).html$ $1.html [L]
RewriteRule ^(.*).png$ $1.png [L]

RewriteRule ^cron/(.*)$ cron/$1 [L]
RewriteRule ^include/(.*)$ include/$1 [L]
RewriteRule ^Res/(.*)$ Res/$1 [L]
RewriteRule ^vendor/(.*)$ vendor/$1 [L]

RewriteRule ^data/files/(.*)$ data/files/$1 [L]

RewriteRule ^Res/Images/(.*)$ Res/Images/$1 [L]
RewriteRule ^([a-z0-9A-Z_\/\.\-\@%\ :,]+)/?(.*)$ index.php?r=$1&%{QUERY_STRING} [L]
RewriteRule ^/?(.*)$ index.php?r=index&%{QUERY_STRING} [L]
Enter fullscreen mode Exit fullscreen mode

Next run:

composer require mezon/service
composer require mezon/service-logic
Enter fullscreen mode Exit fullscreen mode

Then create simple class for business logic implementation:

<?php
namespace Lambda;

use Mezon\Service\ServiceBaseLogic;

class Logic extends ServiceBaseLogic
{

    public function helloWorld(): string
    {
        return 'Hello world!';
    }
}
Enter fullscreen mode Exit fullscreen mode

Then we need to asseble our service in index.php:

// here we use MockProvider for no authentication or authorization
$securityProvider = new MockProvider();

// here we decide to use REST transport
$serviceTransport = new ServiceRestTransport($securityProvider);

// here we create object of the business logic
$serviceLogic = new Logic($serviceTransport->getParamsFetcher(), $serviceTransport->getSecurityProvider());

// setup transport and set that we need to use `helloWorld` method for router '/hello-world/' and GET method
$serviceTransport->setServiceLogic($serviceLogic);
$serviceTransport->getRouter()->addGetRoute('/hello-world/', $serviceLogic, 'helloWorld');

// create service object ...
$service = new ServiceBase($serviceTransport);

// ... and then simply run service
$service->run();
Enter fullscreen mode Exit fullscreen mode

Pretty simple yeah? )

Do you have any proposals how to improve Mezon Framework? Then use GitHub to submit your feature request.

Top comments (0)