Melhor maneira de implementar um sistema de navegação Web 2.0
-
15-09-2020 - |
Pergunta
Agora aqui está minha situação:Estou fazendo um CMS.Quando os links são clicados, gostaria que as páginas carregassem dinamicamente usando Ajax.O problema nos links!
A única maneira de alterar o endereço na barra de endereço em tempo real é usar tags âncora.Mas o PHP não obtém as tags âncora, portanto não consigo carregar o conteúdo da página no carregamento do site usando PHP.E se eu carregasse as páginas usando uma string de consulta, a string de consulta não poderia ser atualizada na barra de endereço ao clicar em um link, pois isso recarregaria a página.
Suponho que o Javascript possa verificar o endereço, salvar a tag âncora em um cookie e recarregar a página, mas prefiro não ter que ir tão longe.
Alguém sabe uma solução para este problema?
Solução
Houve uma pergunta semelhante não faz muito tempo e descobri a seguinte solução.
Seus URLs devem apontar para páginas reais para que funcionem para usuários com deficiência de js.Os manipuladores de cliques devem lidar com solicitações ajax.Hash deve conter o URL e uma parte como &ajax
para indicar o tipo da solicitação.
Se a requisição for de ajax basta enviar o conteúdo.Caso contrário, coloque o conteúdo em cabeçalho e rodapé para responder com um site completo.
Os URLs devem funcionar com links para hashes gerados por ajax e usá-los como links.A ideia é basicamente imitar o tipo de comportamento que você pode ver no Facebook.
JavaScript
// click handler for ajax links
function goToWithAjax(hash) {
hash = hash.href ? hash.getAttribute("href", 2) : hash;
ajax( hash, function( response ) {
document.getElementById("content").innerHTML = response;
});
hash = ("#!/" + hash).replace("//","/");
window.location.hash = hash;
return false;
}
.htaccess
auto_prepend_file = "prepend.php"
auto_append_file = "append.php"
acrescentar
$url = $_SERVER['REQUEST_URI'];
$parts = explode('#!', $url);
$hash = isset($parts[1]) ? $parts[1] : false;
// redirect if there is a hash part
if ($hash) {
header("Location: $hash");
}
// find out if it's an ajax request
$ajax = strstr($url, "&ajax");
// we need header if it's not ajax
if (!$ajax) {
get_header();
}
acrescentar
// we need footer if it's not ajax
if (!$ajax) {
get_footer();
}
get_header()
function get_header() {
echo <<< END
<html>
<head></head>
<body>
<div id="page">
<div id="header">
<div id="logo"></div>
<ul id="nav">menu...</ul>
</div>
<div id="content">
END;
}
get_footer()
function get_footer() {
echo <<< END
</div> <!-- end of #content --->
<div id="footer">(c) me</footer>
</div> <!-- end of #page --->
</body>
</html>
END;
}
Outras dicas
I can see why you might want to load parts of the page with ajax. A whole page is rather pointless though.
A jQuery solution might be something like:
$(a.ajax_link).click(function(){
var url = $(this).attr('href');
$.ajax({
url:url,
success:function(data) {
$('body').html(data);
return false;
}
});
});
I have in no way tested that, but it should still work without javascript enabled.