MVC Framework

Published  Sep 07, 2012
Updated  Sep 10, 2012
0  3

Description

basic framework using an MVC design. you can use it locally or create your own project and import the files. an example is available in the download section.

Making your own project:[list=1]
[]Download the framework and extract it to your root web directory
[
]Create a new folder for your project, for the purpose of this example it is named "Example"
[]Create three new folders inside of the Example directory named "models", "views", and "controllers"
[
]Still inside of Example, create a new .htaccess file and paste

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

this will redirect all traffic to index.php with the exception of directories and files that actually exist. you may choose to remove the RewriteCond lines if you want exverything to be redirected
[*]Now create an index.php file inside of Example. In order for the framework to know where your project is, you must define a constant called 'USER_ROOT'

define("USER_ROOT", dirname(__FILE__));

next you will have to import the framework.

require 'PATH/TO/FRAMEWORK/index.php';

where "PATH/TO/FRAMEWORK" is the path to index.php of the file you downloaded in step 1. To test if everything is working properly open a web browser and navigate to the Example folder. You should get something similar to> Controller 'index' doesn't exist!
A fatal error has occured :(
[]Now that the framework has been imported correctly, it will start a bootstrap that will load all the necessary files. Once it has finished that it will look at the url in order to determine what controller to call. it is designed so that http://yourwebsite.com/user/search?id=231 will call the method 'search' of the controller 'user' with the arguments of 'array( id => 1)'
[
]With that, to create a very basic home page, make a new file called index.php inside of the controllers folder of Example
[]Next, create a new class named index that extends the class 'SunPHP\Controller' [note: the use of namespaces in libs is to avoid name collisions, however, res files are part of the global namespace as they are only imported if the user explicitly instructs the framework to include it using App::uses()] After making the class you will now have to create two new functions called 'construct' and 'toString()'
[
]Inside of the construct function make an echo statement that echoes anything you want
[*]Inside of the
toString function only type 'return CLASS;'
[*]Your file should now look something similar to

<?php
    class index extends SunPHP\Controller
    {
        public function __construct()
        {
            echo "Default page.";
        }

        public function __toString()
        {
            return __CLASS__;
        }
    }
?>

[*]Switch back over to your web browser and reload the page you opened in step 5 and you should get a message similar to> Default page.
[/list]

Download

Filename
Size
Date
Downloads
4.64 KB
Sep 07, 2012
234
4.65 KB
Sep 07, 2012
177

Comments

Sign in to comment.
Hawkee   -  Sep 08, 2012

Always nice to see some code tags though. It helps people see a reason to download it and try it.

 Respond  
sunslayer   -  Sep 08, 2012

@Hawkee i made an example in MVC.zip but this is designed to require multiple files in different directories that would be difficult to demonstrate clearly in a couple of code tags

 Respond  
Hawkee   -  Sep 08, 2012

Would be nice to see some code snippets demonstrating this.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.