What is the best way to set up a simple website? [closed]
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this questionI have been making websites for years now. Most of them tend to be under 10 pages and don't require any programming at all. I am wondering if anyone with lots of experience can recommend the standard or best way to go about setting up these types of websites. Let me explain in a bit more detail what I mean.
Here is a typical structure of one of the websites I have made in the past.
/css
/images
/js
/html
/html/about.html
header.php
footer.php
index.php
In this case, 开发者_JAVA技巧my index.php includes the header.php file at the top and the footer.php file at the bottom and I use PHP's switch function to handle the different cases for the content that I would like to view. For example, I might see index.php?z=about in the URL bar and my index.php will look for a file called about.html in /html
I know this all seems a little dumb to ask since is there really a best way to go about doing this?
Depending on the page, one of my main navigation items will have an "active" class. The work is done in PHP but I wonder if using javascript would be a better solution.
I'll appreciate any replies, thanks.
Just a few things off the top of my head:
- Yes, stick with PHP for the
active
class. Some people have JavaScript disabled. - You might want to use URL rewriting to de-uglify the URLs. (ex.
http://www.example.com/about
in the URL bar gets rewritten on the server tohttp://www.example.com/index.php?z=about
) - If the site gets bigger, you might want to use a database instead of files in a folder.
- I'd put
header.php
andfooter.php
either outside of the document root or in an includes folder that disallows access to it with a 403 Forbidden error.
I have uploaded a zip file of an example site to http://superuntitled.com/default/default.zip Take a look if you like.
I like to store all of my html content in included files, and leave my index.php file to call these files into a template.php file...
require_once('common.php');
$view = (isset($_GET['page']) && $_GET['page'] != '') ? $_GET['page'] : '';
switch ($view) {
case '' :
$view = 'home';
$section = 'templates/template-default.php';
$pageTitle = "The Homepage";
break;
case 'about' :
$view = 'about';
$section = 'templates/template-about.php';
$pageTitle = "The About Page";
$sidebar = 1;
break;
case 'notfound' :
$section = "templates/template-not-found.php";
$pageTitle = "404 not found";
break;
}
require_once 'view/template.php';
The template file is as simple as this:
require_once('header.php');
if(isset($sidebar))include("view/sidebar.php");
echo "<div id='content'>";
require_once (SERVER_URL.$section);
echo "</div>";
require_once('footer.php');
This way I can set all sorts of variables in the index.php file. The "pretty urls" are simple with htaccess (RewriteRule ^([-\A-Za-z0-9\_]*)$ index.php?page=$1
).
The common.php file contains all of the globals, session variables and includes database connection files and php functions files.
As for the active class, the $view variable can be checked against in the nav section, I use a simple php function:
function curPage($current, $pageTitle) {
if ($pageTitle==$current)
echo " class=\"selected\"";
}
And in the nav section I simply call this function:
<a href="about" <? curPage('about',"$view"); ?> >About</a>
This way keeps things clean and simple.
Use what is simplest yet does what you need. What you described seems fine and if it works for you, go ahead with it. And yes, use PHP for generating the "active" class - in fact, do as much as you can in PHP so that you don't need to rely on JavaScript.
Why don't you use a simple templating framework? They are relatively simple to understand and will expose you to some tried and tested methodology.
You'll also gain experience in using resources already available rather that coding everything yourself.
You won't need to make initial design decisions yourself which gets you up and running faster. (in simple cases like this)
For example: http://www.smarty.net/
Pick one with good documentation and an active developer community behind it.
精彩评论