Dynamic redirection with PHP Switch Case

Asked

Viewed 347 times

0

I’m getting beaten up and I don’t know why, because this is the first time I’ve tried to do something like this, I have a menu like this:

 <div id="collapseUtilities" class="collapse" aria-labelledby="headingUtilities" data-parent="#accordionSidebar">
      <div class="bg-white py-2 collapse-inner rounded">
        <h6 class="collapse-header">Clientes:</h6>
        <a class="collapse-item" href="?page=cadastro_cliente">Cadastrar</a>
        <a class="collapse-item" href="">Editar</a>
        <a class="collapse-item" href="">Listar</a>

      </div>
    </div>

As you can see he redirects to a page, down there I have

  <?php switch($_GET['page']){

        case 'home': include 'home.php';
        case 'cadastro_cliente': include 'cadastro_cliente.php';
        case 'categorias': include 'categorias.php';

      } ?> 

What happens is that if I enter the home page.php it lists down the register page and categories, if I enter the client registration it opens categories right below, what I need to do to open only the case page?

Hug

2 answers

0

In the switch, it is necessary to finish each block case with a break. Otherwise, PHP will continue to execute the following statements. It would look like this:

 <?php switch($_GET['page']){

        case 'home': 
            include 'home.php';
            break;
        case 'cadastro_cliente': 
            include 'cadastro_cliente.php';
            break;
        case 'categorias': 
            include 'categorias.php'
            break;
  } ?> 
  • 2

    It is not always "necessary". It has many situations where the cascade effect is desirable. Obviously this is not the case of the author, but you have to be careful not to generalize.

0


See that the initial error is in the omission of the statement break. See what the PHP manual says about this:

It is important to understand how the switch statement is executed in order to avoid mistakes. Switch declension performs line by line (actually, declaration by declaration). At the beginning no code is executed. Only when a case statement is found with a values corresponding to the value of the switch expression, PHP will start to execute the statement. PHP will continue to execute the statement until end of the switch block, or even the first break declaration found. If no break declaration is written at the end of the list of statements do case, PHP will continue executing the next cases.

Source: PHP Handbook

About the code, it is important to treat the request and set the "home" as the default page if no value is passed and on switch, also perform a treatment. Define a case default, a pattern that corresponds to everything that was not matched by the other cases. An example in your case, a 404 error page.

<?php

if (isset($_GET['page'])) {
    $requisicao = $_GET['page'];
} else {
    $requisicao = 'home';
}

switch($requisicao) {
    case "home":
        include(__DIR__."/home.php");
        break;
    case "cadastro_cliente": 
        include(__DIR__."/cadastro_cliente.php");
        break;
    case "categorias": 
        include(__DIR__."/categorias.php");
        break;
    default:
      include(__DIR__."/404.php");
}

In terms of information and curiosity, it is worth mentioning here that this is the basic about the Front Controller Design Standard.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.