0

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!

  • 3
    Your framework is full of bad practices, including : 1) Global state 2) Breaking of Separation of Concerns 3) Breaking the SRP principle. And why do you write your own framework? Framework developers spend many years to develop a stable framework. The explanation to the problems you described in your post can be found in any popular framework. Namely : Laravel, Symfony or Zend. You can simple take a look at their API (documentation) and see how they do that. – Yang May 06 '15 at 17:38
  • What can I say? Yes, you should use routing. But on the other hand, you are still not showing you fully understood the concept of OOP (if I look at your config object in particular: You instantiate it to execute code that defines global constants, without any useful means of using this object itself, and the drawback of not being able to instantiate it again because that would trigger PHP errors when trying to define an existing constant again). The Aura framework has a nice Router component, and I'd suggest you use it.https://github.com/auraphp/Aura.Router/tree/2.2.2#readme – Sven May 06 '15 at 19:41

0 Answers0