Dealing with custom controllers

All I want to do is a button in the top admin menu which would start the file download on the same page of admin interface. I’ve done it, but it keeps adding unnecessary header, scripts and footer into the file AFTER the data I need. How do I fix this? I’ve extended modManagerController, tried to override the methods but no. It keeps adding stuff from manager/templates/default/header.tpl, footer.tpl. Please help!

If you don’t need a full CMP, then maybe you can use the field “Handler” in the Menu instead and just run a Javascript command to start the download.

Not an option because Im fetching data from db via php script (which extends modManagerController), I don’t believe it’s impossible to do. And besides IDK how to work with MODx js handlers omg

I have not tested it but maybe you could make an AJAX-request (MODx.Ajax.request(...)) to a connector and fetch your data in a processor (instead of a controller).

I was affraid someone would reply like this. I appreciate but it’s gonna take me forever to get into connectors + modx js requests, I’ll try it tho, but preventing header/footer from appearing inside a csv file (OH WHY, GOD? WHY?) seemed easier

OK, you’re right. It’s seems to be possible with a controller.
Maybe something like this works:

class MyDownloadManagerController extends modExtraManagerController {

    public function process(array $scriptProperties = array()) {}
    public function getPageTitle() {
        return '';
    }
    public function getTemplateFile() {
        return '';
    }
    
    public function render() {
		header('Content-Type: text/csv');
		header('Content-Disposition: attachment;filename="test.csv"');
		
		echo "just;a;test";
		exit;
    }
}

exactly what I’ve tried, no effect. It outputs digit 1, omg from time to time it happens idk why. Also all methods should be implemented( which I did but no effect)

Here is an example of an export controller from the extra “Collections”

It has the additional lines session_write_close(); and ob_clean(); and adds some more headers (e.g. for Cache-Control) that maybe are necessary for the code to work correctly.

Yes! Just had to add die() after the script! No need to extend the class then. Thank you

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.