Placing classes in elements according to the URI passed

Asked

Viewed 57 times

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 past is localhost/testes/index.php, I would like to know if there is how, and how to do so, depending on what is passed on URI, me adicionar or remover classes specific elements in that navbar. I searched through some questions with answers from stack, but none of them gave me a light on how to do or where to start, so I’m opening this.

  • Just make conditions with PHP and echo the classes you want to add, if I understood what you wanted to do.

2 answers

4


How did you not exemplify the possibilities of URI, you can do something by merging the functions 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;
}

Thus the variable $class shall have the value for each URI defined in switch, now just display it in the statement class:

<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 not enabled in your php o short_tag, replace the <?=$class?> for <?php echo $class ?>

  • 1

    In PHP 5.4+ o <?= already comes "enabled" by default and can be used without the need to short_tag. ;)

1

If that’s what I understood "Style Sheets", here’s an idea I’ve 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 that’s it, sorry if it’s not because it’s what I understood "style sheet". It wasn’t very elaborate because I just did that after reading your post. It becomes an idea that can be a seed for something more advanced, the possibilities are immeasurable. I have a class that I call Getdaurl that analyzes everything in the URL and lays the foundation for the other classes to know what it will show the user. Everything, absolutely everything is filtered by regex because everything goes through the default.php, there is nothing on my site that happens outside the root.

Browser other questions tagged

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