agavi output type overwrite in action

um den output type einer agavi action auch nach dem routing zu ändern, kann man innerhalb der action den output type überschreiben.


/**
 * assume that 'html' is the standard GET output for this action
 * when this action is called via POST you want to change the output type to 'json'
 * because on error or success you want to show small messages per json instead full html
 */
    public function executeRead(AgaviRequestDataHolder $rd)
    {
        return 'Input';
    }

    public function executeWrite(AgaviRequestDataHolder $rd)
    {
        $this->getContainer()->setOutputType($this->getContext()->getController()->getOutputType('json'));
        try
        {
            // do POST stuff
        }
        catch(Exception $e)
        {
            $this->setAttribute('message', $e->getMessage());
            return 'Error';
        }
        return 'Success';
    }

Agavi routing specials

Um bestimmte Agavi output_types bei speziellen routings zu bedienen kann man die routing.xml wie folgt anpassen.

<!-- cut out leading slash -->
<route name="slasher" pattern="^/" stop="false" imply="true" cut="true" />

<!-- match ajax calls with special http header -->
<route pattern="XMLHttpRequest" source="_SERVER[HTTP_X_REQUESTED_WITH]" stop="false" output_type="ajax" />

<!-- match json calls with special http header -->
<route pattern="application/json" source="_SERVER[HTTP_ACCEPT]" stop="false" output_type="json" />

<!-- cut out special pointer -->
<route name="portal" pattern="^({portal:[a-zA-Z]{3}}/)?" stop="false" imply="true" cut="true">
    <callbacks>
        <callback class="RoutingCallback" />
    </callbacks>
</route>