views:

272

answers:

7

HI All,

I want to create a web site in MVC architecture, with out using any frameworks. What would be the Files/Folder structure (Module/Views/Models).

EDIT: I know the Zend, CakePHP. but my Question is how can we structure in Simple PHP. I dont want to use any of the frameworks, because i need the maximum performance/speed. I heard that frameworks are slower comparied to simple PHP web applications , That is why i selected simple PHP.

EDIT 2: I want every single webpage to be called via index.php,

A: 

I really like the symfony tree structure http://andreiabohner.files.wordpress.com/2007/03/cheatsheetsymfony001_enus.pdf

Regarding your edit, I suggest symfony directory structure (or a subset of it, as you need), not to use symfony.

Regarding the frameworks performances, it depends. Good frameworks know their issues, and use caching, and other optimization techniques; using a lightweight http server, instead of apache, may be another isuue. But everything depends on your particular needs.

AlberT
+2  A: 

You could have a structure like:

  root folder/
     index.php
     views/
     models/
     controllers/
     util/

index.php is the front loader - all URLs go through this and it works out which controller to call based on the URL. You can remove the index.php from the URL using mod_rewrite.

views/ folder contains your html,rss,xml,etc view files - you will populate values in these from your controllers. You could use Smarty here but, frankly, I don't see the point.

models/ folder will contain your ORM or DB connectivity code. A model is easy to write from scratch.

controllers/ folder will contain one controller file per URL. Say, books.php for the CRUD URLs for handling books. Each controller should load the model and use the data to populate a view.

util/ folder can hold any classes that are not models or controllers. Helper stuff like Security, session management, etc.

Seeing as you are writing it yourself you can do away with the cruft and keep it simple. But you may find it's actually a bigger job than you think. I know you don't want to use one but Code Igniter does keep things very simple, is light and doesn't try to do as much for you was other frameworks like Cake and Symfony do.

Steve Claridge
It's a good idea to only keep files you want to be accessed directly from the web in the root directory. More secure that way.
Reinis I.
A: 

Before to work with Zend Framework, I used to do something similar.

Here is the files/folders structure :

/
/views
/layouts
/controllers
/library
/etc
/data
Site.php
index.php

Views : contains all the templates, one per controller/action

Layouts : a layout which get a var containing the filename to include (from the views)

controllers : the controllers

library : all extra tools needed for the project

etc : documentations, etc..

data : for file upload

Site.php file used to init the whole project, kind of a bootstrap called by the index.php

index.php : call the bootstrap

<?php
class Site
{
    protected $_action = NULL;
    protected $_contentFile = NULL;
    protected $_args = array();
    protected $_headTitle = NULL;
    protected $_headerStack = array();

    public function __construct ($action)
    {
        $this->setAction($action);
        $this->setArgs();
    }


    public function setHeader($name = null, $value = null)
    {
        if (null != $name && null != $value)
        {
            $this->_headerStack[$name] = $value;
        }
    }

    public function sendHeaders()
    {
        if (null != $this->_headerStack)
        {
            foreach ($this->_headerStack as $key => $value)
            {
                header($key . ':' . ' ' . $value);
            }
        }
        return $this;
    }



    public function setAction($action)
    {
        $this->_action = (! empty($action) ) ? $action : 'error';
        return $this;
    }

    public function setArgs()
    {
        $this->_args['GET'] = $_GET;
        $this->_args['POST'] = $_POST;
    }

    public function getParam($name)
    {
        if ( ! empty($this->_args['GET'][$name]))
        {
            return $this->_args['GET'][$name];
        } else {
            return null;
        }
    }

    public function getParams()
    {
        return $this->_args['GET'];
    }

    public function getPost()
    {
        return $this->_args['POST'];
    }

    public function preRun()
    {
        if (is_file('views/' . $this->_action . '.phtml'))
        {
            $content = 'views/' . $this->_action . '.phtml';
            $this->setContentFile($content);
        }

        if (is_file('library/' . ucfirst($this->_action) . 'Action.php'))
        {
            require_once 'library/' . ucfirst($this->_action) . 'Action.php';
            if (function_exists  ('init')) 
            {
                init();
            }
        }
    }


    public function run()
    {
            $this->sendHeaders();
            $this->preRun();
            require_once 'layouts/main.phtml';
            $this->sendHeaders();
    }

    public function setContentFile($content)
    {
        $this->_contentFile = ( !empty($content) ) ? $content : '';
        return $this;
    }

    public function getContent()
    {
        require_once $this->_contentFile;
    }

    public function setHeadTitle($title)
    {
        $this->_headTitle = $title;
        return $this;
    }

    public function getHeadTitle()
    {
        return $this->_headTitle;
    }

}

Then, in my index I did :

$action = $_GET['action'];

$site = new Site($action);

$site->run();

I removed some extra security checks for convenience...

You can then extends this to include a models directory called from the controller, etc...

Boris Guéry
A: 

I've been building caret to be the most simple VC platform.

I have a simple C file that pulls meta data out of the comments that I then use JavaScript (server side) to build a URL router along with functions to link files together. It is extremely simple and makes programming a breeze without an OO weirdness.

Notice, that I don't have the M. I do the model aspect in my own language, but in general it reduces to a function that gets tables and a set of functions acting as RPC calls. I am using Kira..

MathGladiator
A: 

http://github.com/jaywilliams/WEB2BB/tree/master/application/

A fork of Web2BB

meder
A: 

The original creator of PHP has posted a good article about this type of thing.

http://toys.lerdorf.com/archives/38-The-no-framework-PHP-MVC-framework.html

alex
A: 

As user meder suggested, try very simple mvc framework web2bb. There is also my port of it, enhanced and optimized for PHP 5.2. You can access its source code via svn here:

Google Code web2bb-php52

The source has embedded BLOG example application.

Sergei