PHP code example of elie29 / core

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

    

elie29 / core example snippets


// point out to MY_PROJECT
$base = dirname(dirname(__DIR__));

// should return an array
return [
    // zend-phpdi-config
    'dependencies' => [
        'aliases' => [
            RouterInterface::class => Router::class,
            RenderInterface::class => Render::class,
        ],
        'autowires' => [
            Router::class,
            Render::class,
            HomeIndexController::class
        ],
    ],

    // Core configuration
    'core' => [
        // Router configuration: Query Protocol by default
        'router' => [
            RouterConst::NAMESPACE => 'Controllers\\'
        ],
        // Render configuration
        'render' => [
            RenderConst::VIEWS_PATH => $base . '/src/views',
            RenderConst::CACHE_PATH => $base . '/data/cache',
            RenderConst::CLEAN_OUTPUT => true,
        ],
    ],
];

// Protect variables from global scope
return call_user_func(function () {

    $config = $config));
});

chdir(dirname(__DIR__));

ta/config/container.php';

$core = new Core($container);
$core->run();

namespace Controllers;

class HomeIndexController extends AbstractController
{

   public function doRun(): bool
   {
      $render = $this->container->get(RenderInterface::class);
      // true because cache_time is not defined in config.php
      return $render->hasLayoutExpired();
   }

   public function run(array $params = []): array
   {
      $render = $this->container->get(RenderInterface::class);

      // global data for layout/templates
      $render->assign([
         'description' => 'first description',
      ]);

      // layout specific data
      return ['tplContent' => '<span>my content</span>'];
   }
}

public function run(array $params = []): array
{
   $render = $this->container->get(RenderInterface::class);

   // global data for layout/templates
   $render->assign([
      'description' => 'displayed description',
   ]);

   // layout specific data
   return ['tplContent' => $this->getMyTemplate()];
}

protected function getMyTemplate(): string
{
   $cacheID = sha1(__FILE__ . 'product/detail');
   $cacheTime = 5; // 5 seconds in cache

   $render = $this->container->get(RenderInterface::class);

   // data is not needed when file is read from cache
   $data = [];

   // overrides keys assigned in run
   $render->assign([
      'description' => __METHOD__,
   ]);

   if ($render->hasTemplateExpired($cacheID, $cacheTime)) {
      $data = [
         'item' => time(),
         'description' => 'overriden by assign method'
      ];
   }

   // specific product/detail template under views_path
   return $render->fetchTemplate($data, 'product/detail', $cacheID, $cacheTime);
}
html
<html lang="en">
<head>
   <meta charset="utf-8">
   <meta name="description" content=" echo htmlentities($description, ENT_QUOTES, 'UTF-8') 
html
<h1>Product detail Template</h1>

<div> echo htmlentities($item, ENT_QUOTES, 'UTF-8')