PHP code example of yokel / yapi

1. Go to this page and download the library: Download yokel/yapi library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yokel / yapi example snippets



# /local/php_interface/init.php
\Yapi\Yapi::getInstance()->run($routes, $basePath, $beforeAction);


// GET http://yoursite.name/yapi/test/result.html
\Yapi\Yapi::getInstance()->run([
    'test' => '\YourNameSpace\YourController'
]);


// GET http://yoursite.name/my_api/test/result.html
\Yapi\Yapi::getInstance()->run(
    [
      'test' => '\YourNameSpace\YourController'
    ],
    '/my_api/'
);


// GET http://yoursite.name/my_api/test/result.json
\Yapi\Yapi::getInstance()->run(
    [
      'test' => '\YourNameSpace\YourController'
    ],
    '/my_api/',
    function() {
        // do some action
    }
);


# Export/YapiController.php
interface YapiController {

    /**
     * Возвращает кастомный обработчки результата (для разных форматов)
     * @param string $format
     * @return mixed
     */
    public function getResultHandler(string $format);

}


namespace YourNameSpace;

use Yapi\Export\YapiController;

class Test implements YapiController {

    public function getResultHandler(string $format) {
        if ($format === 'custom') {
            // Для обработки результатов в формате custom будет использован класс \YourNameSpace\CustomResultHandler
            return '\YourNameSpace\CustomResultHandler';
        } else {
            return null;
        }
    }
    
    public function result($params) {
        return 'some result as text';
    }
    
}


namespace YourNameSpace;

use Yapi\Export\YapiResultHandler;

class CustomResultHandler implements YapiResultHandler {
    
    /**
     * Приведём результат в верхний регистр 
     * @param $result - результат, полученный от контроллера
     */
    public function handle($result) {
        echo strtoupper($result);
    }
    
}