PHP code example of geekcow / fony-core

1. Go to this page and download the library: Download geekcow/fony-core 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/ */

    

geekcow / fony-core example snippets



e('MY_DOC_ROOT', __DIR__);
define('MY_ASSET_ROOT', __DIR__);

use Geekcow\FonyCore\FonyApi;
use {PROJECTNAMESPACE}\router;

// Requests from the same server don't have a HTTP_ORIGIN header
if (!array_key_exists('HTTP_ORIGIN', $_SERVER)) {
  $_SERVER['HTTP_ORIGIN'] = $_SERVER['SERVER_NAME'];
}

try {
  $router = new Router('{PROJECTCONFIGFILE}');
  $API = new FonyApi($_REQUEST['request'], $router);
  // if you want to have the origin set:
  // $API = new FonyApi($_REQUEST['request'], $router, $_SERVER['HTTP_ORIGIN']);
  echo $API->processAPI();
} catch (\Exception $e) {
  echo json_encode(Array('error' => $e->getMessage()));
}




namespace {PROJECTNAMESPACE};

use Geekcow\FonyCore\Controller\GenericController;
use Geekcow\FonyCore\FonyRouter;
use Geekcow\FonyCore\Utils\SessionUtils;
use {PROJECTNAMESPACE}\model\TestModel;

class Router extends FonyRouter
{
    public function __construct($config_file)
    {
        parent::__construct($config_file);
    }
    
    public function prestageEndpoints($endpoint, $request){
        parent::prestageEndpoints($endpoint, $request);

        switch ($this->endpoint) {
            case 'generic-controller-endpoint':
                $this->action = new GenericController();
                $this->action->setRequest($request);
                $this->action->setModel(new TestModel());
                $this->action->setFilter(['fields', 'that', 'you', 'do not', 'want', 'to', 'show']);
                $this->setAllowedRoles(Allow::CUSTOMROLE());
                break;
            case 'generic-actionable-controller-endpoint':
                $this->action = new GenericActionController();
                $this->action->setRequest($request);
                $this->action->setModel(new TestModel());
                $this->action->setFilter(['fields', 'that', 'you', 'do not', 'want', 'to', 'show']);
                $this->setAllowedRoles(Allow::CUSTOMROLE());
                break;
        }
    }
    
    /**
     * Shows a welcome message
     *
     * @return string
     *
     */
    //WELCOME MESSAGE
    public function welcome()
    {
        if ($this->method == 'GET') {
            return "WELCOME TO FONY PHP";
        } else {
            return "Invalid Method";
        }
    }

    /**
     * Executes only a POST operation.
     *
     * @return JSON Authenticated response with token
     *
     */
    public function genericControllerEndpoint()
    {
        switch ($this->method) {
            case 'POST':
                $this->action->doPost($this->args, $this->verb);
                $this->response_code = $this->action->response['code'];
                return $this->action->response;
                break;
            case 'OPTIONS':
                exit(0);
                break;
            default:
                $this->response_code = 405;
                return "Invalid method";
                break;
        }
    }

    /**
     * Executes an endpoint using the default behavior.
     *
     * @return JSON response
     *
     */
    public function genericActionableControllerEndpoint()
    {
        return $this->executesCall();
    }
}


    $sessionInstance = SessionUtils::getInstance(new CustomAuthenticatorClass());