dev-master
9999999-devThrift server application using Yii2
BSD-3-Clause
The Requires
by Rangel Reale
yii2 thrift
Thrift server application using Yii2
Thrift extension for Yii2, (*1)
The preferred way to install this extension is through composer., (*2)
Either run, (*3)
php composer.phar require --prefer-dist RangelReale/yii2-nithrift "*"
or add, (*4)
"RangelReale/yii2-nithrift": "*"
The Thrift base classes must be accessible someway. I put the thrift/lib/php/lib path in my application root, and add this to composer.json:, (*5)
"autoload": { "psr-0": { "Thrift": "lib" } }
Create a directory named generated
in your application root.
Put your .thrift files into it, and generate the php wrapper on the
default gen-php path, using command below., (*6)
thrift --gen php:server path/to/the/thrift/file
The files should be like this:, (*7)
-- ROOT -- config -- controllers -- generated file1.thrift file2.thrift -- gen-php -- file1 -- file2 ...
In the component configuration add the thrift
component, with the
Thrift definitions., (*8)
return [ 'component' => [ 'thrift' => [ 'class' => 'RangelReale\nithrift\Thrift', 'definitions' => [ 'shared', 'tutorial', ], ] ] ]
Implement your Thrift handlers in separate class files, following the
thrift documentation. I recommend using a services
directory on the application
root. There is no need to override or implement any other interface., (*9)
Your controller should extend \RangelReale\nithrift\Controller
. You can use
the custom \RangelReale\nithrift\Action
class to implement the services., (*10)
class ApiController extends \RangelReale\nithrift\Controller { public function actions() { return [ 'calculator' => [ 'class' => 'RangelReale\nithrift\Action', 'handler' => 'app\services\CalculatorHandler', 'processor' => 'tutorial\CalculatorProcessor', ] ]; } }
If you prefer, you can implement an inline action, and either return a
\RangelReale\nithrift\Response
object, or a processor object directly., (*11)
public function actionCalculator() { $handler = new \app\services\CalculatorHandler(); $processor = new \tutorial\CalculatorProcessor($handler); return new ThriftResponse($processor); // could be `return $processor;` }
Rangel Reale (rangelspam@gmail.com), (*12)
Thrift server application using Yii2
BSD-3-Clause
yii2 thrift