PHP code example of esyoilgmbh / annotations

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

    

esyoilgmbh / annotations example snippets




* @AnnotationTarget(AnnotatedElementType::METHOD)
 */
class Route extends Annotation {}



 Controller {
	/**
	 * @Route('/')
	 */
	function home() {}
	
	/**
	 * @Route('/login')
	 */
	function login() {}
}



 RequestRouter {
	function process($path) {
		$reflect = new AnnotatedReflectionClass('Controller');
        $obj = $reflect->newInstance();
        foreach ($reflect->getMethods() as $method) {
            $route = $method->getAnnotation('Route');
            if ($route->value() == $path) {
                $method->invoke($obj);
                break;
            }
        }
	}
}



* @AnnotationTarget(AnnotatedElementType::METHOD)
 */
class Route extends Annotation {
	public $path;
	public $method;
	
	public function __construct($path = null, $method = null) {
        $this->path = $path;
        $this->method = $method;
    }

    public function path() {
        return $this->path;
    }

    public function method() {
        return $this->method;
    }
}



 Controller {
	/**
	 * @Route('/', 'GET')
	 */
	function home() {}
	
	/**
	 * @Route('/login', 'GET')
	 */
	function login() {}
	
	/**
	 * @Route('/login/auth', 'POST')
	 */
	function loginAuth() {}
}



 Controller {
	/**
	 * This route ignores the method parameter
	 * @Route('/foo')
	 */
	function home() {}
	
	/**
	 * This route ignores the path parameter so we have to specify that the
	 * parameter we are passing is meant to be used as the method value.
	 * This is obviously pointless, but for the sake of example...
	 * @Route(method='GET')
	 */
	function bar() {}
}