mercredi 6 mai 2015

How to load controller / create router for php mvc framework

I am writing a light weight php mvc framework for my portfolio and as a barebone setup for my future developments. I am stuck now because I am trying to use PSR-4 autoloader through composer. I understand the concept of PSR-4 etc but I am wondering how should I handle the routing.

My folder structure is:

--|-Root---
  index.php
  composer.json
-----|-application------
--------|-controller----
--------|-model---------
--------|-core----------
-----------|-baseController.php
-----------|-Application.php
-----------|-baseView.php
-----------|-baseModel.php
--------|-view----------
--------|-config
-----------|-config.php
-----|-vendor------
--------|-autoload.php
-----|-assets------

I am fairly good with php but writing an own mvc framework is a hard challange, even bigger because I never used PSR standards before.

Now my questions are:

  1. Should I use router at all?
  2. If yes then what pattern should I use to acomplish that.

So, in my index.php I define all constants to store directories like ROOT, APP, VENDOR. I then load vendor/autoload.php generated from my composer.json. After autoload.php I load and initiate my config by calling this code:

if(is_readable(APP . 'config/config.php')) 
{
    require APP . 'config/config.php';

    //Initiate config
    new AppWorld\Confyy\config();
} 
else 
{
    throw new Exception('config.php file was not found in' . APP . 'config');
}

After config I setup application environment and then I initiate my app by calling:

// Start the application
new AppWorld\FrostHeart\Application();

Then my baseController is very very thin:

<?php
namespace AppWorld\FrostHeart;
use AppWorld\FrostHeart\baseView as View;

abstract class baseController {

    public $currentView;

    public function __construct() {

        //Initiate new View object and set currentView to this object.
        $this->currentView = new View();

    }

}

And this is my baseView.php:

<?php
namespace AppWorld\FrostHeart;

class baseView {

    public function show($file, $data = null, $showTemplate = 1) {

        if($data != null) {
            foreach($data as $key => $value) {
                $this->{$key} = $value;
            }
        }

        if($showTemplate === 1) {
            require VIEW_DIR . header . ".php";
            require VIEW_DIR . $file . ".php";
            require VIEW_DIR . footer . ".php";
        } 
        elseif($showTemplate === 0) 
        {
            require VIEW_DIR . $file . ".php";
        }

    }

}

my config.php:

<?php
namespace AppWorld\Confyy;

class config {

    public function __construct() {
        $this->application();
        $this->database();
    }

    public function application() {

        /**
        * Define application environment, defaults are:
        * 
        * development
        * live
        * 
        */

        define("ENVIRONMENT", "development");

        //Define base url
        define("BASE_URL", "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);       
        //Define default controller
        define("DEFAULT_CONTROLLER", "landing");
        //Define default method
        define("DEFAULT_METHOD", "index");    
        //Define controllers directory
        define("CONTROLLER_DIR", APP . "controller/");
        //Define views directory
        define("VIEW_DIR", APP . "view/");

    }

    public function database() {
        //Define DB Server
        define("DB_SERVER", "localhost");
        //Define DB User 
        define("DB_USER", "root");
        //Define DB Password
        define("DB_PASSWORD", "");
        //Define DB Name
        define("DB_NAME", "test");
    }

}

and at last my composer.json autoload section:

"autoload": {
        "psr-4":{
            "AppWorld\\": "application",
            "AppWorld\\Conffy\\": "application/config",
            "AppWorld\\FrostHeart\\": "application/core",
            "AppWorld\\Controls\\": "application/controller"
        }
    }

So how should I implement a router that routes the URL requests and load correct files?

Thanks!

Aucun commentaire:

Enregistrer un commentaire