2

How can i FORCE raw output in joomla 2.5 without passing &format=raw in the url ?

i tried this in the controller.php

require_once (JPATH_BASE.DS.'libraries'.DS.'joomla'.DS.'document'.DS.'raw'.DS.'raw.php');
JFactory::$document = new JDocumentRaw();
//doesn't work, outputs only "Array"

and

JRequest::setVar('tmpl', 'component');//doesn't disable view rendering

and

$document = &JFactory::getDocument();
$doc = &JDocument::getInstance('raw');
$document = $doc;
//gives me Strict Standards: Only variables should be assigned by reference in ...
//and doesn't disable view rendering

Since i print raw output, i don't even build Jview and stuff, just die() from the controller, but i wanted to see if there was a nicer way ?

max4ever
  • 11,909
  • 13
  • 77
  • 115

5 Answers5

1

To change doc type and load a new renderer you need (change 'raw' to 'yourDocType'):

if (JRequest::getVar('format') != 'raw') {
    $url = JURI::current() . '?' . $_SERVER['QUERY_STRING'] . '&format=raw';
    header('Location: ' . $url);
    // or if you dont mind a text/html header ...
    // redirect($url);
}

Or:

    JRequest::setVar('format','raw');
    JFactory::$document = null;
    JFactory::getDocument();

Or:

    JFactory::$document = JDocument::getInstance('raw');
user229044
  • 232,980
  • 40
  • 330
  • 338
ekerner
  • 5,650
  • 1
  • 37
  • 31
0

You have to use setType for the JDocument instead of of getting new instance. You will also need view.raw.php view instead of view.html.php.

Adding code below to your controller should solve your problem.

public function __construct()
{
    parent::__construct();

    $document = JFactory::getDocument();
    $document->setType('raw');
}

You can also crate your own output template (e.g tmpl=max4ever instead of tmpl=component)... and customize output any way your like. See answer in this thread.

Community
  • 1
  • 1
Alex
  • 6,441
  • 2
  • 25
  • 26
  • so i have to build the viewer anyway, i was hoping to avoid this – max4ever Apr 27 '12 at 07:42
  • you don't have to. 1st option of setting document type should work fine. If you have your login in `view.html.php` simply create new file `view.raw.php` and make that view class extend or include your html view. – Alex Apr 27 '12 at 13:08
  • the point is i don't want to build extra folders and extra files, since i am outputing raw, i just echo from the controller then i die() – max4ever Apr 27 '12 at 13:13
  • @max4ever whatever works for you. Keep in mind that dying in controller will prevent some plugins from being executed and script will not finish it's execution properly. It's sort of hacking the framework... – Alex Apr 27 '12 at 13:18
  • This doesn't appear to work when adding to the component's controller. The only solution that worked (for me) was _ekerner's_ first suggestion. – ᴍᴀᴛᴛ ʙᴀᴋᴇʀ Nov 27 '13 at 16:06
0

A little late, but I spent way too much time hunting when I needed this, so hear you go.

I used this code to load the Joomla Platform, which made "everything available", and proceeded to write my own output according to my needs. In my case, I wanted "api" calls to interact with another system we have, so put this in my root directory (with some other checks for safety), and voila - with JSON or XML output, this did the trick.

in my root as "index_api.php"

// We are a valid Joomla entry point.
define('_JEXEC', 1);

// Setup the base path related constant.
define('JPATH_SITE', dirname(__FILE__));
define('JPATH_BASE', JPATH_SITE);
define('JPATH_PLATFORM', JPATH_BASE . '/libraries');

// Import the platform
require_once JPATH_PLATFORM.'/import.php';

define('DS', DIRECTORY_SEPARATOR);

if (file_exists(JPATH_BASE . '/includes/defines.php'))
    include_once JPATH_BASE . '/includes/defines.php';

if (!defined('_JDEFINES'))
    require_once JPATH_BASE.'/includes/defines.php';

require_once JPATH_BASE.'/includes/framework.php';

// Instantiate the application.
$app = JFactory::getApplication('site');

// Make sure that the Joomla Platform has been successfully loaded.
if (!class_exists('JLoader'))
    exit('Joomla Platform not loaded.');

/* Now try some standard Joomla stuff. None of the below is necessary, just showing that Joomla is loaded and running*/
$config = new JConfig();

$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('version_id');
$query->from('#__schemas');
$query->where('extension_id=700');
$db->setQuery($query);
$db->query();
if($db->getErrorNum()) {
    die($db->getErrorMsg());
}else{
    echo "<pre>Joomla Version is ".$db->loadResult()."</pre>";

}

echo "<pre>".print_r($config,true)."</pre>";
echo "<pre>".print_r($db,true)."</pre>";
echo "<pre>".print_r($app,true)."</pre>";
GDP
  • 8,109
  • 6
  • 45
  • 82
0

In your querystring, put the following variables:

format=custom
view=viewName

If you had a view, for example called json, you would create the view folder in the views parent folder called: json

Inside this you put your view file and use the format type in the file name.

view.json.php

In your url string you put (for example):

index.php?option=com_customcomponent&view=Json&format=json

This will call the json view and load the view.json.php view file. The class name would be customcomponentViewJson

If you are actually using json output you would need to set appropriate mime headers on the document.

$doc = JFactory::getDocument();
$doc->setMimeEncoding('application/json');

Here is a good article on using json with Joomla: http://docs.joomla.org/Generating_JSON_output

This works with Joomla 2.5 and I think 1.5. If the format is set to anything other than html or xml, I think Joomla assumes a raw output type and sends back the component response without the site template. i.e. the raw response you want for things like json messages.

This seems to be poorly documented and hard to understand. I have spent several frustrating hours with this issue, so hope this helps someone.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
0

Well for me I'm doing it other way without the raw and it goes ok

$mainfraim = & JFactory::getApplication();
        $idContact = JRequest::getVar('id_contact');
        $modelContact = $this->getModel('clientcontact');
        if($modelContact->delete($idContact))
            print "1";
        else 
            print "0";
        $mainfraim->close();

I open the JFactory I do whatevere I want and then I close it for the result I echo what ever I want if you want the JSON it's simple

 $matches = array_slice($matches, 0, 15);

        echo json_encode($matches);
Rad
  • 4,403
  • 4
  • 24
  • 25