tags:

views:

114

answers:

4

In the past, I've been creating my apps with basically a view (i.e. search.php or display.php) and a large file with a bunch of functions (i.e. library.php). I intend to change all this by going the MVC route w/o a framework. I just don't have time to learn a new framework, I want to get this done and do my best to separate the php code from my html code. If I can accomplish that, I'm happy. Anyways, how does one deal w/ the following? I think I've been doing this the wrong way ever since I started making user membership type sites.

Let's take stackoverflow for example. A guest visitor sees a "Login" page at the top of the header. Once you log in, the "header" changes to display menus that are relevant to a member of the site. In this case, I see my username and a logout link (along with a few others). What I've done in the past is take my header.php (I include it into my main page) and with a few if/else statements, I display it depending on whether a user is log in or not.

I found an example somewhere (dont recall), that had two separate views. If it's a guest user, display a different header from someone who is logged in. My code gets very messy if I have to keep track if a user is log in or not and having to display the correct view for the user.

+1  A: 

I would take a look at the Smarty template engine. It is available at http://www.smarty.net/

Once you have that installed, you should be able write code that separates the Controller from the View, like this:

SamplePage.php:

<?php
require 'Smarty.class.php';
class SamplePage extends Smarty
{
    public function handleRequest()
    {
        $this->template_dir = '.';
        $this->assign("text", "Hello World!");
        $this->assign("userLoggedIn", true);
        $this->display('SamplePage.htm');
    }
}
$page = new SamplePage();
$page->handleRequest();
?>

SamplePage.htm:

<h1>{$text}</h1>
{if $userLoggedIn}
  You are logged in.
{else}
  You are not logged in.
{/if}

You will need to create a web server writeable directory called templates_c to run this example.

David Barnes
I like Smarty a lot, and I highly recommend going this route. One thing though - if you find yourself trying to do things that you can't figure out how to do in Smarty, chances are good that you're thinking about it the wrong way. Remember, Smarty templates should only contain presentation code and template variables. No PHP!
iddqd
+1  A: 

I would recommend using a lightweight framework with an easy learning curve (such as kohanaphp) before you attempt to design your own MVC framework. That way you will already be familiar with the ins and outs of MVC to avoid common pitfalls and - more importantly - pick up on best practices. Cheers.

jusunlee
I'm a fan of kohana but as a first framework codeigniter might be a better bet due to the quality of documentation and size of community
seengee
A: 

MVC is more of a "best practices" package .. i have seen people writing the code app in controllers without any views .. so yeah everything goes with some. however what you are looking for is a way to divide your layers .. i wouldnt go the smarty route .. u will have to learn a whole new language .. i would encourage you to spend a little time learning any light weight MVC .. i really like Code Igniter .. i have used it a couple of times, gets you started pretty fast. there maybe several other options out there.

For your specific case you can have a main template , which lets say has 2 includes ... one runs when $_SESSION[userID] is present and other runs when it isnt. Several ways to go about it .. that is just an idea.

As others i would recommend , u spend a day and learn a basic MVC framework .. it would help in the long term.

Sabeen Malik
A: 

The main thing to take away when people might say "keep your php out of your html" is really to keep your business logic out of your views. There's absolutely nothing wrong with using actual php code/syntax in your html if you're using it to handle basic view logic such as looping over a list or whether or not to display a log in link or menu depending on the current user session. That's fine and dandy.

<ul>
<?php
    foreach($items as $item):
?>
    <li><?= $item; ?></li>
<?php
    endforeach;
?>
</ul>

You can still build a MVC application and use php for your view logic. Your business logic being anything dealing with your domain models and data persistence.

With that said here is a good article on the subject:

http://fabien.potencier.org/article/34/templating-engines-in-php

Good info on the pros and cons of using a template engine like Smarty as opposed to pure php in your view code.

If you do decide to use a template engine, an alternative to Smarty is the recently released Twig engine:

twig-project dot org

Hope this helped some!

Derek Reynolds