using redirect in moodle 2.0

using redirect in moodle 2.0

by Joseph Rézeau -
Number of replies: 2
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators
In the course of re-factoring a module for moodle 2.0 I am getting the following error. Can someone point me to some examples of best strategy to avoid this error in the new moodle 2.0 API? Thanks,
Joseph

You should really redirect before you start page output
* line 562 of \lib\outputrenderers.php: call to debugging()
* line 2602 of \lib\weblib.php: call to core_renderer->redirect_message()

Average of ratings: -
In reply to Joseph Rézeau

Re: using redirect in moodle 2.0

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Generally, a Moodle script should look like this:

require_once('config.php');

// Calls to optional_param and/or required_param.

// Initialise $PAGE.
$PAGE->set_url(...);

// Check permissions.
require_login(...);
require_capability(...);

// See if there is any submitted data.
// If so process it, and then redirect.

// Otherwise
echo $OUTPUT->header();

// Output the body of the page.

echo $OUTPUT->footer();


One feature of that is that every call to redirect is before the call to $OUTPUT->header();. The same applies in Moodle 1.9. All calls to redirect should be before print_header.

An example is http://cvs.moodle.org/moodle/course/request.php?view=markup. Actually, I think there is a small but there. There is a call to notice that I think should be a call to redirect.

http://cvs.moodle.org/moodle/course/pending.php?view=markup is a slightly more complex example.
Average of ratings: Useful (2)
In reply to Tim Hunt

Re: using redirect in moodle 2.0

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators
Thank you for your explanations, Tim. I've got it to work now.
Joseph