PHP code example of zemd / micro

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

    

zemd / micro example snippets


class FeatureHandler implements HandlerInterface {
  public function handle(CommandInterface $command, $context) {
    // do stuff
    return "response";
  }
}

class FeatureMetaInformation implements HandlerMetaInterface {
    public function getMethods() {
      return ['GET'];
    }
  
    // Unique string that match handler name, it should be possible to be constructed from request string
    public function getEndpoint() {
      return '/users/search';
    }
    
    public function getCommandGuesser() {
      return false; // If false returns getCommandClass() method will be used.
    }
    
    public function getCommandClass() {
      return 'my.command.as.service'; // or FeatureCommand::class
    }
  
    public function getDescription() {
      return 'Some cool feature';
    }
}

  class FeatureCommand implements CommandInterface {
    /**
     * @RequestParam("mobile", pattern="\+\d{8,13}", ;
    
    /**
     * @RequestParam("country_code", protected $lang;
    
    // ... getters/setters/serialize/unserialize
  }

class TestController {
  public function handleAction($param1, $param2, $param3, Request $request) {
    $checkpoint = "$param1/$param2/$param3";
    
    $handlerMetaManager = new HandlerMetaManager();
    $handlerMeta = $handlerMetaManager->getMeta($checkpoint);
    
    $commandBuilder = new CommandBuilder();
    // in case command guesser returns service id, container must be passed to builder
    $commandBuilder->setContainer($container);
    
    $command = $commandBuilder->setRequest($request)
                  ->setMeta($handlerMeta)
                  ->build();
                  
    $handlerDispatcher = new CommandDispatcher($config);
    $result = $handlerDispatcher->dispatch($command);
    
    // now we have $result with response
    // we can serialize it in json/xml/...
    
    return $result;
  }
}