PHPackages                             mezon/service - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. mezon/service

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

mezon/service
=============

Small service script

1.6.2(3y ago)65.9k2MITPHPPHP &gt;=7.2.0CI failing

Since Feb 18Pushed 3y ago2 watchersCompare

[ Source](https://github.com/alexdodonov/mezon-service)[ Packagist](https://packagist.org/packages/mezon/service)[ Docs](https://github.com/alexdodonov/mezon-service)[ RSS](/packages/mezon-service/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (15)Versions (58)Used By (2)

Set of classes for creating microservices
=========================================

[](#set-of-classes-for-creating-microservices)

[![Build Status](https://camo.githubusercontent.com/917316596c86ef488eafe87199327dc54f1a3fbb024a33eacc61739b7009ffd6/68747470733a2f2f7472617669732d63692e636f6d2f616c6578646f646f6e6f762f6d657a6f6e2d736572766963652e7376673f6272616e63683d6d6173746572)](https://travis-ci.com/alexdodonov/mezon-service) [![codecov](https://camo.githubusercontent.com/182daf902154532502870472971f1f5151af9d5d18bac34a0b1598163068aa6a/68747470733a2f2f636f6465636f762e696f2f67682f616c6578646f646f6e6f762f6d657a6f6e2d736572766963652f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/alexdodonov/mezon-service) [![Scrutinizer Code Quality](https://camo.githubusercontent.com/512360e5641f5b054d1db80d734985744c32d365324a6c53f4231a12088f75d7/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f616c6578646f646f6e6f762f6d657a6f6e2d736572766963652f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/alexdodonov/mezon-service/?branch=master)

Installation
------------

[](#installation)

Just type

```
composer require mezon/service

```

First step
----------

[](#first-step)

This is our first service.

```
class TodoService extends \Mezon\Service\ServiceBase implements \Mezon\Service\ServiceBaseLogicInterface
{

    /**
     * First endpoint
     */
    public function actionPing()
    {
        return ('I am alive!');
    }
}

\Mezon\Service\Service::start(TodoService::class);
```

Here:

- \\Mezon\\Service\\ServiceBase - base class for simple services
- \\Mezon\\Service\\ServiceBaseLogicInterface - class must implement this interface to provide actions in format 'action'
- \\Mezon\\Service\\Service::start(TodoService::class) - launching our service

Then you can access your first endpoint in a way like this:

```
http://localhost/?r=ping

```

Here is 'ping' is part afer 'action' in the 'actionPing'.

You can create longer endpoints:

```
public function actionLongEndpoint()
{
    return ('long endpoint');
}
```

And it will be available via this URL:

```
http://localhost/?r=long-enpoint

```

Introducing logic
-----------------

[](#introducing-logic)

In the concept of Mezon framework service class is only a container for logic, model, transport, security providerrs and so on.

So we may want to fetch all logic to the separate class. Then we shall do it in this way:

```
/**
 * Logic implementation
 *
 * @author Dodonov A.A.
 */
class TodoLogic extends \Mezon\Service\ServiceBaseLogic
{

    /**
     * First endpoint
     */
    public function actionPing()
    {
        return ('I am alive!');
    }
}
```

And then we shall modify service class like this:

```
/**
 * Service class
 *
 * @author Dodonov A.A.
 */
class TodoService extends \Mezon\Service\ServiceBase
{
}

\Mezon\Service\Service::start(TodoService::class, TodoLogic::class);
```

But as you see - we have empty service class with only base functionality. So we can completely remove it and change our code:

```
\Mezon\Service\Service::start(\Mezon\Service\ServiceBase::class, TodoLogic::class);
```

Multyple logic classes
----------------------

[](#multyple-logic-classes)

But you can split your functionality into several classes like in the next example:

```
\Mezon\Service\Service::start(\Mezon\Service\ServiceBase::class, [
    TodoSystemLogic::class,
    TodoReadLogic::class,
    TodoWriteLogic::class
]);
```

Here you just pass several classes when creating service.

Complex routing
---------------

[](#complex-routing)

You can create more complex routes. To do this you have to setup them in the routes.json config of your service. The content of this file must looks like this:

```
[
	{
		"route": "/user/[s:userName]/profile/articles/[i:articleId]/comments/[i:headCommentId]",
		"callback": "userHeadComment",
		"method": "GET",
		"call_type": "public_call"
	}
]
```

And we need logic class for this route:

```
class CommentLogic extends \Mezon\Service\ServiceBaseLogic
{

    /**
     * Our endpoint
     */
    public function userHeadComment(string $route, array $params)
    {
        return [
            // some data here
        ];
    }
}
```

In this example the method userHeadComment handles routing processing. And this method receives array with routes parameters. Something like that:

```
[
    'userName' => 'name of the user',
    'articleId' => 'id of the article',
    'headCommentId' => 'comment's id'
]
```

But you can also store all route configs in PHP files like this:

```
