Pagetitle and Meta Description for posts page | PyroCMS

By: William
Category: PyroCMS

How to set page title and meta description on posts index page

In a near future, this will have it's own easy to manage solution within PyroCMS. But until then, it's not easy to set pagetitle and description on the posts index page. This is the page that lists all your posts.

This is one solution, of many. The one that fits me. What i do is extending the core posts controller and using the variables module that comes with PyroCMS.

Step 1

Let's start with adding a variables group named blog. The slug is the important part, make sure its blog. Then we need to create two fields. I choose text fields where i can set the min, max and recommended length as well as include a counter. This turns out to be perfect actually since you are able to guide the client to write better seo stuff.

Make sure the slugs are pagetitle and description.

Once the fields are created, you go to groups and click assignments on the blog one. Assign both the fields you created. You can now go to variables->manage blog and write the pagetitle and description you want.

Step 2

Now, let's create our own controller that extends the default PostsCrontroller. This is mine:

namespace Pixney\WilliamastromTheme;

use Anomaly\VariablesModule\Variable\Command\GetVariableValue;
use Anomaly\PostsModule\Http\Controller\PostsController;
use Anomaly\VariablesModule\Variable\Contract\VariableRepositoryInterface;

/**
 * Class PixneyPostsController
 *
 * @link          http://pixney.com/
 * @author        Pixney. (hello@pixney.com)
 * @author        William Åström (william@pixney.com)
 */
class PixneyPostsController extends PostsController
{

	public function index() {
  		$this->template->set('meta_title',$this->setPageTitle());
  		$this->template->set('meta_description',$this->setDescription());
        return $this->view->make('anomaly.module.posts::posts/index');
	}

	private function setPageTitle() {
		return $this->dispatch(new GetVariableValue('blog', 'pagetitle', ''));
	}

	private function setDescription() {
		return $this->dispatch(new GetVariableValue('blog', 'description', ''));
	}

}

Step 3

In your routes file that we created during the previous blog post, let's adjust the blog route to use our custom controller:

Route::get('blog',[
    'as'   => 'anomaly.module.posts::posts.index',
    'uses' => 'Pixney\WilliamastromTheme\PixneyPostsController@index',
]);