PHP code example of melodyx / ajax

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

    

melodyx / ajax example snippets




return [
    ...
    Melodyx\Ajax\AjaxBundle::class => ['all' => true]
];

class Kernel extends BaseKernel
{
    `default kernel content`
    
    private Request $request;

    public function handle(Request $request, int $type = HttpKernelInterface::MAIN_REQUEST, bool $catch = true)
    {
        $this->request = $request;
        return parent::handle($request, $type, $catch);
    }

    protected function getHttpKernel()
    {
        if (!isset($this->request)) {
            return parent::getHttpKernel();
        }

        if ($this->request->get('handle')) {
            return $this->container->get('ajax_kernel');
        }

        return parent::getHttpKernel();
    }
    
    `default kernel content`
}

    public function handle<handler_name>(): void
    {
        $this->addPiece('<any_key_you_want>', '<any string content you send>');
    }



namespace App\Controller;

use Melodyx\Ajax\AbstractAjaxController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class HomepageController extends AbstractAjaxController
{
    #[Route('/', name: 'route.homepage')]
    public function index(Request $request): Response
    {
        if ($request->isXmlHttpRequest()) {
            $data = json_decode($request->getContent(), true);
            return new Response(json_encode($data));
        }
        return $this->render('homepage/index.html.twig', [
            'controller_name' => 'HomepageController',
        ]);
    }

    public function handleRedraw(Request $request): void
    {
        $this->addPiece('redraw', 'There is redrawed content');
    }
}