Skip to content

Latest commit

 

History

7 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

janpa-logo

Janpa is an ultra lightweight PHP MVC framework. Initially created for my school project I have decided to published it due to the fact that most of the frameworks contain a lot of redundant utilities that inevitably slow down your application. Whole janpa framework have around 600 lines of code. Framework uses built-in library that contains routing and few super classes.


Installation

This version do not contain any frontend pages :(

  1. Clone repository
  2. Setup your server and drop all files there
  3. Change htaccess files name from htaccess.txt to .htaccess
  4. Update your config.ini file for database connection
  5. Delete example controllers & models (optional)
  6. Done, ready to go!

Here's how the directory looks like. Htaccess isolates app directory from unwanted access.

The application directory contains everything, models, controllers and the most important config.ini file where database password is stored. Public directory is a storage for our images and stylesheet and it doesn't have htaccess making it accessible for everyone. Index in main directory is our routing page we will be writing all paths there.

  • app
    • controllers
      • BlogController.php
      • UserController.php
    • lib
      • Controller.php
      • Input.php
      • Model.php
      • QueryBuilder.php
      • Router.php
      • Time.php
    • model
      • Blog_model.php
        • User_model.php
    • view
      • index.php
    • htaccess
    • config.ini
  • public
    • headers.php
    • css
      • style.css
  • htaccess
  • index.php <- our routing page

I created a schema that shows how this thing works. schema

Let's go through that schema with with code examples

  1. User uses some kind of interaction with server, user may send input like POST request or just type server link in browser. For example let's take a look at user that trying to access article at our blog page.

webaddress/article/1

  1. Router checks if there is a route like "/article" in our main index file which is our routing page.
foreach ($this->routes as $route) {
  if ($route->path == $path) {
    // route found - code continues
  }
}
// if route is not found
echo "<h1>Page not found</h1>";
  1. Every route have optional variable for securing
  $router->Map("/new_post", "BlogController/NewPost", true);
  $router->Map("/login", "UserController/Login");

In this example route "/new_post" is secured by third argument (true) given in maping function (by default its set to false)

  1. Login is ingrained deep into routing. It means that if you want to make some kind of login system on your webpage you have to use session["user"] to make it through router security without permision denied or editing router code.
if($route->secure) {
  if(!isset($_SESSION["user"])) {
    echo "<h1>Permision denied</h1>";
    header("Location: /login");
    die;
  }
}

I know this isn't the best idea but it works so Im not changing it for now.

  1. Creates instance of class from routing class name and includes input class as basic Controller library.
$controller = new $route->controller_name;
$method_params = array();
foreach($route->params as $id => $param) {
    if(!empty($full_path[$id-1])) {
        array_push($method_params, $full_path[$id-1]);
    } else {
        array_push($method_params, null);
    }
}
call_user_func_array(array($controller, $route->function_name), $method_params);
die;

In php you can create class instance just of its name given by string.

  1. Using class function given in index.
//index route
$router->Map("/article/id", "BlogController/ViewArticle");
 
//this function gets called from BlogController
public function ViewArticle($id) {
    $this->load_model("Article_model");
    $article = $this->Article_model->get_by_id($id);
    $this->render("article", array(
        "article" => $article,
    ));
}

In this controller function we use "Article_model" to load article from database with our id given in GET method, at the end rendering article view with our article data/object.

  1. Loading models into our controller.

After checking if file exists it requires once so that it can create a new class object containing given model.

// example used in BlogController
$this->load_model("Blog_model");

//Function simply checks if model exists and then creates instance of it in given controller.
function load_model($model_name)
{
    if (file_exists($_SERVER["DOCUMENT_ROOT"] . "/app/model/$model_name.php")) {
        require_once $_SERVER["DOCUMENT_ROOT"] . "/app/model/$model_name.php";
    } else {
        echo "Model named : $model_name not found";
        die;
    }
    $this->$model_name = new $model_name;
}
  1. Model and QueryBuilder
class Model
{
    function __construct()
    {
        $this->qb = new QueryBuilder();
    }
}

Every model inherits from "mother model" class database connection so it's not establishing connection for every model.

Query builder itself is big, 200 lines of functions we can use to interact with database. Even though I created it myself sometimes I have to look there for a function that I'm not sure is there or I don’t remember how it works. I'm not going to make a querybuilder documentation, I don’t have time and patience…

Model examples :
  1. Our example blog model which returns article object from database.
function get_by_id($id) {
  $this->qb->select("posts");
  $this->qb->where(array(
      "id" => $id,
  ));
  return $this->qb->execute();
}

//after loading model inside controller we can do something like that
$article = $this->Article_model->get_by_id($id);
  1. Again blog model example. This one deletes article from database by given id.
public function delete($id) {
  $this->qb->delete("posts");
  $this->qb->where(array(
      "id" => $id,
  ));
  return $this->qb->execute();
}

//usage
$this->Blog_model->delete($id)

About

Ultra light weight PHP MVC framework

Resources

Stars

2 stars

Watchers

2 watching

Forks

Releases

Packages

Contributors

Languages