Showing posts with label namespaces. Show all posts
Showing posts with label namespaces. Show all posts

Thursday, July 2, 2015

myFrontController v0.3

Note: The blog posts are written based on the notes I make on the readMe.txt file. Some the actions where "to do" items which were implemented later, for this reason you may see text like "I should do that"

Major improvements in v0.3
- autoloading function
- routing
- give up admin/index  (unique point of entry in the application)
- improvements to the login flow

1. Autoloading
 As you could see in the post about version 0.2 there are some issues with the autoloading function. I decided to improve this and even more use a popular standard PSR-4. My autoload function is inspired from this one:  http://www.php-fig.org/psr/psr-4/examples/  - just that I do not have the path  /src/ directory. I  started using also  namespaces in order to  to respects the PSR-4 standard and maybe later I will be able to use packages built by other people together with myFrontController CMS.

My vendor\package combination of namespaces is :  CPANA\myFrontController ,under this I have added :

CPANA\myFrontController\controller
CPANA\myFrontController\model
......

I had to change some code in my FrontController class as it seems there is a known bug (weird behaviour ) :      when I try to dynamically create the name of the class  "new $_GET['controller']()" I get message that the class cannot be found
 Here are some links about this subject:
http://stackoverflow.com/questions/6150703/php-dynamic-instance-with-not-fully-qualified-namespaces
    https://bugs.php.net/bug.php?id=51126
       
This is the code to  solve the problem:
  
                $class_name=__NAMESPACE__ . '\\'. $_GET['controller'];
                $obj= new $class_name();
                $obj->$_GET['action']();

2. Routing

I started reading the Symfony Book and I  implemented a routing system inspired from the one used in Symfony.
The logic to call the correct function will be :
    1. read the requested URL ($_SERVER['REQUEST_URI']), and exclude the /myFrontController
    2. verify in the \config\route.xml if there is any route matching -> if yes than read from \config\route.xml the function and call it.
            -> if no redirect to 404 page - created new controller class called "PageNotFound.class.php"

So I did the following:

1. created \config\route.xml file to store routes and matching class and method to be called
2. modified FrontControll class
3. added .htaccess from Symfony and replaced to redirect to "index"    instead of "app"                                                               

3. Give up admin/index

-in order to use just one front controller (not one for normal user and one for admin user) some changes should be made (also taking in account the new routing system)
-this will involve changing the way the pages are rendered (templating?) maybe request and response objects
---->>>
-new class \view\Render.class.php
    -it contains a static property $content  which is echoed in content.php  and also $menu which depends on being an admin or normal user
    -and a method which renders the main_template.php  (which imports content.php and menu.php or menu_admin.php)
    -each time we want to change the output, I change the value of the $content and call the Render::renderPage()
   
4. Login

I modified the LoginUser.class.php and also the Admin.class.php

-in Admin.class.php the function "renderLogin" it is called both when clicking "Admin" button and also when clicking on the Submit button
-in Login.class.php I added the static function validateLoginAdmin() which is used to check which menu (admin or normal user one) should be displayed. at the moment I call it on each specific class (Home, Blog) but it should be moved in to Render




5. Overview
At this point if you want to see the Home page the flow of the application is the following:
-you are redirected to index.php
-the front controller searches the path in router.xml and calls the controller (the needed method for the job)
-the method getHome() from StaticPages class is called from controllers __contruct(), which furher calles fetchStaticInfo($pagetype) from DBCon class. The information is fetched from database
- the render() function from controller class is called




The files can be downloaded at this link.