Making your own MVC in PHP [Section 1] 12-01-2017, 06:21 PM
#1
Most current popular MVCs do have many features that could and may suit you though its full of bloat content that you just simply don't need. In this general tutorial I'll be walking you through making a simple Model View Controller in PHP. I'll be writing this tutorial for Apache2 with AllowRewrite set to All for website directory.
By default on ubuntu/debain servers you'll need to go to /etc/apache2/apache2.conf and ctrl+w search "/var/www" and set AllowRewrite from None to All
How this MVC will work is using an auto-loader function to combine all the files and make them runnable inside index.php
To start off we'll need to make the .htaccess file, How this works it takes everything after / and returns it as a GET request.
Now if you were to put in the index.php (Do not test this on a production server. Vulnerable to XSS Reflected.)
It will echo out what ever you put after domain.tld/blehblehkekbleh
Output: blehblehkekbleh
For this part we'll need to make a few directories even if they're left black in this section.
You'll need: class, controllers, views, and inc.
To finish off this tutorial we'll do the index, auto loader and route page so you can see progress.
It's requiring the auto-loader which will include all classes/controllers/etc.
The route is to get the correct page.
Now if you were to go to domain.tld you'd see "Hello world."
To add a new route you'd simply make another Route::Set
If you were to go to domain.tld/example you'd see "Hello world. 2"
The auto-loader use spl_autoload_register function to find all the classes, and is self explanatory. If you need help you can look at the documentation on PHP.net
With that it will look for all controllers and classes.
If you have any questions or need any help feel free to reply. I will continue to make more sections as I have time.
By default on ubuntu/debain servers you'll need to go to /etc/apache2/apache2.conf and ctrl+w search "/var/www" and set AllowRewrite from None to All
How this MVC will work is using an auto-loader function to combine all the files and make them runnable inside index.php
To start off we'll need to make the .htaccess file, How this works it takes everything after / and returns it as a GET request.
.htaccess
Code:
RewriteEngine On
RewriteRule ^(.+)$ index.php?url=$1 [L,QSA]
Now if you were to put in the index.php (Do not test this on a production server. Vulnerable to XSS Reflected.)
temp index.php
Code:
<?php
echo $_GET['url'];
?>
Output: blehblehkekbleh
For this part we'll need to make a few directories even if they're left black in this section.
You'll need: class, controllers, views, and inc.
To finish off this tutorial we'll do the index, auto loader and route page so you can see progress.
index.php
Code:
<?php
require_once "./inc/autoload.php";
require_once "./inc/route.php";
?>
The route is to get the correct page.
/inc/route.php
Code:
<?php
Route::set('index.php', function() {
echo "Hello world.";
});
?>
To add a new route you'd simply make another Route::Set
Code:
Route::set('example', function() {
echo "Hello world. 2";
});
The auto-loader use spl_autoload_register function to find all the classes, and is self explanatory. If you need help you can look at the documentation on PHP.net
/inc/autoload.php
Code:
<?php
function autoLoaderInclude($includeName){
if(file_exists(strtolower('./class/'.$includeName.'.class.php'))) {
$pathClass = strtolower('./class/'.$includeName.'.class.php');
require_once($pathClass);
}
if(file_exists(strtolower('./controllers/'.$includeName.'.php'))) {
$pathController = strtolower('./controllers/'.$includeName.'.php');
require_once($pathController);
}
}
spl_autoload_register('autoLoaderInclude');
?>
/class/route.class.php
Code:
<?php
class Route {
public static $validRoutes = array();
public static function set($route, $function) {
self::$validRoutes[] = $route;
if($_GET['url'] == $route) {
$function->__invoke();
} else {
echo "Route not found";
}
}
}
?>
If you have any questions or need any help feel free to reply. I will continue to make more sections as I have time.
(This post was last modified: 12-01-2017, 08:01 PM by Esoterith.)