PHP Classes

How to Create Simple PHP Microservices using Mezon Framework - Mezon Service package blog

Recommend this page to a friend!
  All package blogs All package blogs   Mezon Service Mezon Service   Blog Mezon Service package blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Create Simple ...  
  Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  

Author:

Viewers: 358

Last month viewers: 2

Package: Mezon Service

Mezon is a PHP framework that you can use to develop PHP applications using the MVC design pattern.

The Mezon framework also allows developers to create applications based on microservices. Microservices can be the base to quickly create complex applications based on service components that implement specific functions necessary to make those applications.

Read this article to learn how to create simple microservices using Mezon Framework.




Loaded Article

In this article you will learn:

1. Configuring the Web Server

2. Install the Mezon Framework Packages

3. Create the Service Business Logic

4. Create the Server Web Script

5. Download and Install the Mezon Service Package Using PHP Composer

6. Conclusion


1. Configuring the Web Server

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):

# show php errors
#php_flag display_startup_errors on
#php_flag display_errors on
#php_flag html_errors on

# 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]

2. Install the Mezon Framework Packages

Next run:

composer require mezon/service
composer require mezon/service-logic

3. Create the Service Business Logic

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!';
    }
}

4. Create the Server Web Script

Then we need to assemble 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();

5. Download and Install the Mezon Service Package Using PHP Composer

You can download or install the Mezon Service package using PHP Composer tool by going to this download page to get the package code. That page also contains instructions on how to install package using PHP Composer from the PHP Classes site.

6. Conclusion

Pretty simple yeah? )

Do you have any proposals how to improve the Framework? Then use github to submit your feature request )




You need to be a registered user or login to post a comment

1,616,107 PHP developers registered to the PHP Classes site.
Be One of Us!

Login Immediately with your account on:



Comments:

No comments were submitted yet.




  Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  
  All package blogs All package blogs   Mezon Service Mezon Service   Blog Mezon Service package blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Create Simple ...