tags:

views:

305

answers:

11

I'm building a forum in PHP and MySQL (for the funs). And I'm looking for a more elegant, cleaner, nicer, smarter way of strcturing it. I usually do it like this:

page.php

<?php include 'header.php' ?>

<?php code for this specific page ?>

content <?php loops and shit all over ?>

<?php include 'footer.php' ?>

In the header file I have stuff I need on all pages like db connection, functions etc.

I'm just a hobbycoder and CakePHP, zend framwork is just too much for me. I just get sweaty when I see it. But maybe there is a super lightweight MVC out there that i could try using :/?

A: 

One cool way of structuring simple code I use to use:

<?php
include("header.php");

$page = $_GET["page"];

include($page.".php");

include("footer.php");
?>

For example: http://site.com/index.php?page=contact

Luca Matteis
That is dangerous (you have to really validate the input) and leads to URLs that are not friendly in my opinion.
J. Pablo Fernández
This is just a proof of structure. Validation and URL friendliness aren't included, but can of course easily be accomplished.
Luca Matteis
I agree with both points. paFileDB uses this technique to include certain files. They use an array to check for valid actions: $allowed_acts = array( 'home', 'page', 'etc' ); then they just check if "?act=<str>" is valid using that array.
Zack
+1  A: 

but maybe there is a super lightweight mvc out there that i could try using :/?

I'm a Zend Framework kind of guy, but you might want to consider Yii: it performs quite well, has code-generation capabilities in the same vein as CakePHP, and the documentation is pretty good.

Cal Jacobson
+2  A: 

I'm a big fan of Symfony. I'm not sure if I'd exactly call it lightweight however. I hear Prado is another good choice & it seems to be lighter weight.

dborba
+1  A: 

If you want "super light weight", just make a class or 2 to handle your data and roll your own template engine. Here are some examples for DYI PHP template engines

If you don't feel like creating your own, TinyButStrong is a very light one.

Edit Finally found a link I was looking for earlier. For my own PHP sites, I use a slightly modified version of this one. I highly recommend it. It's straight-forward, very easy to implement, and light.

Dinah
By being a hobby-coder who 'sweats' when seeing cakePHP, rolling his own template engine is definately the approach he should use.
Ian Elliott
A: 

Have you tried Symfony?

I haven't so I'm not recommending it. I've just heard about it (in a speed comparison with Ruby on Rails and Django) ages ago and pointing to it.

J. Pablo Fernández
+2  A: 

The Framework you want to use really depends on your current use case. If you just want to separate code and presentation Smarty might be not as bad of a choice because it is unlike the full fledged MVC PHP Frameworks, still very graspable for "Hobby" PHP Programmers in the beginning (even though I don't entirely agree with the idea of inventing basically another language just for templating purposes).

I know what you mean with that it is too much to learn ne of the big Frameworks in the beginning but when you once got into it you will really benefit from the Architecture as your Application grows with the time (and I already judge by your question, that you are interested into a proper application design).

Daff
A: 

Just create your own. Most of the frameworks out there are pretty heavy and you have to commit to them. Zend being an exception, you can use the pieces you want with your own framework.

For separating my view from my controller, I keep my html (or other format) in separate files with "substitution" data tags. I opt for surrounding my tags with %%. So for example:

// This template content can be in a separate file
// and loaded instead with file_get_contents()
$tpl  = <<<TPL
Isn't it great %%name%% that you can put "quotes" and html, 
css, and %%markup%% between HEREDOC parameters without having 
to use /slashes\ to escape.
TPL;

//Load your data into an array
$dataTags = array(
'%%name%%'=>'Joe',
'%%markup%%'=>'javascript'
);

//Then perform the substitution and output
echo str_replace( array_keys($dataTags), $dataTags, $tpl );

Thats a very basic example. But by creating a "contract" of substitution data tags, you could give it to a web specialist and you don't have to care what they do with creating the web page. That separates your View from your Controller.

For the Model part you may want to look into Doctrine.

A: 

By far the easiest (and best, imo) framework to learn is CodeIgniter. It has very clear documentation, and a helpful community. Give it a shot, it was my first framework and I was in the same, helpless boat as you.

ryeguy
A: 

Take a look at Fusebox for PHP. You can download the latest version here.

Mike Jr
+1  A: 

try nicedog - it's an extremly lightweight mvc-framework.

here's the example code

index.php:

require 'NiceDog.php';
R('')->controller('Test')->action('index')->on('GET');
R('tag/(?P<tag>[-\w]+)')->controller('Test')->action('p_tag')->on('GET');    

class Test extends C{
    public function index(){
        echo 'Hello';
    }

    public function p_tag($tag){
        $this->tag = $tag; 
        $this->render('views/index.php');
    }
}
run();

view/index.php:

<span><?php echo $this->tag; ?></span>

that's a simple but working web app!

there's also a ready-made .htaccess for nice urls. no database classes, views are php, not some fancy heavyweight templating system.

it's very well suited for a mvc-newcomer because it's so simple. almost no learning curve!

try it and you'll love it.

Schnalle
A: 

To be honest, the phpBB3 coding guidelines for structure are very easy to read and code in. To me, it makes the most sense when coding PHP and it's what I use when I do all my php work.

Zack