PHP code example of karmaphp / karma

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

    

karmaphp / karma example snippets




= \Karma\AppFactory::create();

$app->run();



ainer = \Karma\ContainerBuilder::build(
    \Karma\Container::class,
    [
        'smarty' => \DI\get(\Karma\Service\SmartyService::class)
    ]
);

$app = \Karma\AppFactory::create($container);

$app->run();



ainer = \Karma\ContainerBuilder::build(
    \Karma\Container::class,
    [
        'smarty' => \DI\get(\Karma\Service\SmartyService::class)
    ]
);

$app = \Karma\AppFactory::create($container);

$app->get('/', [\App\Controller\MainController::class, 'Index']);

$app->run();

 namespace App\Controller;

use \Karma\Controller;

class MainController extends Controller
{
    public function Index()
    {
        return 'Merhaba Dünya';
    }
}

 namespace App\Service;

use Smarty;

class SmartyService
{
    /**
     * @var Smarty
     */
    protected $smarty;

    public function __construct()
    {
        $this->smarty = new Smarty();
    }

    public function fetch($template, array $params = [])
    {
        return $this->smarty
            ->assign($params)
            ->fetch($template);
    }
}

 namespace App\Service;

use Twig\Environment;
use Twig\Loader\FilesystemLoader;

class TwigService
{
    /**
     * @var Environment
     */
    private $twig;

    public function __construct()
    {
        $loader = new FilesystemLoader(ROOT_DIR . '/views/twig');

        $this->twig = new Environment($loader, [
            'cache' => ROOT_DIR . '/views/twig_c',
        ]);
    }

    public function render($name, array $context = [])
    {
        return $this->twig->render($name, $context);
    }
}