View generation outputs extraneous > just after <body> tag

Using version 3.2.7. I just recently upgraded to 3.2.7!

Output looks like:

<html>
<head></head>
<body>></body>
</html>

Note the extra > just after the body tag…

Layout blank.ctp is as follows:

<html>
BlankLayout.ctp
<?= $this->fetch('content') ?>
</html>

Template tester.ctp is:

<?php
echo 'Tester.ctp'
return;
?>

Controller tester is:

function tester() {
    $this->viewBuilder()->layout('blank');
}

All this generates a page source of:

><html>
BlankLayout.ctp
Tester.ctp</html>

a single greater than sign on the page with the two outputs from Layout & Template for testing!

This > sign doesn’t seem to affect HTML pages, but trying to output pdf doesn’t work since “output has already been generated!” error occurs.

I’ve changed AppController so that initialize and beforeFilter just return after parrent:: is executed.

I’ve added a change to AppView.php so that the initialize() function performs ONLY a parent::initialize() and returns. No other requires, echo statements!

Any ideas on where this > sign is being output would be greatly appreciated.

After much pulling of hair (what little I have left)… The extraneous > was found at the end of the MyController.php

} // end of class
?>>

I see now that any text (or html) I place in a controller that is NOT php script is sent to the output! Is this what is supposed to happen? I guess so, since I often use echo to output debug information within a controller function… I just didn’t expect “extraneous” text in a controller, and outside of the class to be automatically sent to the output! Actually, I quite expected a run time error since the file is “strictly” speaking a php file to be processed only by php.

Any way, I’ve found my error and I’m glad it is easily resolved… I’ll leave this on the forum to maybe help others in the future!

Some php advices is that do not add the finishing ?> at the end of the file in not-view action files.

Also, do not echo anything in controller or model. If you try to redirect or modify some header after an echo call it wont work because php already sent the headers and started send the content (altough there are some methods to avoid that like output_buffer but they are not bullet-proof)

2 Likes

Thanks @raul338. I’ve seen the ending tag left out in some others code, and wondered about it. I’ll try to break the habit of always closing all matching braces, etc…

By the way, I’ve modified the solution since I don’t output a lot of stuff in a controller… I just meant that I use echo in controllers for var dumps, and debugging stuff.

1 Like