Skip to content

This is a php framework for learning purposes and usage.

License

Notifications You must be signed in to change notification settings

ShiniDev/Sapling

Repository files navigation

Sapling

A small PHP framework that implements MVC design pattern. This framework is created with the intent to give the author knowledge about php and at the same time be usable for the community.

Installation

composer create-project shinidev/sapling directory

Configuration

.envtemplate

You can edit the .envtemplate file to correspond to your database configuration, after editing it create a copy and name it .env. It is what Sapling reads for your database configurations. You should not include your .env file when uploading your repository to github.

config/Debug.php

In here you can enable Sapling's error reporting. Sapling's error reporting is on by default. DEVELOPMENT, when true displays error. STRICT, when true kills the application if there are errors. Note, that this is seperate from php error reporting, if you want to enable php error reporting go to your .env file and set DEVELOPMENT to "TRUE".

config/Routes.php

In here you can set your base url, default controller, default function and error page directory.

Usage

src/App/Controller

This is where you're gonna load your models and views.

Loading a Model

$this->loadModel('modelFile', 'optionalname')

Using a loaded model

$this->ModelFile->function() or $this->OptionalName->function()

Loading a view

$this->loadView('viewFile', 'optionalData')

src/App/Model

This is where you're going to communicate to your database. Model classes is recommended to extend the QueryBuilder class.

Example Insert Query

    $this->table('tablename'); // sets the table to insert data to 
    $this->insert(['columns'], ['datas']); // tells what columns and corresponding data
    // Insert only needs a table name

Example Select Query

    $this->table('tablename'); // sets the table to get data from
    $this->whereMany(['columns'], ['values']); // Sets the where clause based on the given columns and values
    // or
    $this->whereSpecific('column', value, '!='); // Appends and sets the specific column and its value one by one.
    // or
    $this->whereManual("WHERE column = ?", [values]); // Set the where clause manually.
    $this->join('left', 'tablename', 'on condition'); // Joins tables
    $this->order('column'); // Order by column
    $this->limit(100); // Limits result
    $this->select(['columns']); // Selects columns
    // Select only needs a table, all the others are optional

Example Update Query

    $this->table('tablename'); // sets the table to get data from
    $this->whereMany(['columns'], ['values']); // Sets the where clause based on the given columns and values
    // or
    $this->whereSpecific('column', value, '!='); // Appends and sets the specific column and its value one by one.
    // or
    $this->whereManual("WHERE column = ?", [values]); // Set the where clause manually. 
    // It is necessary to have always setted the where clause or else Sapling will display error
    $this->update(['columns'], [values]);

Example Delete Query

    $this->table('tablename'); // sets the table to get data from
    $this->whereMany(['columns'], ['values']); // Sets the where clause based on the given columns and values
    // or
    $this->whereSpecific('column', value, '!='); // Appends and sets the specific column and its value one by one.
    // or
    $this->whereManual("WHERE column = ?", [values]); // Set the where clause manually. 
    // It is necessary to have always setted the where clause or else Sapling will display error
    $this->delete();
    // Deletes rows based on the where clause that is setted.

Getting the builded query

    $this->getLastQuery(); // returns a string

Url Helper

Getting the base url

Url::baseUrl();

Redirecting

Url::redirect(url);

Passing parameters to controller

  http://localhost/Sapling/controller/method/param1/param2/param3
  // The url above passes 3 parameters to a controller function.
  public function test(param1, param2, param3); // Your controller method

Loading resources

Loading a css file in a view php

href="resources/css/test.css"

Loading a js file in a view php

'<script src="resources/js/test.js"></script>'

Notes

Test.php controller and TestModel.php only exists to serve as an example on how to use a controller and a model in Sapling. Database are not needed as long as you don't load a model in your controller.

Current Features

  • Url Routing
  • Security against script access
  • Secured database credentials
  • Simple debugger
  • Flexible query builder
  • Simple folder structure
  • Easy to use and understand
  • Documented

Features to learn/add

  • Better routing implementation, similar to of Laravel or Codeigniter
  • API implementation