PHP code example of sparky-spa / sparky

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

    

sparky-spa / sparky example snippets


\SparkySpa\Sparky::config([
    // Mandatory properties
    'namespace' => 'App\SparkyComponent', // the namespace of your components
    'ajax_uri' => '<ajax_uri>', // endpoint for ajax request. It could be relate or absolute link. 
    'view_callback' => function(string $view_name, array $data = []): string 
    {
        // here you can use any views handler, blade, twig, handlebar etc.
        // Or, just implement the native PHP 

// POST <ajax_uri>/{$component_name}/{$action}/{$args}
// POST <ajax_uri>/{$component_name}/{$action}
// POST <ajax_uri>/{$component_name}

// GET <ajax_uri>/{\SparkySpa\Sparky::NAME_MINI_JS}

use function SparkySpa\handleHttpRequest;

echo handleHttpRequest(
	$component_name,
	$action,
	$args,
	$post_body
);


use function SparkySpa\handleHttpRequest;
use SparkySpa\Sparky;

echo handleHttpRequest(
	Sparky::NAME_MINI_JS
);



namespace App\SparkyComponent;

use SparkySpa\Component;

class ChatCheckerComponent extends Component
{
    #region Actions

    /**
     * @inheritDoc
     */
    public function render()
    {
        return $this->view('spa/chat_checker');
    }

    #endregion
}

use function SparkySpa\sparky;
use App\SparkyComponent\ChatCheckerComponent;

echo sparky('chat_checker', []);
echo sparky(ChatCheckerComponent::getName(), []);
echo ChatCheckerComponent::getTplBody([]);

use function SparkySpa\sparky;

echo sparky('user.chat_checker');

$component->mount(); // it doesn't call in Ajax request
 
$component->beforeExecuting();

$response = $component->customAction(); // an emitted method

if (!is_string($response)) {
    $component->beforeRendering();

    $response = $component->render();
}

$component->afterRendering();