Putting classes into elements according to the last URI

0

Well, I have the following code:

<body>
    <?php
    $server = $_SERVER['SERVER_NAME'];

    $endereco = $_SERVER ['REQUEST_URI'];

    ?>
    <!-- Menu principal -->
    <nav class="navbar navbar-default">
        <div class="container">
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav pull-right">
                    <li><a href="#">Home</li>
                    <li><a href="#">Quem somos</a></li>
                    <li><a href="#">Loja</a></li>
                    <li><a href="#">Fale conosco</a></li>
                </ul>
            </div><!-- /.Colapso da navegação -->
        </div>
    </nav>
    <!-- \.Menu principal -->

In this case, the URI passed is localhost/testes/index.php , I would like to know if it has how, and how to do so, depending on what is passed in URI , adicionar or remover co_de % specific elements in this classes . I looked up some questions with answers from navbar , but none of them gave me a light how to do or where to start, so I'm opening this one.

    
asked by anonymous 05.02.2017 / 21:43

2 answers

4

Since you have not exemplified the possibilities of URI , you can do something by merging parse_url and switch :

$url = parse_url('http://localhost/testes/index.php');

// Resulta em :
//array (3) {
//   ["scheme"]=>
//   string(4) "http"
//   ["host"]=>
//   string(9) "localhost"
//   ["path"]=>
//   string(17) "/testes/index.php"
// }

$class = '';

switch ($url['path']) {
    case '/testes/index.php':
        $class = 'class-a'
        break;
    case '/testes/create.php':
        $class = 'class-b'
        break;
    case '/testes/update.php':
        $class = 'class-c'
        break;
}

So the variable $class will have the value for every URI defined in the switch , now just display it in the class statement:

<nav class="navbar navbar-default <?=$class?>">
    <div class="container">
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav pull-right">
                <li><a href="#">Home</li>
                <li><a href="#">Quem somos</a></li>
                <li><a href="#">Loja</a></li>
                <li><a href="#">Fale conosco</a></li>
            </ul>
        </div><!-- /.Colapso da navegação -->
    </div>
</nav>

PS: If your short_tag is not enabled in your php, replace <?=$class?> with <?php echo $class ?>

    
05.02.2017 / 22:40
1

If it is what I understood "Style sheets", here is an idea I made now for this post:

<?php
class Monitora_a_ulr{
$classe_X = null;
function __construct(){
$this->onde = $_SERVER['REQUEST_URI']; $this->guardiao_da_url();
$this->DirPageFolhaEstilos = RAIZ.'/'.FOLHAESTILOS.'/';
}


function guardiao_da_url(){

preg_match('/^(?:[\/]{1}(teste))(?:.*)?$/', $this->onde, $resultado);

if((isset($resultado[1]) == true) AND ($resultado[1] != null)){


$a_folha_de_estilo = $this->DirPageFolhaEstilos.$resultado[1]."_style.css";

//Verificar se a folha de estilo existe
if(file_exists($a_folha_de_estilo) == true){

//Se ela existir atribuir ela à uma variável
$a_folha_de_estilo = $resultado[1];

} else {

//Senão atribuir a folha padrão a essa variavel
$a_folha_de_estilo = 'padrao'."_style.css";

}

}
//Estabelecer o link da folha de estilo
$classe = '<link rel="stylesheet" type="text/css" href="http://localhost/dir_das_classes/'.$a_folha_de_estilo.'" />';

//Retornar um resultado pra ser usado no site
return $this->classe_X = $classe;
}


}
?>

USE:

<?php
require_once(RAIZ.'/diretorio_das_funcoes_e_classes/Monitora_a_ulr.php');

$determina_a_classe_desta_pagina = new Monitora_a_ulr;

echo $determina_a_classe_desta_pagina->classe_X;
?>

I hope this is it, sorry if it is not because it was what I understood "style sheet". It was not very well done because I did it now after reading your post. It stands as an idea that may be a seed for something more advanced, the possibilities are immeasurable. I have a class that I call GetdaURL that parses everything in the URL and launches the base so that the other classes know what it is going to show the user. Everything, absolutely everything is filtered by regex because everything goes through default.php, there is nothing on my site that goes outside the root.

    
05.02.2017 / 22:41