Issues

ZF-8257: ContextSwitch not respecting default context

Issue Type: Patch Created: 2009-11-09T08:27:14.000+0000 Last Updated: 2012-12-13T13:47:34.000+0000 Status: Open Fix version(s): Reporter: Mark van der Velden (dynom) Assignee: Matthew Weier O'Phinney (matthew) Tags: - Zend_Controller

Related issues: Attachments: - Zend-Controller-Action-Helper-Contextswitch.patch

Description

Zend_Controller_Action_Helper_ContextSwitch is ignoring the default set with setDefaultContext()

Comments

Posted by Satoru Yoshida (satoruyoshida) on 2009-11-16T13:38:08.000+0000

I set component

Posted by Mark van der Velden (dynom) on 2009-11-16T23:40:40.000+0000

The patch is no good, since it breaks functionality. I solved it in a different fashion, by doing the following:

\_getParam('format', $this->\_defaultContext); // Set the context $this->\_helper ->contextSwitch() ->setDefaultContext($this->\_defaultContext) ->setAutoDisableLayout(true) ->initContext($format); // .. ?> (But it feels that I'm hacking broken functionality.)

Posted by Kim Blomqvist (kblomqvist) on 2011-05-25T18:09:58.000+0000

I cannot see problem here. If this has been an issue it is likely already fixed as the source code around the patch provided here has notably changed. Please feel free to reopen if something new appears.

Posted by Alexander Romanenko (alexanderrv) on 2011-06-15T17:53:34.000+0000

I have the same problem in latest 1.11.7

Posted by Kim Blomqvist (kblomqvist) on 2011-06-16T04:32:35.000+0000

Alexander - could you provide detailed code to reproduce this issue and I will look into it again.

Posted by Alexander Romanenko (alexanderrv) on 2011-06-16T14:10:36.000+0000

Code taken straight from my current project <?php

class TransactionController extends Zend_Controller_Action {

public function init()
{
   $this->_request->setParam('format', 'xml');
   $this->_helper->contextSwitch()
                 ->addActionContext('getbymonth', array('xml'))
                 ->setDefaultContext('xml')
                 ->initContext();
}

public function getbymonthAction()
{
    $month = $this->_request->getParam('month');
    $year = $this->_request->getParam('year');

    $this->view->month = $month;
    $this->view->year = $year;
    // then I build XML file from these vars in getbymonth.xml.phtml

}

}

With setDefaultContext('xml') I should not have to use $this->_request->setParam('format', 'xml'); and probably addActionContext too but not sure about that one. Right now, I have setParam doing job of setDefaultContext.

Posted by Kim Blomqvist (kblomqvist) on 2011-06-22T07:24:17.000+0000

I have checked that setDefaultContext() has no effect in context switch process. You can set and get it, but the class does not use it.

Posted by Kim Blomqvist (kblomqvist) on 2011-06-22T10:45:47.000+0000

I think that the use case addressed here is invalid and the way it was mentioned to be used is something like this

<pre class="highlight">
 /* Uses default context */
$this->_helper->contextSwitch()
              ->setDefaultContext('json')
              ->addActionContext('foo')
              ->addActionContexts(array('bar', 'baz'));

/* Does not use default context */
$this->_helper->contextSwitch()
              ->addActionContext('foo', 'json')
              ->addActionContexts(array('bar' => 'json', 'baz' => 'json'));

As far as I understand the format param cannot be set by default to match with the context, because it breaks the functionality.

Posted by Alexander Romanenko (alexanderrv) on 2011-06-28T02:54:43.000+0000

sorry, not fully understanding your comment. Do you mean there is no place to use setDefaultContext in this plugin at all? Or there is no place for it in controller? (maybe during bootstrap plugin helper configuration?)

Posted by Kim Blomqvist (kblomqvist) on 2011-06-28T13:26:32.000+0000

bq. sorry, not fully understanding your comment. Do you mean there is no place to use setDefaultContext in this plugin at all? Or there is no place for it in controller? (maybe during bootstrap plugin helper configuration?)

Alexander,

You can set the default context by using setDefaultContext(), but it does not have any effect while the decision of the context which to switch has been made. If you want to force the action context to be xml, it's correct to use ``` as you have done, and there is nothing wrong with it.

bq. With setDefaultContext('xml') I should not have to use $this->_request->setParam('format', 'xml');

As said, I think that your logic is - unfortunately - invalid. setDefaultContext() wasn't designed to work like that and in my opinion it even shouldn't because that breaks the functionality of the normal action.

Posted by Kim Blomqvist (kblomqvist) on 2011-07-30T16:24:27.000+0000

Auto-reassign

Posted by Volker Richert (gemuesepudding) on 2012-12-13T13:29:49.000+0000

I think changing initContext like this

<pre class="highlight">
        // Return if no context switching enabled, or no context switching
        // enabled for this action
        $contexts = $this->getActionContexts($action);
        if (empty($contexts)) {
            return;
        }

        // Return if no context parameter provided
        if (!$context = $request->getParam($this->getContextParam())) {
            if ($format === null && $this->_defaultContext === 0) {
                return;
            }
            $context = $this->_defaultContext;
        }

will be a better solution. This will not break forcing a context but will allow to fall back to the defaultContext.

After changing it you can sud the ContextSwitch like this:

<pre class="highlight">
$this->_helper->contextSwitch()->setDefaultContext('json')->initContext();

After that 'json' will be used if not format ist submited, but the format parameter will be used it submited. And it's still possible to force XML by "initContext('xml')"

gemuesepudding

Posted by Volker Richert (gemuesepudding) on 2012-12-13T13:46:11.000+0000

A small fix. We have to check if _defaultContext is part of the action contexts:

<pre class="highlight">

        // Return if no context parameter provided
        if (!$context = $request->getParam($this->getContextParam())) {
            if ($format === null && $this->_defaultContext === null) {
                return;
            }
            if ($this->hasActionContext($action, $this->_defaultContext)) {
              $context = $this->_defaultContext;  
            } else {
              $context = $format;
            }
            
        }

Posted by Volker Richert (gemuesepudding) on 2012-12-13T13:47:34.000+0000

and $format must be set to null;

<pre class="highlight">
        // Return if no context parameter provided
        if (!$context = $request->getParam($this->getContextParam())) {
            if ($format === null && $this->_defaultContext === null) {
                return;
            }
            if ($this->hasActionContext($action, $this->_defaultContext)) {
              $context = $this->_defaultContext;  
            } else {
              $context = $format;
              $format = null;
            }   
        }

Sorry

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts