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

Was this helpful?

  1. Reference

Argument resolver

PreviousTemplatesNextController

Last updated 2 years ago

Was this helpful?

Controller methods can inject properties if it is type-hinted correctly:

  • Post $post for single post page

  • Term $term and PostCollection $posts for archive page

  • User $author and PostCollection $posts for author archive page

  • Blog $blog

The resolvers take care of validating the controller callable and its arguments. In case of a problem, it throws an exception with a nice message explaining the problem.

This is also relevant to handle 404 error : see the guide

Exemple

<?php

namespace App\Controller;

use Metabolism\WordpressBundle\Entity\Post;
use Metabolism\WordpressBundle\Entity\PostCollection;
use Metabolism\WordpressBundle\Entity\Term;
use Metabolism\WordpressBundle\Entity\User;
use Metabolism\WordpressBundle\Entity\Blog;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class BlogController extends AbstractController
{
	public function homeAction(Post $post, Blog $blog)
	{
		if( $blog->is_admin )
			return $this->render('admin.html.twig', ['post'=>$post]);

		return $this->render('home.html.twig', ['post'=>$post]);
	}	
	
	public function pageAction(Post $post)
	{
		return $this->render('single.html.twig', ['post'=>$post]);
	}

	public function searchAction(PostCollection $posts, $search)
	{
        	return $this->render('search.html.twig', [
            		'search_query'=>$search,
		        'posts'=>$posts
        	]);
	}

	public function categoryAction(Term $term, PostCollection $posts)
	{
        	return $this->render('archive.html.twig', [
            		'posts'=>$posts,
            		'term'=>$term
	       	]);
	}

	public function authorAction(User $user, PostCollection $posts)
	{
        	return $this->render('author-archive.html.twig', [
            		'posts'=>$posts,
            		'author'=>$user
        	]);
	}
}
Better error pages