Recommend this page to a friend! |
Download .zip |
Info | View files (49) | Download .zip | Reputation | Support forum | Blog | Links |
Last Updated | Ratings | Unique User Downloads | Download Rankings | |||||
2020-05-21 (3 months ago) | Not yet rated by the users | Total: 28 | All time: 9,997 This week: 336 |
Version | License | PHP version | Categories | |||
mezon-service 1.0 | MIT/X Consortium ... | 5 | HTTP, PHP 5, Design Patterns |
Description | Author | |
This package can implement Web services routing requests to action classes. |
Just type
composer require mezon/service
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:
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
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);
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.
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:
<?php
return [
[
"route" => "/user/[s:userName]/profile/articles/[i:articleId]/comments/[i:headCommentId]",
"callback" => "userHeadComment",
"method" => "GET",
"call_type" => "public_call"
]
];
It is quite obvious that not all your endpoints will be public. Some of them should check credentials.
First of all you should understand that framework knows nothing about your registry of users and their permissions and roles. So you have to provide information about that.
Do it by implementing security provider like inthe listing below:
// this is first step
// but we have some more )
class TodoSecurityProvider extends \Mezon\Service\ServiceAuthenticationSecurityProvider{
}
And our code will look like this:
/
* Logic implementation
*/
class TodoLogic extends \Mezon\Service\ServiceBaseLogic
{
/
* First endpoint
*/
public function ping(): string
{
return 'I am alive!';
}
/
* Second route
*/
public function whoAmI(): string
{
return 'I\'am Batman!';
}
}
/
* Service class
*/
class TodoService extends \Mezon\Service\ServiceBase
{
}
/
* Security provider
*/
class TodoSecurityProvider extends \Mezon\Service\ServiceAuthenticationSecurityProvider{}
\Mezon\Service\Service::start(TodoService::class, TodoLogic::class, null, TodoSecurityProvider::class);
And routes must be described like this:
[
{
"route": "/ping/",
"callback": "ping",
"method": "GET",
"call_type": "public_call"
},
{
"route": "/who-am-i/",
"callback": "whoAmI",
"method": "GET"
}
]
You can extend your models with fields wich configuration will be defined in the client's code. For example you have entity 'user' with some custom fields. But you don't know what custom fields it will have. You don't know will it have field 'skype', or field 'notes', or field 'gender' etc. List of these fields may vary from project to project.
To work with such fields you can use \Mezon\Service\CustomFieldsModel
tba
tba
Files |
Files | / | ServiceConsoleTransport |
File | Role | Description |
---|---|---|
ConsoleRequestParams.php | Class | Class source |
ServiceConsoleTransport.php | Class | Class source |
Files | / | ServiceHttpTransport |
File | Role | Description |
---|---|---|
HttpRequestParams.php | Class | Class source |
ServiceHttpTransport.php | Class | Class source |
Files | / | Tests |
File | Role | Description | ||
---|---|---|---|---|
conf (2 files) | ||||
ConsoleRequestParamsUnitTest.php | Class | Class source | ||
CustomFieldsModelUnitTest.php | Class | Class source | ||
DbServiceModelUnitTest.php | Class | Class source | ||
HttpRequestParamsUnitTest.php | Class | Class source | ||
MockParamsFetcher.php | Class | Class source | ||
ServiceAuthenticat...roviderUnitTest.php | Class | Class source | ||
ServiceBaseLogicUnitTest.php | Class | Class source | ||
ServiceBaseLogicUnitTests.php | Class | Class source | ||
ServiceBaseUnitTest.php | Class | Class source | ||
ServiceConsoleTransportUnitTest.php | Class | Class source | ||
ServiceHttpTransportUnitTest.php | Class | Class source | ||
ServiceLogicUnitTest.php | Class | Class source | ||
ServiceLogicUnitTests.php | Class | Class source | ||
ServiceMockSecurityProviderUnitTest.php | Class | Class source | ||
ServiceRestTransportUnitTest.php | Class | Class source | ||
ServiceTests.php | Class | Class source | ||
ServiceTransportUnitTest.php | Class | Class source | ||
ServiceUnitTest.php | Class | Class source | ||
ServiceUnitTests.php | Class | Class source | ||
TestService.php | Class | Class source |
Files | / | Tests | / | conf |
File | Role | Description |
---|---|---|
routes.json | Data | Auxiliary data |
routes.php | Aux. | Auxiliary script |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.