I have a ajax + pushState navigation system on my site.
If the user clicks an internal link, it will be done $.post
and I check in PHP if $_SERVER["REQUEST_METHOD"] === "POST"
so I load a page, for example: indexPost.html ,
and if the user directly access the link I make a $_SERVER["REQUEST_METHOD"] === "GET"
then I load indexGet.html .
Here is the example of indexe's :
indexPost.html :
<p>Olá, esse é o conteudo!</p>
indexGet.html :
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<title>indexGet.html</title>
<link rel="stylesheet" href="/style.css">
</head>
<body>
<p>Olá, esse é o conteudo!</p>
</body>
<script type="text/javascript" src="/script.js"></script>
</html>
I had to resort to developing this way because if I uploaded, for example, indexGet.html through $.post
the entire HTML file would be duplicated.
My question is: is there any simpler way to do this without having to always create 2 files for each action? Remembering that, I will have to do this with several files, that is, it would be a very tiring process.
I think there is a simpler way, but I could not think of anything better than that and besides, I researched and found no alternative examples.
EDITED
Well, here is an example of what I use on my system:
index.php (which is required by $.post
through my script):
if($_SERVER["REQUEST_METHOD"] === "POST") {
$html = new Template("caminho/views/post/index.html");
//aqui vão outras variáveis do template...
$html -> show();
} else if($_SERVER["REQUEST_METHOD"] === "GET") {
$html = new Template("caminho/views/get/index.html");
//aqui vão outras variáveis do template...
$html -> show();
}
I'm using a template system, and as you can see, I'm saving the same file, but in different folders, one for each case.
In the case of POST , the index.html saved in path / views / post / index.html contains only content.
In case of GET , the index.html saved in path / views / get / index.html has scripts and css's in addition of the content.
Thanks!