Undefined index in GET

-1

Folder tree:

jp@JP:/opt/lampp/htdocs/curso$ tree
.
├── clases
│   ├── Conexion.php
│   └── Estudiante.php
├── css
├── imagenes
├── index.php
├── js
└── modulos
    ├── Controlador.php
    └── Enrutador.php

5 directories, 5 files

File index.php :

<?php 
    include_once('modulos/Enrutador.php');
    include_once('modulos/Controlador.php');

 ?>
 <!DOCTYPE html>
 <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>POO en PHP</title>
        <link rel="stylesheet" href="css/estilos.css">
    </head>
    <body>
        <h1>
            Bienvenidos todos los estudiantes
        </h1>

        <section>
            <?php 
                $enrutador = new Enrutador();
                $enrutador->cargarVista($_GET['cargar']);
             ?>
        </section>
    </body>
 </html>

File Enrutador.php :

<?php 
    class Enrutador{

        public function cargarVista($vista){

            switch ($vista): 

                case "crear":
                    include_once('vistas/' . $vista . '.php');
                    break;

                default:
                    include_once('vistas/error.php');



            endswitch;

        }
    }
 ?>

Errors when I run it:

  

Welcome to all students

     

Notice: Undefined index: load in /opt/lampp/htdocs/curso/index.php on line 21

     

Warning: include_once (views / error.php): failed to open stream: The file or directory does not exist in /opt/lampp/htdocs/curso/modulos/Enrutador.php on line 13

     

Warning: include_once (): Failed opening 'views / error.php' for inclusion (include_path = '.: / opt / lampp / lib / php') in / opt / lampp / htdocs / course / modules / Router. php on line 13

    
asked by José Pablo Santizo 05.09.2017 в 07:36
source

1 answer

1

The first error or "Notice" is because you are probably not passing any value in the URL for the index to load, to avoid that error appears, you can do a check like this:

$enrutador->cargarVista(isset($_GET['cargar']) ? $_GET['cargar'] : '');

The second error is quite explicit and is easily evident in the structure of folders and files that you show:

  

The file vistas/error.php does not exist, in fact there is not even the vistas folder.

    
answered by 05.09.2017 в 07:41