PHP code example of dgvirtual / codeigniter4-components

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

    

dgvirtual / codeigniter4-components example snippets


<x-button-green onclick="alert('I was clicked!')">
   <?= $clickMeLabel 

<button
  style="color: white; background-color: green;"
  <?= isset($onclick) ? 'onclick="' . $onclick . '"' : '' 

public $psr4 = [
  APP_NAMESPACE => APPPATH,
  'Dgvirtual\Components' => APPPATH . 'ThirdParty/codeigniter4-components/src', // this line
];

public array $decorators = [
  'Dgvirtual\Components\Libraries\ComponentDecorator', // this line
];

<img
  src="<?= $src ?? '' 

<x-avatar src="<?= $userAvatarURL 

<button
  <?= isset($hx_post) ? 'hx-post="' . $hx_post . '"' : '' 

<x-button-green onclick="alert('I was clicked!')">
  Click Me!
</x-button-green>

<button
  style="color: white; background-color: green;"
  <?= isset($onclick) ? 'onclick="' . $onclick . '"' : '' 

$routes->get('famous-quotes/fetch', '\Dgvirtual\Components\Controllers\FamousQuotes::fetch');

public $componentsLookupPaths = [
  // your local components
  APPPATH . 'Views/Components/',
  // example components
  __DIR__ . '/../Components/',
];

namespace Config;

use Dgvirtual\Components\Config\Components as BaseComponents;

class Components extends BaseComponents
{
    public $componentsLookupPaths = [
        APPPATH . 'Views/Components/',
    ];

    /**
     * Cache all view-only components for 10 minutes.
     * Set to null (default) to disable.
     */
    public ?int $viewCacheTtl = 600;
}

class AvatarComponent extends Component
{
    /**
     * Cache the rendered HTML for 1 hour.
     * Null (default) means no renderer caching.
     */
    public ?int $cacheTtl = 3600;
}

class UserCardComponent extends Component
{
    public ?int $cacheTtl = 300;

    public function cacheKey(): string
    {
        // One cache entry per user
        return (string) session('user_id');
    }
}
html
  <x-famous-quotes seconds="30" />