Reputation: 43
Im trying to use make php generate pages by only including a header, is that even possible? Or must I use includes within the page for page components?
<?php
include($_SERVER['DOCUMENT_ROOT'].'/api/header.php');
$GLOBALS['pagetype'] = "main";
$GLOBALS['$pagetitle'] = "Test";
/* content goes here */
?>
Ideally I want all pages to follow this template.
One of my layouts is something like this:
Page headerbar
----
Content
----
Navigation Index
How can I make it place the page content in the correct place?
EDIT: I suck at explaining stuff, sorry about that. What I want is actually, to make a header.php to do all the "include" work for the pages. So I don't need to place includes within specific positions of a page.
Upvotes: 0
Views: 550
Reputation: 22402
It is possible in many different ways. Here's a pretty solid way to get about it (although no security considerations taken here, you have to care about that)
Let's say you have the following directory structure on the web-server:
•/.
•/..
•/.htaccess
•/static-pages/index.html
•/static-pages/foo.html
•/static-pages/bar.html
•/templates/header.php
•/templates/footer.php
•/app.php
$page = $_SERVER['REQUEST_URI']? $_SERVER['REQUEST_URI'] : "/index.html";
$inc = './static-pages' . $page;
require_once('./templates/header.php');
include_once($inc);
require_once('./templates/footer.php');
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
server {
listen 0.0.0.0:80;
server_name localhost;
root /var/www/html/web/;
rewrite ^/app\.php/?(.*)$ /$1 permanent;
try_files $uri @rewriteapp;
location @rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
location ~ ^/(app)\.php(/|$) {
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_param HTTPS off;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
fastcgi_index index.php;
send_timeout 1800;
fastcgi_read_timeout 1800;
fastcgi_pass phpfpm:9000;
}
location /php/fpm/status {
fastcgi_pass phpfpm:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location /php/fpm/ping {
fastcgi_pass phpfpm:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
}
You should now be able to use:
This will load index.html in static-pages
will load foo.
etc.
My example was configured with https://github.com/devigner/docker-compose-php with some minor changes that are specific to my local setup, but you should be able to use it just out of the box to try things out.
Upvotes: 1
Reputation: 4217
I assume what you are asking for is a way to not have to put all the common HTML content. There are many ways to achieve this. The simplest to implement and understand is what has been described by others. Include header and footer files and sandwich your content in between them.
Your page would look like this:
<?php
// This has to be placed BEFORE including the other files
$pageConfig = ['pageType' => 'main', 'title' => 'Test' ];
?>
<?php require_once 'header.php'; ?>
<!-- YOUR PAGE CONTENT GOES HERE -->
<div>This is a test</div>
<?php require_once 'footer.php'; ?>
header.php
<!-- begin header.php -->
<html>
<head>
<title><?= $pageConfig['title'] ?></title>
</head>
<body>
<!-- end header.php -->
footer.php
<!-- begin footer.php -->
</body>
</html>
<!-- end footer.php -->
This is an extremely simple approach to templating and probably isn't suitable to building extensive applications. There are numerous templating systems and MVC systems that do this for you and more. You should look in to those if you are doing anything serious.
Upvotes: 1
Reputation: 65
Like @ADyson says, make a header.php page like this:
<?php
//use require_once('exampleclass.php'); if you want to use OOP- classes and
//put all other php code here
?>
<html>
<head>
<link rel="stylesheet" href="styles.css"/>
</head>
<body>
//put your html code here and use css to make it prettier
</body
</html>
Then in your other pages that need to use this page, put this in your php files:
<?php
include('header.php');
?>
It is that simple really, just keep including the header.php page and put different html code for the content in those pages.
Hope this answers your question! Ramon
Upvotes: 1