PHP code example of webignition / simplytestable-pagecache-bundle

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

    

webignition / simplytestable-pagecache-bundle example snippets




namespace App\Controller;

use SimplyTestable\PageCacheBundle\Services\CacheableResponseFactory;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Twig_Environment;

class ExampleController 
{
  public function exampleAction(
      CacheableResponseFactory $cacheableResponseFactory,
      Twig_Environment $twig,
      Request $request
  ): Response {
    // ... perform whatever operation your action oesn't match what
    // the request is asking for. Render and return a response based on the response
    // already created
    $response->setContent($twig->render(
        '::base.html.twig',
        []
    ));
    
    return $response;
  }
}



namespace App\Controller;

use App\Entity\Post;
use Doctrine\ORM\EntityManagerInterface;
use SimplyTestable\PageCacheBundle\Services\CacheableResponseFactory;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Twig_Environment;

class ExampleController 
{
  public function exampleAction(
      $post_id,
      EntityManagerInterface $entityManager,
      CacheableResponseFactory $cacheableResponseFactory,
      Twig_Environment $twig,
      Request $request
  ): Response {
    $postRepository = $entityManager->getRepository(Post::class);
    $post = $postRepository->find($post_id);

    $response = CacheableResponseFactory->createResponse($request, [
      'post_id' => $post_id,
    ]);
    
    if (Response::HTTP_NOT_MODIFIED === $response->getStatusCode()) {
        return $response;
    }
    
    $response->setContent($twig->render(
        'post.html.twig',
        [
          'post' => $post,
        ]
    ));
    
    return $response;
  }
}