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.

Heroku

Deliver your unique apps, your own way.

Heroku tackles the toil — patching and upgrading, 24/7 ops and security, build systems, failovers, and more. Stay focused on building great data-driven applications.

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

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay