PHP code example of adelowo / cfar

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

    

adelowo / cfar example snippets




//filename : index.php

use Aura\Router\RouterContainer;

Container->getMap();


$routeMapper->get('blog.read', '/blog/{ide}')
    ->handler('\Http\controller\BlogController@show');

$routeMapper->get(null, "/")
    ->handler('\Http\controller\BlogController');

$routeMapper->get('dev', '/dev');

$routeMapper->get(null,'/error')
    ->handler('\Http\controller\ErrorController'); //`indexAction` would be the invoked method

$routeMatcher = $routeContainer->getMatcher();


$request = Zend\Diactoros\ServerRequestFactory::fromGlobals(
	$_SERVER,
	$_GET,
	$_POST,
	$_COOKIE,
	$_FILES
);

$matched = $routeMatcher->match($request);

if (!$matched) {
	throw new \Aura\Router\Exception("Route does not exists");
}

foreach ($matched->attributes as $key => $val) {
	$request = $request->withAttribute($key, $val);
}


/**
 * This is totally optional. But you could use some "Control Inverting", than have `new` wrap all lines of your code
*`SomeContainer` implement `Interop\Container\ContainerInterface`.;
* A neat way to do this is to extend your choosen container and have the `get` method exposed by the interface retrieve the service from the container.
* @see https://github.com/slimphp/slim/
*/
$container = new SomeContainer(); 

//Add an ORM, Doctrine in this case.
$container['db'] = function ($container) {
    
    $paths = array("/src/Entities");
    
    $isDevMode = false;

    $dbParams = [
        'driver' => 'pdo_mysql',
        'user' => 'root',
        'password' => 'xx-xxx-xx-xx',
        'dbname' => 'foo',
    ];
    
    $config = \Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
    
    return \Doctrine\ORM\EntityManager::create($dbParams, $config);    
};

//You def' need a logger
$container['logger'] = function ($container) {

    $logger = new \Monolog\Logger("Your App Name");
    
    $handler = new \Monolog\Handler\SyslogHandler('Owambe');
    $handler->setFormatter(new \Monolog\Formatter\LineFormatter());
    
    $logger->pushHandler($handler);

    return $logger;
};

//register X,Y,Z services 

try {

    $cfar = new \Adelowo\Cfar\Cfar($matched , $container);

    $cfar->dispatch();

} catch (\Adelowo\Cfar\CfarException $exception) {
    echo $exception->getMessage(); 
}




namespace Adelowo\Controller;

class BlogController
{

    protected $container;
    
    public function __construct(ContainerInterface $container = null)
    {
        $this->container = $container;
    }
    public function showUser($id , $param)
    {
        $db = $this->container->get('db');
        
        $data = $db->find("User" , $id);
        
        var_dump($data);
    }

    public function showPdf($name)
    {

            echo $name;
    }

    public function indexAction($id ,$name)
    {
        echo $id. PHP_EOL;
        echo $name;
    }
}