DEV Community

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

Posted on • Edited on

4 1

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.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Billboard image

Try REST API Generation for Snowflake

DevOps for Private APIs. Automate the building, securing, and documenting of internal/private REST APIs with built-in enterprise security on bare-metal, VMs, or containers.

  • Auto-generated live APIs mapped from Snowflake database schema
  • Interactive Swagger API documentation
  • Scripting engine to customize your API
  • Built-in role-based access control

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay