Table of Contents

Building a Controller

Every time you access a VuFind URL, work is being done by a controller class to render a response. You can add new actions to VuFind by building new controllers, and you can modify existing behavior by extending or overriding existing controllers.

Background

A full description of Laminas controllers is beyond the scope of this page, but in a nutshell:

Controller workflow:

What controllers do:

Some of the resources on the Laminas page provide more detailed documentation.

VuFind-Specific Controller Conventions

Some VuFind-specific tips for building controllers:

Overriding an Existing Controller

To override an existing controller, follow these steps:

$config = [
    'controllers' => [
        'factories' => [
            'YourNamespace\Controller\ControllerNameController' => 'VuFind\Controller\AbstractBaseFactory',
        ],
        'aliases' => [
            'VuFind\Controller\ControllerNameController' => 'YourNamespace\Controller\ControllerNameController',
        ],
    ],
];

Creating a New Controller

Creating a new controller is much like overriding an existing one.

Example - How to add a new page to your theme

:!: This example requires VuFind 7.0 or newer; earlier releases of VuFind required more of this work to be done manually as described above.

Note: this example assumes you have set up a local custom module called MyModule and a local custom theme called mytheme.

php $VUFIND_HOME/public/index.php generate plugin MyModule\\Controller\\TestController VuFind\\Controller\\AbstractBaseFactory
<?php
 
namespace MyModule\Controller;
 
class TestController extends \VuFind\Controller\AbstractBase
{
    /**
     *
     * @return \Laminas\View\Model\ViewModel
     */
    public function homeAction()
    {
        return $this->createViewModel();
    }
}
php $VUFIND_HOME/public/index.php generate staticroute Test/Home MyModule	
This is my test page -- it embeds behavior from the search page:
<?=$this->render('search/home.phtml');?>

See the Code Generators Part 2 video for a hands-on example of building a custom controller.