0

I have only coded 2 sites till now, first one in plain php (wall of php intermingled with js, html , super messy) and the other in codeigniter(much better as far as separating display with logic is concerned)

now I have to code a website for a friend (designer). The friend wants me to use core php and not any framework like codeigniter (something I really like and am comfortable with !) .

I had a look at a couple of roll your own MVC frameworks guides, but am not sure if this is the ideal approach.

Can someone guide me here? my goal is to use plain php , without the help of thirdparty frameworks, to code a site , where the logic is separate from the view .

Thanks

I have a week to get comfortable without whatever approach we take here.
Please don't ask why I can't use a framework.

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by diafol
1

You have loads of ways of doing this. BUT, are you allowed to use a templating engine like RainTPL or Twig? Failing that you could use a DIY templater. Or even use heredoc synatx so that you can write html and include native variables.

Personally, I'd route everything through the index page (just like frameworks) and use htaccess to rewrite the url.

So the index.php file could be something like this:

<?php
    session_start();
    $page = new Page(); //my page object - gets various stuff like nav stuff, page name, etc
    include(file_exists('templates/' . $page->getIncludePage() . '.php')) ? 'templates/' . $page->getIncludePage() . '.php' : 'templates/404.php';
    //LOAD HEADER (COMMON) INFO
    include('templates/header.php');
    //ECHO INFO FROM TEMPLATE FILE
    echo $content;
    //LOAD THE FOOTER (COMMON) INFO
    include('templates/footer.php');
?>

templates/header.php

<!DOCTYPE HTML>
<html lang="en_GB" xml:lang="en_GB">
<head>
<meta charset="utf-8">
<title><?php echo $title;?></title>
<meta content="<?php echo $description;?>" name="description" />
<meta name="author" content="diafol" />
<link href="/css/site.css" type="text/css" media="all" rel="stylesheet" /> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
</head>
<body>
<div id="wrapper">
    <div id="header">
        <h1>My Site</h1>
        <div id="dropmenu" class="dropmenu">
            <?php echo $page->getNav();?>
        </div>
    </div>
    <div id="content">

templates/about.php

<?php
    $title ="Title of the page";
    $description="Meta description for the page";

$content = <<<"CONTENT"
    <h2>Page Title</h2>
    <h3>Section Title</h3>
    <p>Something to say $name</p>
CONTENT;
?>

templates/footer.php

    </div>
    <div id="footer">
        <p>&copy; diafol, 2012. All Rights Reserved.</p>
    </div>
</div>
</body>
</html>

That's just one approach, maybe not the best, but it's pretty simple. Oh, the htaccess file:

Options +FollowSymlinks
RewriteEngine On
RewriteRule ^(includes|class) - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/*$ index.php?page=$1 [L]

Edited by diafol

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.