Can not modify header information - headers already sent [duplicated]

0

Hello first of all I understand that the error can be produced by blank spaces before <?php or after the closing tags. but I really can not find the problem in my code, the message is as follows.

  

[10-Aug-2017 05:49:15 Europe / Berlin] PHP Warning: Can not modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/POO/project/Views/template.php : 65) in /Applications/MAMP/htdocs/POO/proyecto/Controllers/EstudiantesController.php on line 47

template.php

<?php namespace Views;
$template = new Template();

class Template
{
public function __construct()
{
?><!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>SIS32 | byCODE</title>
    <link rel="stylesheet" href="/Views/template/css/bootstrap-paper.css">
    <link rel="stylesheet" href="/Views/template/css/font-awesome.css">
    <link rel="stylesheet" href="/Views/template/css/myStyle.css">
    <script src="/Views/template/js/jquery.js"></script>


</head>
<body>
<nav class="navbar navbar-inverse">
    <div class="container-fluid">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
                    data-target="#bs-example-navbar-collapse-2">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="/">Administración de estudiantes</a>
        </div>

        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-2">
            <ul class="nav navbar-nav">
                <li class=""><a href="/">Inicio <span class="sr-only">(current)</span></a></li>

                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button"
                       aria-expanded="false">Estudiantes <span class="caret"></span></a>
                    <ul class="dropdown-menu" role="menu">
                        <li><a href="/estudiantes">Listar</a></li>
                        <li><a href="/estudiantes/agregar">Agregar</a></li>
                    </ul>
                </li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button"
                       aria-expanded="false">Secciones <span class="caret"></span></a>
                    <ul class="dropdown-menu" role="menu">
                        <li><a href="/secciones">Listar</a></li>
                        <li><a href="/secciones/agregar">Agregar</a></li>
                    </ul>
                </li>
            </ul>


            <ul class="nav navbar-nav navbar-right">
                <li><a href="#">byCode</a></li>
            </ul>
        </div>
    </div>
</nav>


<div class="container" id="container"><?php }

    public function __destruct(){

?></div>
    <script src="/Views/template/js/bootstrap.js"></script>
</body>
</html>
<?php
}


}
?>

StudentsController.php

<?php namespace Controllers;

use Models\Estudiante as Estudiantes;
use Models\Seccion as Secciones;

class EstudiantesController
{
    private $estudiante;
    private $section;

    public function __construct()
    {
        $this->estudiante = new Estudiantes();
        $this->section = new Secciones();
    }

    public function index()
    {
        $datos = $this->estudiante->listar();
        return $datos;
    }

    public function editar($id)
    {
        print 'Editar el alumno ' . $id;
    }

    public function agregar()
    {
        if ($_POST) {
            $permitidos = array('image/jpeg', 'image/png', 'image/gif', 'image/jpg');
            $limite = 700;

            if (in_array($_FILES['imagen']['type'], $permitidos) && $_FILES['imagen']['size'] <= $limite * 1024) {
                $nombre = date('is') . $_FILES['imagen']['name'];
                $ruta = 'Views' . DS . 'template' . DS . 'imagenes' . DS . 'avatars' . DS . $nombre;
                move_uploaded_file($_FILES['imagen']['tmp_name'], $ruta);


                try {
                    $this->estudiante->setNombre($_POST['nombre']);
                    $this->estudiante->setEdad($_POST['edad']);
                    $this->estudiante->setPromedio($_POST['promedio']);
                    $this->estudiante->setImagen($nombre);
                    $this->estudiante->setIdseccion($_POST['idseccion']);
                    $this->estudiante->add();
                    header("Location: " . URL . "estudiantes");


                } catch (\Exception $e) {
                    echo 'Ah ocurrido un error al registrar el alumno ' . $e->getCode();
                }


            } else {
                print 'imagen no permitida';
            }

        } else {
            $datos = $this->section->listar();
            return $datos;
        }


    }
}
$estudiante = new EstudiantesController();
?>

Note: I am following a videotutorial of poo , then I leave the template.php that is shown in the course.

<?php namespace Views;
	$template = new Template();
	class Template{
		public function __construct(){
?>
	<!DOCTYPE html>
	<html lang="es">
	<head>
		<meta charset="UTF-8">
		<title>Administración de estudiantes | Codigo Facilito</title>
		<link rel="stylesheet" href="<?php echo URL; ?>Views/template/css/bootstrap.css">
		<link rel="stylesheet" href="<?php echo URL; ?>Views/template/css/general.css">
	</head>
	<body>
		<nav class="navbar navbar-inverse navbar-fixed-top">
  		<div class="container-fluid">
		    <div class="navbar-header">
		      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-2">
		        <span class="sr-only"></span>
		        <span class="icon-bar"></span>
		        <span class="icon-bar"></span>
		        <span class="icon-bar"></span>
		      </button>
		      <a class="navbar-brand" href="<?php echo URL; ?>">Administración de Estudiantes</a>
		    </div>

		    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-2">
		      <ul class="nav navbar-nav">
		        <li><a href="<?php echo URL; ?>">Inicio</a></li>
		        <li class="dropdown">
		          <a href="" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Estudiantes <span class="caret"></span></a>
		          <ul class="dropdown-menu" role="menu">
		            <li>Listado</li>
		            <li>Agregar</li>
		          </ul>
		        </li>
		        <li class="dropdown">
		          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Secciones <span class="caret"></span></a>
		          <ul class="dropdown-menu" role="menu">
		            <li><a href="<?php echo URL; ?>secciones">Listado</a></li>
		            <li><a href="<?php echo URL; ?>secciones/agregar">Agregar</a></li>
		          </ul>
		        </li>
		      </ul>
		      
		      <ul class="nav navbar-nav navbar-right">
		        <li>SENA</li>
		      </ul>
		    </div>
		  </div>
		</nav>
<?php
		}
		public function __destruct(){
?>
	<footer class="navbar-fixed-bottom">
		Todos los derechos reservados &copy 2017 <br>
		Fresley Quiñones Angarita ~ @_bycar | <b>SENA</b>
	</footer>
	<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
	<script src="<?php echo URL; ?>Views/template/js/bootstrap.js"></script>
	</body>
	</html>
<?php
		}
	}
?>
    
asked by jeancarlos733 10.08.2017 в 05:57
source

1 answer

0

Basically the problem is that you have started sending content to the browser and while you are doing it, you have tried to modify the headers of the response.

Obviously, these headers are already sent and can not be modified again, because the response has already been defined and can not be changed while "sending"

In order not to do "copy and paste" I put a URL where the type of error and the possible causes are explained quite well:

link

In your case so I see the problem is the line of header("Location: " . URL . "estudiantes");

Anyway, what kind of templates are you using? Are you creating it yourself?

I think it would solve a lot of work using some template engine for PHP , some examples:

answered by 10.08.2017 / 09:31
source