Showing posts with label front controller. Show all posts
Showing posts with label front controller. Show all posts

Wednesday, July 1, 2015

myFrontController v0.2

Usually the continuation of a good movie is bad, hopefully my version 0.2 of myFrontController is a real progress.


I moved the files inside specific folders like: controllers, model, login, admin etc. Doing this means I need to implement a more complex autoloading function.

$folders=array('\\controller\\','\\model\\','\\login\\','\\view\\');
   
    foreach($folders as $folder){
       
       if(file_exists($dir . $folder . $class . '.class.php')){
            require_once $dir . $folder . $class . '.class.php';
        }


It is not covering some situations for this reason you may see some "include" in some classes, but I promise to solve the issue in v0.3

I am trying to start using comments in the style used by PHP Documentor. Step by step I hope to get used to write them. An  IDE would probably automatically generate the comments skeleton but at the moment I am using Notepad++

In the image below it is more or less described how my website works and a little on how I implemented the separation of concerns following the MVC design pattern ideas.



Changes from version 0.1 of myFrontController:

added DBCon.class.php - handles connection to database and fetching data

added StaticPages.classes.php -
added BlogModel.classes.php

- replace & with & in the links

----------- database --------------------------------
shift from SQLite to MySQL
create database "myblog"
create table  static_pages
alter table blogposts add column title varchar(255)


----------------------------------------------------------------------------------------------------------------------------------------
added basic login module (based on cookies) to the have an Admin view from where you can add new posts
added:
index_admin.php
/controller/Admin.class.php
/login/LoginUser.class.php

--------------------------------------------------
login module explained:
    first I hardcoded the username and passord inside the static function validateUserPass of the class LoginUser
    ---
    static public function validateUserPass($user,$pass){
            if(($user=='admin')&&($pass=='1234')){
                return true;

    ---

    From a form, username and password are sent via POST method using hidden fields (login.php):

            <form action="index.php" method="POST" >
                Username <br>
                <input type="text" name="username">
                <br>Password <br>
                <input type="text" name="password">
                <input type="submit" value="Login">
                <input type="hidden" name="controller" value="Admin">
                <input type="hidden" name="action" value="validateLogin">
            </form>

    ---
    the hidden values indicate controller name "Admin" and action "validateLogin"

    if the user and password match the ones hardcoded than we set a cookie containing an md5 hash of the user name + a secret word.

     setcookie('PageLogin', md5($user.self::$secret_word));
     setcookie('PageLoginUser', $user);


The files can be downloaded from this link:  https://drive.google.com/file/d/0B4lszAGYHn-dTXNOX2psd2QweGc/view?usp=sharing

myFrontController v0.1

NOTE: I am telling here how I got from point A to point B, not that it is the recommended path.

I started thinking on my Front controller based website from the point of view of a link containing a query string, like this one:

 "https://en.wikipedia.org/w/index.php?title=Main_Page&oldid=664887982"



So the query says that the request will be processed by the file index.php and the $_GET['title'] will be set with the value "Main_Page" and the $_GET['oldid'] will be set with value 664887982.

my links will look  like this (under ..\EasyPHP-DevServer-14.1VC9\data\localweb  I created the folder "myFrontController"):

'http://127.0.0.1/myFrontController/index.php?controller=Home&action=render'

You can read this link like this: index.php should process this request and call the method "render()" of the class "Home"

I will create an index file, a FrontController class, and other controller classes for each functionality I want to implement: Home page, Blog listing page. I will add also a folder called "templates" to store... well the templates.
I created a header and a footer and included them in the index.php

include("templates/header.php");

FrontController::set_controller();
 

include("templates/footer.php");


 OK, so let's move on to the actual FrontController class: it contains a static class called  setController() which reads the values for 'controller' and 'action' from $_GET[] and dynamically create an instance of the specified class and calls the method


            if((!isset($_GET['controller']))||(!isset($_GET['action']))){
       
                echo "do nothing GET";
            }else{
       
                //call controller class for GET method
                $obj= new $_GET['controller']();
                $obj->$_GET['action']();

                   


The second functionality that I want to implement is to list the blog post. I am using SQLite to store the information. You should execute the file "initializeSQLite.php" (you can just navigate to it on your browser) to create an SQLite database, create the tables BlogPosts and store information.

In the Blog.class.php is found the code for reading the database and generate the output.

The third functionality is adding a new post, the link is this one:   '?controller=Blog&action=new_post_entry'

The functionality is done it two steps:
- first step include "templates/new_post_entry.php" which contains a form where you can fill the title of the post, author, post content and a Submit button.
-second step when clicking the Submit button the request goes to index.php (again). together with the data the form contains 2 hidden fields :

     <input type="hidden" name="controller" value="Blog">
    <input type="hidden" name="action" value="postOnBlog">


These two values indicate to FrontController which method/class will handle the request.

Below is the list of files at the end.
You can download the code from here:  https://drive.google.com/file/d/0B4lszAGYHn-dVFh0X0E2NV9BLW8/view?usp=sharing