Open Source-extensions for yii from 2GIS teams
Last time we talked about layered architecture in the yii framework, and now we want to share the code. In our work, we actively use open source solutions and therefore decided to share our own developments that may be useful to the community. Today these are extensions for probably the best yii PHP framework in the world :
- DGSphinxSearch
- AMQP (RabbitMQ)
- MQLogRouter
- DGPinbaLogRoute
- DGApiClient
DGSphinxSearch
Sphinx is a high-performance full-text search engine that you probably know well. The extension allows you to implement both a procedural and an object-oriented approach to generating queries and working with data. The extension is fully integrated into yii and allows you to monitor your work using standard framework tools. To work on servers where C ++ is not installed - an extension for PHP, the mode of operation through the script library is supported.
For example, try to find no more than 30 entries among users named Vasily:
$userlist = Yii::App()->sphinx->select('name')->from(‘users’)->where(‘Василий’)->limit(0, 30)->search();
It became significantly more compact than when using the original library for Sphinx.
Details are on the extension page on the yii website: www.yiiframework.com/extension/dgsphinxsearch
AMQP
RabbitMQ is a fast messaging server written in Erlang's fault-tolerant language.
The extension simplifies working not only with RabbitMQ, but also with any MQ-servers that support the AMQP protocol, and allows you to send and receive messages from the server, and also supports debug mode when there is no direct connection to the server.
To send a message to the "outbox" exchanger, do:
Yii::App()->rabbitMQ->exchange('outbox')->publish('Привет!', '');
To receive a message from the inbox queue, which is subscribed to the outbox exchanger, we do:
Yii::App()->rabbitMQ->queue('inbox')->get();
Extension page: www.yiiframework.com/extension/amqp
MQLogRouter
Yii has a very convenient logging and profiling system built in. In the basic delivery, it allows you to save logs to the file system, to the database or output to the browser. We have added a special LogRoute, which directs logs to the MQ server.
The convenience of the solution is manifested when there is a need to analyze the interaction of several separate applications that interact through the REST or SOAP interface and maintain logs on different servers.
www.yiiframework.com/extension/cmqlogroute - the connection instruction is quite simple and comes down to setting up the application config.
DGPinbaLogRoute
Pinba is a server monitoring and code profiling tool. Due to the fact that packets are sent to the server asynchronously, the transmission does not affect the performance of your application. Pinba itself collects summary statistics on the execution of each script and allows you to use it in real time.
To send logs to Pinba, just connect DGPinbaLogRoute as a yii-logger and call the usual ones for yii:
Yii::beginProfile();
…..;
Yii::endProfile();
More details here: www.yiiframework.com/extension/dgpinbalogroute
The next step we see in the visualization of the collected statistics and plan to share the corresponding module.
DGApiClient
APIClient is a PHP wrapper for our 2GIS API product . The product itself allows applications to receive 2GIS reference data from more than 1 million organizations and create local search services on their basis or use them on existing city portals, thematic sites and other projects.
The extension facilitates integration tasks and allows the developer to abstract from the implementation of REST requests and work with the API directly from the PHP code. Let's look at an example of how to use it.
For example, here we get a list of 10 geo objects 100 meters from point 82.901886,54.991984 in xml format:
$list = Yii::App()->apiClient->geoSearch(array(
'q' => '82.901886,54.991984',
'radius' => 100,
'limit' => 10,
), 'xml');
And now we are looking for 10 companies for “beer” in the vicinity of Arbat in Moscow:
$list = Yii::App()->apiClient->search(array(
'where' => 'Москва Арбат',
'what' => 'пиво',
'limit' => 10,
), 'xml');
If we want to receive the answer in the form of objects, it is enough to replace 'xml' with 'object'.
The full list of methods is specified in the API documentation on api.2gis.ru
* * * The
full list of extensions for yii that our development teams post can be found on the framework website using the 2GIS tag: www.yiiframework.com/extensions/?tag=2GIS
Of course, this is not all that we would like to share. In the future, we plan to lay out an extension for ActiveRecord, which can work with several database servers at the same time, and a task manager that allows you to run various tasks, controlling their sequence and number. If the described applications prove to be in demand, we will continue to make our small contribution to the large and important Open Source movement.