I'm trying to make a really simple website with multiple pages. I've been coding in HTML for a while now, but just started to experiment with PHP.
My page setup goes like this:
Rootfolder/settings/language -- In there, I just put a index.php
Rootfolder/settings/Privacy -- In there, I also just put a index.php
In this particular case, I could indeed use rename the files to language.php/Privacy.php to reduce space. But that is still not what I'm expecting from a lightweight website.
In order to make it easier for myself, I use include_once to include the header, meta data (I know it's not smart, because of SEO reasons), footers and other stuff that is very general and has to be the same across the whole website. But here is the thing; I really think that this method is way to complicated. Every time I need a new file, I just have to duplicate the page that includes the include_once files, and then change the content of it. Same goes for the titles.
So, what I'm looking for is a page setup like this (1) :
Rootfolder/Pages/index.php -- Inside that index.php, the content has to change dynamically. The visitors still have to go to: https://domain.tld/settings/language but the server has to decode it to the setup from (1) and change the content to the original content of Rootfolder/settings/language/index.php.
In the past, I've downloaded some PHP scripts, which all included title setups like: <title> $title - $SiteName </title> (or something like that) - So it must be possible to change content dynamically.
The problem is... I really don't know where to start. Does anyone has good/lightweight solution for this?
Related
I am a beginner in web development. I know that the index.php file provides the foundation of a website. With some template design, we can re-use the headers and footers for many of the pages of the site. For the content, suppose I have the following:
<?php include 'content.php';?>
However, each page contains unique content. Therefore, this makes me think that for every page that I have, I need to create a separate PHP file. For instance, for page one I create a content_one.php file and for page two I create a content_two.php etc. Am I correct in my assumption? Thanks in advance.
The simplest way of doing this is to make your index.php the front controller for the site.
You do this with a case statement like this:
$page = isset($_GET['page']) ? $_GET['page'] : 'home';
switch ($page) {
case 'faq' :
require_once('includes/faq.php');
exit;
case 'blog' :
require_once('includes/blog.php');
exit;
case 'home':
default:
require_once('includes/home.php');
}
Then your menus just need to use www.test.com?page=blog and adding new pages is as simple as putting the code and markup for that page in a new script in the includes directory and updating your menu code.
With rewrite rules, you can easily have url's like www.test.com/page/blog
It's a simple and effective way to handle routing.
Yes, that's pretty much the standard pattern for those coming to PHP from a predominantly HTML background. Typically they'll start with a multiple-page HTML site, each page with its own HTML file. As the site grows, they'll realize there is a lot of duplicated markup (header, footer, etc) and it becomes tedious to keep it all consistent. The solution is to rename their pages with a .php extension so the files are processed before they're served to the client, then the duplicate markup is removed and placed in an include file. The files are then all updated to pull in the content from the file using either include or require.
I wouldn't say that approach is wrong. This is exactly how I came to PHP oh so many years ago! But it isn't really the best in the long run. Realize that it's a stepping stone and as you learn more you'll realize what proper architecture/design looks like.
I don't know the true level of your programming experiences. When you're comfortable, you make want to look into some web frameworks, like Slim and Laravel. In these frameworks, the index.php file serves as a central entry point. All requests go through index and then are dispatched to different classes/methods/functions depending on the requested path. This is when your site starts to resemble a web application more than a web site from the development standpoint.
Best of luck!
I have a wordpress site which I manage(I am not a developer)
I ran a pagespeed test via https://developers.google.com/speed/pagespeed/
I got some issues like caching problems and so on so I used several plugins to take care of them.
however Im now stuck with Optimize CSS Delivery problems.
so I thought to try and fix it by myself and move the problematic URLs to the end of the page, however I cant figure out where these URLS are coming from. or which page is requesting for them.
appreciate any help with this
What I do in similiar situations: put some text inside the HTML elements of any php-file. E.g. put in page.php the "page-top" somewhere in the beginning of the file, put "page-end" at the end. In footer.php etc do the same. So you come very fast to the corner where the URLs might come from.
Here's a rough picture of how I how I have my files and what they do.
index.php at the start displays the beginning HTML tags and includes header.php (a div of links), and at the very end includes footer.php which displays the end HTML tags.
In between this, index.php takes all user input from GET parameters, POST forms, etc and gets data from the database (functions.php and mysql_functions.php). To display the body content it includes a PHP template file based on whatever page the user requested, e.g: index.php?p=page1 includes template_page1.php.
The information to be displayed in the template file is stored in an array instead of just plain variables, so in the template I then have what looks like this:
<title><?php echo $content['page_title']; ?></title>
And
<div>
<?php
foreach($name as $content['names']){
echo '<p>'.$name.'</p>';
}
?>
</div>
There is no real heavy code in the template files, only loops and basic if statements. The heavy stuff is done in index.php and the functions files.
Sometimes I feel though like it might be hacky and messy and not a good way of doing things. I looked at a template system (Code Igniter) the other day and it seems to be complex with so many different files to change just to display some content.
Could I just have some criticisms and some pros/cons of the way I'm doing it and some ideas of how I could better do it?
You are correct. It is not good practice to stack a lot of code into one file. However, if you are a beginner with PHP you shouldn't worry too much about it yet. I can't fully picture your file structure in my head, but I think I have an idea.
Try putting every PHP function and whatnot in other files and use as little PHP in index.php as possible. Though, make sure you don't have too many unnecessary files (one for every single event/function).
There was at least a beta version of Template Toolkit done in php. But the original in perl is essentially a different language. It's quite powerful.
The original is on CPAN. The current version is 2.25, I think.
My web building system has content and markup almost totally separated.
This is the basic page before content addition:
[% PROCESS header.inc
Title = "Sherwood's Forests -- Seedling Sales"
Desc = "Sherwood's Forests Pre-season orders for little Trees."
%]
<DIV id="content">
[% FILTER multimarkdown %]
# Seedling Sales 2014
<div markdown="1" class=picr>
![Picture alternate text][/base-absolute/path/to/image.jpg]
</div>
Please be patient
#### This file is still germinating
[% PROCESS Copyright.inc %][% END %]
</div>
[% INCLUDE footer.inc %]
All content goes between the FILTER line and the bottom Content is written in markdown. allows me to float an image on the right side of column. The CSS is entirely liquid so the screen is still usable (barely) on a iPhone.
Most of the magic is in header.inc. It reads a the file, fileindex.txt which is in essence the menu tree for the entire website. No java. All static web sites.
Fileindex.text looks like this:
ABBAB:f:/Home/Business_Stuff/Inventory.html:3:Inventory.html:Inventory
ABCAB:f:/Home/Business_Stuff/Ordering.html:3:Ordering.html:Ordering
ABDAB:f:/Home/Business_Stuff/Prices.html:3:Prices.html:Prices
ABJAB:f:/Home/Business_Stuff/Business_Stuff.html:3:Business_Stuff.html:Business Stuff
ADAAA:d:/Home/Services:2:Services:Services
ADJAB:f:/Home/Services/Christmas_Trees.html:3:Christmas_Trees.html:Christmas Trees
ADKAB:f:/Home/Services/Custom_Growing.html:3:Custom_Growing.html:Custom Growing
ADLAB:f:/Home/Services/Nature_Walks.html:3:Nature_Walks.html:Nature Walks
The alphabet soup at the beginning determines the sort order for the index. d or f is directory or file. The number is the number of levels down in the menu hierarchy it is. Then file component of the full path. Finally the text used for the link name. Some of this is redundent from earlier rewrites, but it wasn't worth fixing. "Don't optimize it yet". The last field allows having a different link name than the file name. This isn't used much.
fileindex.txt itself is generated by a perl program MakeFileIndex. It uses two sources of information: the results of find . run on the source tree, and a file tt2seq which has optional overrides on the order of menu items.
I want to add a new page to my site.
Copy the first file to the appropriate directory. Change the contents of that file with my new content. If it needs to appear in other than non-alphabetic order, then I have to add lines to tt2seq to control that. Run MakeFileIndex. Run ttree -a (a utility in Template Toolkit) This updates all the menu items in every file.
Verify that the page looks like I want. If not, then re-edit. Rerun ttree for the file I just edited. When I'm happy, sync to my server.
The net effect is that the amount of time I spend with the mechanics is tiny compared to the time it takes to write.
If I decide to re-arrange the site, I just move files around the directory tree, and rebuild. This does NOT fix any internal links in the content blocks.
The site produced this way is http://sherwoods-forests.com. It's an old fashioned site. The emphasis is on content, not on sizzle. But it's a good system for producing static pages.
Until now, I've been using the <iframe> tag to load things like headers/footers/navbars into my webpage. These cause so much hassle though and as I'm about to start building a new site I thought I'd get it sorted now.
I was thinking of having all the html code in a php file and just loading it in dynamically.. Ideally I'd like the code to become a part of the page. So it appears inline. But I also want to be able to edit one single file if I need to change one bit rather than editing the same file 100 times.
<iframe>'s did this well until recently and I don't want to use workarounds to solve my problems. Could someone please post some code I could adapt or post a link to something that tells me how to do this? Cheers
You can use PHP's include() function to include elements like headers and footers in your pages.
So:
include('header.php');
. . . will look for a file called header.php in the same directory and include it in your page. Then you just need to write this at the top of your pages.
That said, this isn't really a very good way to go about designing your site. How about looking for a content management system, that allows you to keep the design and content of your site separate?
Are PHP includes what you're looking for ? http://php.net/manual/en/function.include.php
Currently, I have a system that actively generates the pages on my site using PHP, more or less like so:
index.php ------include(query.php);-----> query.php grabs content from a file that corresponds to index.php
Query.php simply assembles the page from the mentioned index.php file and from header, footer, and navigation files.
The index.php file acts as a sort of proxy or label if you will so that when users visit the site, instead of having urls like "query.php?page=index" they have real pages.
The issue of course is that this is a bit convoluted. For each page of the site, I need 2 files: the "wrapper" file (such as index.php) and a content file to which it corresponds. I'd like to only be using a single file. The issue is that the single file should only contain the content of the page - not structure, header, footer etc.
So, what I'd like to be able to do is to be able to have index.php contain ONLYand a paragraph for example. When it is accessed, somehow PHP kicks in and applies a template and the header and footer.
Is PHP too high level a language to be able to do this? I know it is often done with Tomcat and java servlets, but I thought it would be cool to do with PHP.
EDIT: Important point, I don't want to use GET variables.
It's a bit hard to tell what you're trying to do, but have you looked into using a framework such as Kohana or Synfony? This will do pretty much exactly what you're asking.
If you're looking for a good template system, I suggest PHPTAL.
Failing that, it doesn't sound like you need to do anything that special. On the index.php page, why not just include header.php, the content, and footer.php? Short of using auto_append_file and auto_prepend_file, you can't add content to the page that is not explicitly in the code.
It sounds like what you want to do is route all requests through a single point (like frameworks do). Let's call it main.php.
On main.php you would have:
include header.php
include $_SERVER['PATH_INFO'] . ".php" //Requested file from URL. TODO handle this better
include footer.php
Then you would route all requests (using your server configuration) to "/main.php/pagename." Then pagename would only need its respective content.