Zend Framework Source Code Snippets

Bgy_Controller_Plugin_AjaxContextDefaultFormat

Bookmark and Share
1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
34
3536
37
38
39
4041
42
<?php
class Bgy_Controller_Plugin_AjaxContextDefaultFormat
    extends Zend_Controller_Plugin_Abstract
{
    protected $_defaultFormat = null; 
    /**
     * Set a default format for context switcher
     *
     * It is used when no format paramater are provided     * If no format are specified in paramater, json will
     * be used.
     * @param string $context either json, xml or html
     */
    public function __construct($format = 'json')    {
        $this->setDefaultFormat($format);
    }
 
    public function setDefaultFormat($format)    {
        $ajaxContext = new Zend_Controller_Action_Helper_AjaxContext();
        if (!array_key_exists($format, $ajaxContext->getContexts())) {
            throw new Bgy_Controller_Exception('The format "'.$format.'" is not a valid format.');
        }        $this->_defaultFormat = $format;
 
        return $this;
    }
     public function getDefaultFormat()
    {
        return $this->_defaultFormat;
    }
     public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        if ($request->isXmlHttpRequest() && null === $request->getParam('format', null)) {
            $request->setParam('format', $this->getDefaultFormat());
        }    }
}

Comments

You must login before commenting on a snippet. If you do not have an account, please register.

Snippet description

To use in conjonction with AjaxContext Action Helper, and specify a default format, avoiding you to prepend it on each on your ajax request.

You can still use format parameter for specific case.

Usage:

$this->bootstrap('frontController');
$front = $this->getResource('frontController');
$front->registerPlugin(new Bgy_Controller_Plugin_AjaxContextDefaultFormat('json'));

Snippet details

Created:
Boris Guéry Boris Guéry
1 year ago
Edited:
Boris Guéry Boris Guéry
1 year ago
Revision Id:
180
Edit Message:
Initial Release
ZF Version
1.10.0
Tags:
plugin ajax context format
Comments:
0
Views:
3936
Points:
1 (1 votes)

History

r180

Initial Release

Boris Guéry Boris Guéry
1 year ago