Wednesday, July 1, 2015

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

No comments:

Post a Comment