About Features Downloads Getting Started Documentation Events Support GitHub

Love VuFind®? Consider becoming a financial supporter. Your support helps build a better VuFind®!

Site Tools


Warning: This page has not been updated in over over a year and may be outdated or deprecated.
development:plugins:controllers

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
development:plugins:controllers [2016/08/26 18:07] – [Example - How to add a new page to your theme] demiankatzdevelopment:plugins:controllers [2021/08/03 14:04] (current) demiankatz
Line 5: Line 5:
 ===== Background ===== ===== Background =====
  
-A full description of Zend Framework 2 controllers is beyond the scope of this page, but in a nutshell:+A full description of Laminas controllers is beyond the scope of this page, but in a nutshell:
  
 **Controller workflow:** **Controller workflow:**
Line 17: Line 17:
   * Each action method in a controller needs to return either data to pass to a view or a response object if a non-standard action (like a redirect) is necessary.   * Each action method in a controller needs to return either data to pass to a view or a response object if a non-standard action (like a redirect) is necessary.
  
-Some of the resources on the [[development:architecture:zend_framework|Zend Framework]] page provide more detailed documentation.+Some of the resources on the [[development:architecture:laminas|Laminas]] page provide more detailed documentation.
  
 ===== VuFind-Specific Controller Conventions ===== ===== VuFind-Specific Controller Conventions =====
Line 31: Line 31:
  
   * Create a [[development:architecture:customizing_vufind#modules|custom module]] if you do not already have one.   * Create a [[development:architecture:customizing_vufind#modules|custom module]] if you do not already have one.
-  * Create a new class in your module's namespace which extends the core VuFind controller you wish to override.  Change methods as desired. +  * Create a new class in your module's namespace which extends the core VuFind controller you wish to override. You can use the [[development:code_generators#overriding_existing_plugins_and_services_vufind_50_and_later|extendclass code generator]] to automate this process. Once the class is created, add/override methods as desired. 
-  * In your module's config/module.config.php, add a new invokable controller setting pointing to your new controller:+  * If you used the code generator, you are now done. If you are setting things up manually, you will also need to add some configuration to activate your new class. In your module's config/module.config.php, add a new factory setting pointing from your new controller to the factory class used to build it (which is usually the same factory used by the class you are extending), and then make your new controller an alias of the controller you are overriding:
  
 <code> <code>
-$config = array( +$config = [ 
-    'controllers' => array( +    'controllers' => [ 
-        'invokables' => array( +        'factories' => [ 
-            'controllername' => 'YourNamespace\Controller\ControllerNameController', +            'YourNamespace\Controller\ControllerNameController' => 'VuFind\Controller\AbstractBaseFactory', 
-        )+        ], 
-    )+        'aliases' => [ 
-);+            'VuFind\Controller\ControllerNameController' => 'YourNamespace\Controller\ControllerNameController', 
 +        ]
 +    ]
 +];
 </code> </code>
  
Line 48: Line 51:
 Creating a new controller is much like overriding an existing one. Creating a new controller is much like overriding an existing one.
  
-  * As described above, create a custom module, build a new controller, and set up an invokables entry for it. +  * As described above, create a custom module, build a new controller, and set up a factory entry for it. If you are using VuFind 7.0 or newer, you can use the [[development:code_generators#creating_new_plugins|plugin generator]] (see the example below)
-  * Now you need to add routing rules so the framework knows how to find your controller and so you can conveniently link to it within templates.  VuFind's main configuration (module/VuFind/config/module.config.php) contains some variables and loops which automatically generate most of its routes.  You may wish to copy some of this logic into your own module's configuration file so you can set up routes in a similar fashion.+  * Now you need to add routing rules so the framework knows how to find your controller and so you can conveniently link to it within templates.  VuFind's main configuration (module/VuFind/config/module.config.php) contains some variables and loops which automatically generate most of its routes.  You may wish to copy some of this logic into your own module's configuration file so you can set up routes in a similar fashion. Alternatively, you can just use one of VuFind's [[development:code_generators#creating_routes|route generators]].
  
 ==== Example - How to add a new page to your theme ==== ==== Example - How to add a new page to your theme ====
  
-// Notethis example shows how to modify the VuFind corelocalizing changes to a separate module requires some additional work. //+:!: This example requires VuFind 7.0 or newerearlier releases of VuFind required more of this work to be done manually as described above.
  
-  * Create a new controller in /vufindroot/module/VuFind/src/VuFind/Controller and name it as you want (e.g. TestController.php). +// Note: this example assumes you have set up a [[development:architecture:customizing_vufind#modules|local custom module]] called MyModule and a [[development:architecture:user_interface#creating_a_theme|local custom theme]] called mytheme. //
-It extends AbstractBase and contains just function that returns an array:+
  
- class TestController extends AbstractBase +  * Create a new controller called TestController, using this command (which sets up the controller to be built using VuFind's standard controller factory):
-+
- /** +
-+
- * @return \Zend\View\Model\ViewModel +
- */ +
- public function homeAction() +
-+
- return $this->createViewModel()+
-+
- }+
  
-  * In /vufind2root/module/VuFind/config/module.config.php   +  php $VUFIND_HOME/public/index.php generate plugin MyModule\\Controller\\TestController VuFind\\Controller\\AbstractBaseFactory
-below the 'controllers' => array( +
-add a new  +
-      'invokables' => array( +
-        'test'  => 'VuFind\Controller\TestController', +
-and below +
-$staticRoutes = array( +
-add your template filename and path +
-  'Test/Home',+
  
-  * Then create in your theme a directory and a file (lowercase) named as in the configuration (e.g. /vufindroot/themes/demovufind/templates/test/home.phtml) +  * This command will create a file called $VUFIND_HOME/module/MyModule/src/MyModule/Controller/TestController.php. Edit it, and add a homeAction method so that it looks like this: 
-  * Put your contents in that file. For test you can try with  + 
-''<nowiki><?=$this->render('search/home.phtml');?></nowiki>''+<code php> 
 +<?php 
 + 
 +namespace MyModule\Controller; 
 + 
 +class TestController extends \VuFind\Controller\AbstractBase 
 +
 +    /** 
 +     * 
 +     * @return \Laminas\View\Model\ViewModel 
 +     */ 
 +    public function homeAction() 
 +    { 
 +        return $this->createViewModel(); 
 +    } 
 +
 +</code> 
 + 
 +  * Now set up a static route to this new controller action like this: 
 + 
 +  php $VUFIND_HOME/public/index.php generate staticroute Test/Home MyModule  
 + 
 +  * Then create in your theme a directory and a file (lowercase) named as in the configuration (e.g. $VUFIND_HOME/themes/mytheme/templates/test/home.phtml) 
 +  * Put your contents in that file. For example, you can try this: 
 + 
 +<code php> 
 +This is my test page -- it embeds behavior from the search page: 
 +<?=$this->render('search/home.phtml');?> 
 +</code> 
 + 
 +  * Now point your browser at <nowiki>http://your-server/vufind/Test/Home</nowiki>, and you should see the results of your work! 
 + 
 +===== Related Video ===== 
 + 
 +See the [[videos:code_generators_2|Code Generators Part 2]] video for a hands-on example of building a custom controller.
 ---- struct data ---- ---- struct data ----
 +properties.Page Owner : 
 ---- ----
  
development/plugins/controllers.1472234843.txt.gz · Last modified: 2016/08/26 18:07 by demiankatz