Wordpress Bundle
  • What is Wordpress Bundle?
  • Getting started
    • Installation
    • Wordpress
    • Router
    • Cron job
    • Templates
  • Reference
    • Argument resolver
    • Controller
      • AdminAction
      • BlogController
      • FrontAction
      • WordpressAction
    • Entity
      • Block
      • Blog
      • Comment
      • File
      • Image
      • Menu
      • MenuItem
      • Post
      • Term
      • User
    • Repository
      • CommentRepository
      • PostRepository
      • TermRepository
      • UserRepository
    • Service
      • PaginationService
      • BreacrumbService
  • Guides
    • Server requirements
    • Install plugins
    • Use Multisite
    • Gutenberg
    • WP Steroids plugin
      • Image options
      • Maintenance
      • Admin pages removal
      • WYSIWYG Editor
      • Feature Support
      • Multi-site configuration
      • Constants definition
      • ACF configuration
      • Menu
      • Custom Post type
      • Advanced permalink settings
      • Custom Taxonomy
      • Templates
      • Page states
      • External table viewer
      • Roles
      • Optimisations
      • Security
      • Search
    • Twig Cookbook
    • Inject variables in all templates
    • Internationalization
    • Extending Wordpress Bundle
    • Error pages
    • Site health
  • Integrations
    • Advanced Custom Fields
      • Install ACF PRO
  • Extras
    • Migrating from 1.x to 2.0
    • Roadmap
    • Changelog
    • Alternatives
Powered by GitBook
On this page
  • Rendering Templates
  • Fetching Services
  • Fetching Repository

Was this helpful?

  1. Reference
  2. Controller

BlogController

PreviousAdminActionNextFrontAction

Last updated 2 years ago

Was this helpful?

Rendering Templates

If you're serving HTML, you'll want to render a template. The render() method renders a template and puts that content into a Response object for you:

return $this->render('home.html.twig', ['post'=>$post]);

Templating and Twig are explained more in the .

Fetching Services

Symfony comes packed with a lot of useful classes and functionalities, called . These are used for rendering templates, sending emails, querying the database and any other "work" you can think of.

If you need a service in a controller, type-hint an argument with its class (or interface) name. Symfony will automatically pass you the service you need:

use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Response;

public function homeAction(Post $post, LoggerInterface $logger)
{
    $logger->info('We are logging!');
    // ...
}

The Wordpress Bundle adds two services : and

/**
 * @param PostCollection $posts
 * @params PostRepository $postRepository
 * @param PaginationService $paginationService
 * @param BreadcrumbService $breadcrumbService
 * @return Response
 */
public function articleThematicAction(PostCollection $posts, PostRepository $postRepository, PaginationService $paginationService, BreadcrumbService $breadcrumbService)
{
    $context = ['posts'=>$posts];

    if( $page = $postRepository->findByState('news_landing') ){

        $context['breadcrumb'] = $breadcrumbService->build(['data'=>[[
            'title'=> $page->getTitle(),
            'link'=> $page->getLink()
        ]]]);
    }

    $context['pagination'] = $paginationService->build();

    return $this->render('page/term.twig', $context);
}

Fetching Repository

Internally, it still uses Wordpress native functions WP_Query, WP_Term_Query and WP_User_Query to ensure plugins compatibility

/**
 * @param Post $post
 * @param PostRepository $postRepository
 * @return Response
 */
public function pageAction(Post $post, PostRepository $postRepository)
{
    $context = [
        'post' => $post,
        'related' => $postRepository->findBy(['post_type'=>'page', 'post_parent' => $post->parent, 'post__not_in' => [$post->ID] ],null, 4)
    ];

     return $this->render('page.html.twig', $context);
}

get_page_by_state is a new function available in the WP Steroids plugin, see

The Wordpress Bundle adds three repository : , and designed to mimic way to fetch object from database.

Creating and Using Templates article
services
PaginationService
BreadcrumbService
docs
PostRepository
TermRepository
UserRepository
Doctrine Repository