PHP code example of manuelj555 / ajax-bundle

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

    

manuelj555 / ajax-bundle example snippets



// app/AppKernel.php
public function registerBundles()
{
    return array(
        // ...
        new Ku\AjaxBundle\KuAjaxBundle(),
        // ...
    );
}


public function createAction(Request $request)
{
    $user = new User();
    $form = $this->createForm(new UserType(), $user, array(
        'action' => $request->getRequestUri(),
    ));
    $form->handleRequest($request);

    if ($form->isSubmitted() and $form->isValid()) {
        $this->get('fos_user.user_manager')
          ->updateUser($user);

        $this->addFlash('success', 'User Created!');

        $this->get('ku_ajax.handler')->success();
        // Calling to succes method on ajax Handler, stop the redirection on ajax request 
        // and send a status code 200
        return $this->redirectToRoute('admin_company_users_list', array('companyId' => $company->getId()));
    }elseif($form->isSubmitted()){
      // invalid form
      $this->get('ku_ajax.handler')->badRequest();
      // this send a status code 400
    }

    return $this->render('user/create.html.twig', array(
        'form' => $form->createView(),
    ));
}

$this->get('ku_ajax.handler')->success($statusCode = 200);
$this->get('ku_ajax.handler')->error($message, $statusCode = 400)
$this->get('ku_ajax.handler')->badRequest($statusCode = 400)
$this->get('ku_ajax.handler')->redirect($isOk = true, $statusCode = 278)