Skip to content

albanafmeti/micro-framework

Repository files navigation

Usage of frameworks like Laravel, Symfony etc. makes us ( who love PHP ) start thinking and asking ourselves: Could we implement our own framework? In this article we are going to create our simple framework and understanding how can we build a framework with MVC design pattern using PHP.

Micro Framework

Introduction

Why would you like to create your own framework?

If you look around, everybody will tell you that it's a bad thing to reinvent the wheel and that you'd better choose an existing framework and forget about creating your own. But anyhow there are a few good reasons to start creating your own framework:

  1. To create a framework tailored to your very specific needs
  2. To experiment creating a framework for fun
  3. To prove the world that you can actually create a framework on your own
  4. To get a good experience in PHP

MVC architecture

The Model-View-Controller (MVC) is an architectural pattern that separates an application into three main logical components: the model, the view, and the controller. Each of these components are built to handle specific development aspects of an application. The Model component corresponds to all the data related logic that the user works with. The View component is used for all the UI logic of the application. Controllers act as an interface between Model and View components to process all the business logic and incoming requests, manipulate data using the Model component and interact with the Views to render the final output. For more on MVC design pattern read this article at SitePoint: The MVC design pattern and PHP. We are going to use MVC design pattern to build our framework.

Our Project

Instead of creating everything in our framework from scratch, we are going to use some external packages. To install these we are going to use Composer, a project dependency manager used by modern PHP applications. If you don't have it yet, download and install Composer now.

To continuously we are going to discuss the main components and features that our framework will have.

  1. Bootstraping. “bootstrap PHP code” means creating a bootstrapper that handles all the dynamic requests coming to a server and apply the true MVC (Model View Controller) framework so that in future you can change the functionality for each unique controller or application without changing the entire code or application.

  2. Routing. To understand what a router does, you must first understand what a rewrite engine is. A rewrite engine is software that modifies a web URL's appearance (URL rewriting). Rewritten URLs (sometimes known as short, fancy URLs, or search engine friendly - SEF) are used to provide shorter and more relevant-looking links to web pages. The technique adds a degree of separation between the files used to generate a web page and the URL that is presented to the World. If you want rewritten URLs, you need some kind of routing, as routing is the process of taking the URL, braking it into components and deciding what is the actual script to call. Routing is the process of taking a URI endpoint (that part of the URI which comes after the base URL) and decomposing it into parameters to determine which module, controller, and action of that controller should receive the request.

  3. Filters. Before executing an action of the controller, some filters can be applied to a route, which are functions to be executed before the action. In this filters we can make different validation and checks e.g. if the user is not logged in redirect to login page.

  4. Modules. It's a widget based feature, which helps us separating different parts of an application in classes with a special function, that will be executed in a specific part of a layout e.g. a widget showing the weather.

  5. For Database functions we are going to use a powerful ORM currently in the web, used by Laravel framework, Eloquent

  6. Regarding to Views we are going to use a powerful templating engine used by Laravel too, Blade.

The directory structure of our project is going to be like this:

/
+-- app
    +-- controllers
    +-- exceptions
    +-- libs
    +-- models
    +-- modules
+-- bootstrap
+-- config
+-- public
+-- resources
    +-- langs
    +-- views
+-- storage
    +-- cache
    +-- logs
+-- vendor
  • Inside the controllers directory are going to stay the controllers classes that will extend a parent class called Controller located under /app/libs.

  • Inside the libs directory are going to be different classes used to make the framework functional.

  • The exceptions directory is going to contain different exceptions classes extending the Exception class.

  • Inside the models directory will be the models classes of the framework that are going to extend an Eloquent class.

  • modules directory will contain some classes serving for creation of different widgets in the UI while creating for example an website.

  • bootstrap directory will contain a file called FrontController.php which serves like an engine and is going to make all the bootstraping part of the framework

  • We don't have to do with cache directory because it's necessary only for Blade templating engine.

  • logs directory it's going to contain log files for different application errors when it is in production mode

  • The public directory is accessible to all, and here is going to live the index.php file together with .htaccess, an Apache config file. In the public directory will stay all the public stuff like js, css and images etc.

  • In the langs directory are going to be some .ini files which are key = value pairs necessary for the languages of the framework.

  • In the views directory are going to stay all blade view files and layouts

  • cache is the directory where will be compiled files from Blade templating engine which we are going to talk later for. The logs folder may contain different log files.

  • The last directory vendor doesn't have to do with us, because it's necessary for downloaded packages and libraries via Composer.

Continues...

About

It's a PHP micro framework built using MVC design pattern.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published