Route your application's requests

Route is a dispatcher micro-framework designed for manage requests with flexibility and simplicity.

FrontController

This is a basic front-controller class which you can extends to build the front map of your application. It's build with a router that will solve the request. Is implementing the ArrayAccess interface and runFromGlobals method which is used in the generic redcat's index.php.
Extending:
class MyFrontOffice extends \RedCat\Route\FrontOffice{
    function __construct(Router $router,Di $di){
        parent::__construct($router,$di);
        $this->map([
            ['backend/','new:RedCat\Plugin\FrontController\Backoffice'],
            [['new:RedCat\Route\Match\Extension','css|js|png|jpg|jpeg|gif'],'new:RedCat\Plugin\FrontController\Synaptic'],
            [['new:RedCat\Plugin\RouteMatch\ByTml'.($this->l10n?'L10n':''),'','template'],'new:RedCat\Plugin\Templix\Templix'.($this->l10n?'L10n':'')],
        ]);
    }
    function run($path,$domain=null){
        if(!parent::run($path,$domain)){
            http_response_code(404);
            print "Page not found !";
            exit;
        }
        return true;
    }
}
            

And then, use it:
use RedCat\Route\Match\Prefix;
use RedCat\Route\Match\Suffix;
use RedCat\Route\Match\Regex;
use RedCat\Route\Match\Extension;

$f = new MyFrontOffice();
            
Append and prepend methods:
$this->append(new Prefix('test/'),function($path){
    print "My url start with 'test' followed by '$path'";
});
$this->prepend(new Prefix('test/more'),function($path){
    print "My url start with 'test/more' followed by '$path'";
});
$f->append(new Suffix('.stuff'),function($path){
    print "My url end with '.stuff' preceded by '$path'";
});
            
Z-index like api in third parameter (default is zero)
It will look first for ".stuff" matching, then "test/more", and finally "test/":
$this->append(new Prefix('test/'),function($path){
    print "My url start with 'test' followed by '$path'";
},2);
$this->prepend(new Prefix('test/more'),function($path){
    print "My url start with 'test/more' followed by '$path'";
},2);
$f->append(new Suffix('.stuff'),function($path){
    print "My url end with '.stuff' preceded by '$path'";
},1);
            
Parameters automatic wrap:
// test/more is a string, consequently it will be wrapped automaticaly by Prefix object
$this->prepend('test/more',function($path){
    print "My url start with 'test/more' followed by '$path'";
});

// string start with "/^" and end with "$/", consequently it will be wrapped automaticaly by Regex object
$this->append('/^blog/(\w+)/(\d+)$/',function($category, $id){
    // if url is blog/php/200 it will print "php:200"
    print $category.':'.$id;
});
            
Empty url handling:
$f->append('',function(){
    print 'You are on home page !';
});
            
Lazy loading match, array containing first element starting with "new:", the object will be instantiated only if is necessary (previous didn't match):
$f->append(['new:RedCat\Route\Match\Suffix','.stuff'],function($path){
    print "My url end with '.stuff' preceded by '$path'";
});
            
Lazy loading callback, array containing first element or string starting with "new:", the object will be instantiated only if is necessary (matching):
// Class instanciation and method
$f->append('hello',[['new:MyModuleClass'],'methodName']);

// Class instanciation with construct params and method
$f->append('hello',[['new:MyModuleClass',$param1ForInstanciation,$param2ForInstanciation],'methodName']);

// Class instanciation and invokation
//   object will be invoked after instanciation using __invoke magic method if exists
$f->append('hello','new:MyModuleClass');
            
Run
//manual url
$f->run('test/');

//automatic current url
$f->runFromGlobals();
            

Router

The router is the component which is used by FrontController to map, append and prepend pair of match to behaviour. It support the methods explained before in FrontController except runFromGlobals.

Match

The basic match components are distributed under the RedCat\Route\Match namespace but there is also some examples of specific match in the RedCat\Plugin\RouteMatch namespace. The only rule to make a Match object is that he have to be callable implementing __invoke magic method. You can also use php Closure also called anonymous function.

Url

Url is a tiny helper for extract some simple components from Url. $url = new Url;

# http:// or https://
$url->getProtocolHref();

# mydomain.com
$url->getServerHref();

# output integer number of port if different from default (80 for http and 443 for https)
$url->getPortHref();

# root-path-of-redcat/
$url->getSuffixHref();

# http://mydomain.com/root-path-of-redcat/
$url->getBaseHref();

# http://mydomain.com/root-path-of-redcat/current-path/
$url->getLocation();

# http://mydomain.com/root-path-of-redcat/
$url->getSubdomainHref();

# http://fr.mydomain.com/root-path-of-redcat/
$url->getSubdomainHref('fr');

# if current subdomain contain 2 character it will output them
$url->getSubdomainLang();
            

tags: