voyteck/extcontrollers

Extensions for Zend Framework controllers

1.0.x-dev 2019-04-18 22:14 UTC

This package is auto-updated.

Last update: 2024-04-19 09:40:23 UTC


README

Extensions for Zend Framework controllers

Voyteck\VExtControllers\Controller\AbstractController

Extends the standard (Zend) AbstractActionController with possibility to distinguish between AJAX and normal (HTTP) actions. Also validates paramters passed to the controller - now all the GET/POST/params[] parameters can be specified in the function definition, including specifying whether the paramer needs to be POST or GET, as well as with possibility to provide optional parameters

Action methods usage

The most common way to use the class is to extend your controllers by this class. After doing this you will be able to create action methods in the following way:

  • public function testAction() for method that will be a standard 'test' action methiod and will work as before
  • public function testAjax() for method that can be called only using Ajax call
  • public function testAjax($parameter1) for method that can e called using AJAX and needs to provide parameter1 (so the call needs to have parameter1 specified - no matter if it is via POST or GET). Important thing is that this parameter here is mandatory and if not provided - the response 400 will be returned and error will be triggered
  • public function testAjax($parameter1 = 1) for method called using AJAX with with non-mandatory parameter1 that can be passed via GET or POST
  • public function testAjax($post_parameter1) for method called using AJAX with mandatory parameter1 (not post_parameter1 !) passed via POST - this parameter provided via GET will not satisfy the needs - response 400 will be returned and error will be triggered
  • public function testAjax($get_parameter1) for method called using Ajax with mandatory parameter1 passed via GET - this parameter provided via POST will not satisfy the needs - response 400 will be returned and error will be triggered

Also the way how the function returned values are treated is modified:

  • for Ajax calls (functions postfixed with Ajax) if returned value ($returnedValue) boolean - it is translated into return new JsonModel(['success' => $returnedValue]);
  • for Ajax calls (functions postfixed with Ajax) if returned value ($returnedValue) is array - it is translated into return new JsonModel($returnedValue);

protected function retrieveFileData(string $fileElementName = 'uploaded-file', array $postData = array())

Retrieves file that has been uploaded using AJAX transfer

  • @param string $fileElementName Name of element that is holding the transferred file needs to be provided - defaults to 'uploaded-file'
  • @param array $postData Contains list of other fields taht are sent together with the file (they will be stored in Hidden files of the form)
  • @return \Zend\Form\Form|boolean Returns FALSE if the form could not be correctly parsed/validated

Voyteck\VExtControllers\Factory\LazyControllerFactory

Factory for automatically create controllers. If used - package voyteck/extmvc needs to be installed (specified within required packages in composer.json file - so should be already installed if you use composer).

Usage

Easiest way to install (and use) the factory is to specify usage of it in your module.config.php file:

'controllers' => [
	...
	'abstract_factories' => [
		...
		\Voyteck\VExtControllers\Factory\LazyControllerFactory::class,
		...
	],
	...
],