Git Product home page Git Product logo

mvc-2's Introduction

PHP MVC Boilerplate

Overview

PHP MVC boilerplate with user authentication, basic security and MySQL CRUD operations. Framework was developed during the final year of university. It was used for some private projects, however I highly suggest you to use Laravel or some other popular framework for your work.

Requirements

  • Web server: Apache with mod_rewrite enabled
  • Database server: MySQL
  • PHP 7.x

Optional

  • ApiGen / phpDocumentor
  • PHPUnit

Login operation

If you imported database data from sys/db/dump.sql, you could authenticate with following credentials:

If you're manually adding user to users table, don't forget to append salt from sys/Config.php before hashing with SHA-512 algorithm.

Login form

CRUD operations

Each database table should have appropriate model file. For example, table tasks have app/models/TaskModel.php. There you need to hardcode table name in protected $tableName property and eventually add new functions. Provided functions with basic model are:

  • read
  • readAll
  • create
  • update
  • delete

CRUD operations

Router

All routes should be placed inside routes.php. Each route must have following properties:

  • Name of the controller whom the route belongs to
  • Name of the controller's method (the route callback)
  • Request URI, represented via PCRE

For example, if we have following code:

...
new Route('Home', 'index', '|^/?$|'),
...

it means that when user visits URI which matched RegEx |^/?$|, index.php will instantiate HomeController.php and call his index method.

RegEx cheat sheet

Route Regex
/ |^/?$|
users/ |^users/?$|
users/create/ |^users/create/?$|
users/update/15/ |^users/update/([0-9]+)/?$|
users/delete/4/ |^users/delete/([0-9]+)/?$|
store/iphone-8-64gb/ |^store/([a-z0-9]+(?:\-[a-z0-9]+)*)/?$|
Anything |^.*$|

Security

Framework provides basic security mechanisms.

SQL injection

Model class uses prepared statements and Database class uses PDO's DSN charset parameter to set connection encoding to utf8. It is impossible for an attacker to inject malicious SQL this way. For providing defense in depth, you can use input validation - for applications that demand higher level of security, I use input validation not only in PHP, but in MySQL also (via triggers).

XSS

For basic XSS protection (e.g. when we need to insert data in HTML body) you can use Security class. For example, if we need to insert $DATA['user'] in our HTML, we would use following code:

...
<p><?= Security::escape($DATA['user']); ?></p>
...

However, that wouldn't protect us if we needed to insert data inside <script> tag, for example. Visit OWASP page for further instructions. If you want to use third-party library for defense in depth, HTML Purifier is a good one. For API responses in JavaScript, don't forget to use safe JavaScript functions and properties (e.g. element.textContent, jQuery .text() function etc.) when you need to populate the DOM.

Data exposure

This framework provides user authentication functionallity, however if you intend to use it, in order for your users to be protected, you need to use HTTPS (HTTP + SSL). Without SSL encryption anyone could intercept the transmission from your browser to the server. Today, this is completely free thanks to initiatives like Let's Encrypt CA. I highly advise that you use HTTPS-related mechanisms like HSTS and HPKP as well.

Documentation

Great tool for generating documentation is ApiGen. You can install it via Composer (I prefer doing it globally) and simply run:

apigen generate --source MVC --destination MVC/docs
firefox docs/index.html &

Due to the recent problems with ApiGen and PHP 7.2, I used phpDocumentor as an alternative. Install phpDocumentor and run:

phpdoc --directory MVC --target MVC/docs

mvc-2's People

Contributors

mmilanovic4 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.