Logo
  • PHP
    • HipHop / HHVM
    • Modern PHP
    • PHPStorm
    • LAMP
    • Laravel
    • Composer
    • PDO
  • JavaScript
    • node.js
    • AngularJS
  • CSS
    • SASS
    • “CSS4” (CSS level 4)
  • HTML
  • Git
  • LAMP
  • Vagrant
  • UI / UX
  • Architecture of …
  • Off-Topic
With ♥ from Berlin
December 10, 2014
Chris
PHP
25

MINI, an extremely simple barebone PHP application

PreviousNext

For my daily work I often needed to setup super-simple PHP applications, just some more or less static pages plus some dynamic pages with simple database calls, one or two simple forms and maybe a little bit of AJAX. You know, the typical agency stuff. This usually led to the question: Use a real framework or just mash some .php files together ? Building a simple structure from scratch was painfully stressful every time (and usually also very messy and unsecure), using a real framework was also annoying as setting them up and reading into the documentation to do the most simple tasks took lot of time. And let’s be honest, we probably all have gone through feature-hell in frameworks, I still remember trying to get decorators in Zend Framework 1 to work, for WEEKS, when a simple echo $myvariable would have done the job in seconds perfectly. So I’ve build a very very simple application skeleton, some might even call it a framework, that tries to do common PHP application tasks in the most simple way possible, in pure native PHP, without any bullshit around: MINI.

 

The features of MINI

  • an extremely simple, easy to understand barebone PHP application, for the most simple use-cases
  • has a very simple but clean structure
  • makes “beautiful” clean URLs (like example.com/car/show/17, not example.com/index.php?type=car&action=show&id=17)
  • demo CRUD actions: Create, Read, Update and Delete database entries easily
  • demo AJAX call with JSON response
  • tries to follow PSR 1/2 coding guidelines, so we have quite clean code
  • uses PDO for any database requests (so forget all the SQL injection / espacing stuff)
  • killer feature: comes with a PDO debug function that emulates and shows your SQL statements
  • commented code
  • uses only native PHP code, so people don’t have to learn a framework

 

The requirements of MINI

  • PHP 5.3+
  • MySQL
  • mod_rewrite activated

There’re copy & paste tutorials on how to activate mod_rewrite for Ubuntu 12.04 LTS, Ubuntu 14.04 LTS, EasyPHP on Windows, AMPPS on Windows/Mac OS, XAMPP for Windows and MAMP on Mac OS.

 

No mod_rewrite ? Then try TINY, the mod_rewrite-less version of MINI

Mod_rewrite can be installed installed easily on every server in the world. There’s absolutly no reason to rent “shared hosting” or “pre-setup servers” or similar stuff these days. Shared hosting is for beginners to upload HTML file etc. but definitly not for PHP development. DON’T DO SHARED HOSTING, it will only bring you into problems. You can get extremely cheap servers everything, without a contract, delivered in under 60 seconds. So “I cannot install X” or “I cannot setup mod_rewrite” is no valid excuse. Seriously. However, if you still don’t have mod_rewrite activated, then TINY, the mod_rewrite-less version of MINI might be worth a look. But keep in mind that this version is just a quick development tool, not intended for public release, see the Readme on GitHub for more information.

 

MINI 2 (the better one)

MINI has become quite popular, but I was never satisfied with the architecture, so I’ve written a similar version of MINI on top of the wonderful Slim micro framework and called it MINI2. Have a look on the official GitHub repo here.

 

The structure of MINI

mini-structureLet’s have a look:

We have the two main folders application (all application logic) and public (all the files that are accessable for the user). Note the .htaccess in the root of the script. Forget about any other folders that might exist (install stuff etc.) and the composer.json (totally optional, more on that later). For this paragraph it makes sense to open the files locally or on GitHub to read along.

What happens here ?

When the user enters the server, the .htaccess in the applications’s root folder will route him to /public/index.php. This also prevents any access to the application folder, making your application much more secure. The index.php loads the applications configs from /application/config/config.php (by default just the database logins and some automatically recognized path/URL settings) and MINI’s core logic from /application/core. It’s not important to know what exactly happens there for now.

Okay, MINI works like most other frameworks and uses URLs like this: example.com/songs, example.com/songs/editsong/17, example.com/subpage etc. instead of ugly messy index.php?xxx=111&yyy=222 URIs. The logic behind is simple: The first part after the domain (example.com) of the URL selects the controller(-file) in /application/controllers, the second part selects the method inside that file (so /songs/editsong will load /application/controllers/songs.php and call the editSong() method there). Extremely simple. If you only provide the first part, then the index() method will be called automatically, if you don’t provide the first part, then the home controller will be loaded and the index() method will be called automatically. In theory you can do whatever you want inside these methods to build your tool.

Let’s look at a real example: In the default demo setup the homepage (example.com) will load /applications/controllers/home.php (as explained above, code below in [1]) and call index() inside. In index() we simply load a header (/application/views/_templates/header.php), a footer (/application/views/_templates/footer.php) and some real content, in this case the content of /application/views/home/index.php (which is just a little bit of demo text). The principle behind should be clean, right ? Look into the files to get an idea.

Another example, this time one that uses data from the database: Look into /application/controllers/songs.php (code below in [2]): Same like before, but note this line:

$songs = $this->model->getAllSongs();

This simply fills a variable $songs with the results of the method getAllSongs() from the file /application/model/model.php! Self-explaning. In theory you can put whatever you want inside the model-file, usually methods that get, set or manipulate data in any way. In this case the method returns an array of data from the database. Now we can echo out the variable ($songs) inside the view file easily, like in listing [3] that shows how /application/views/songs/index.php looks like.

Extremely simple, right ? There are some more features, but the basic flow of MINI should be clear now. Feel free to experiment with the codebase and make sure you read the comments. Have a look into the header.php to see how CSS and JS is loaded and how links are generated. See the edit/delete/add-methods in the songs-controller to get an idea how CRUD operations are done here and how form handling flows.

 

Code listing [1]: /application/controllers/home.php

class Home extends Controller
{
    public function index()
    {
        // load view files
        require APP . 'views/_templates/header.php';
        require APP . 'views/home/index.php';
        require APP . 'views/_templates/footer.php';
    }

    // ...
}

 

Code listing [2]: /application/controllers/songs.php

class Songs extends Controller
{
    public function index()
    {
        // getting all songs and amount of songs
        $songs = $this->model->getAllSongs();
        $amount_of_songs = $this->model->getAmountOfSongs();

        // load view files
        require APP . 'views/_templates/header.php';
        require APP . 'views/songs/index.php';
        require APP . 'views/_templates/footer.php';
    }

    // ...
}

 

Code listing [3]: /application/views/songs/index.php (shortened)

<table>
  <?php foreach ($songs as $song) { ?>
  <tr>
    <td><?php if (isset($song->id)) echo htmlspecialchars($song->id, ENT_QUOTES, 'UTF-8'); ?></td>
    <td><?php if (isset($song->artist)) echo htmlspecialchars($song->artist, ENT_QUOTES, 'UTF-8'); ?></td>
    <td><?php if (isset($song->track)) echo htmlspecialchars($song->track, ENT_QUOTES, 'UTF-8'); ?></td>
    <td><a href="<?php echo URL . 'songs/deletesong/' . htmlspecialchars($song->id, ENT_QUOTES, 'UTF-8'); ?>">delete</a></td>
    <td><a href="<?php echo URL . 'songs/editsong/' . htmlspecialchars($song->id, ENT_QUOTES, 'UTF-8'); ?>">edit</a></td>
  </tr>
  <?php } ?>
</table>

 

Installation of MINI

The installation of MINI is explained on GitHub for sure. Beside the classic manual installation there’s also a one-line installation script for Vagrant that will automatically install Apache, PHP, MySQL, PHPMyAdmin, git and Composer, set a chosen password in MySQL and PHPMyadmin and even inside the application code, download the Composer-dependencies (optional), activate mod_rewrite and edits the Apache settings, download the code from GitHub and run the demo SQL statements (for demo data). This is 100% automatic, you’ll end up after +/- 5 minutes with a fully running installation of MINI inside an Ubuntu 14.04 LTS Vagrant box.

Everything is explained on GitHub’s MINI readme page.

 

This article was written quite a while ago (8 years), please keep this in mind when using the information written here. Links, code and commands might be outdated or broken.

Random articles

  • MINI2, an extremely simple barebone PHP application on top of SlimMINI2, an extremely simple barebone PHP application on top of Slim
  • New project: Building a naked PHP skeleton / boilerplate application from scratchNew project: Building a naked PHP skeleton / boilerplate application from scratch
  • Install MINI in 30 seconds inside Ubuntu 14.04 LTSInstall MINI in 30 seconds inside Ubuntu 14.04 LTS
  • SensioLabs, creator of Symfony and Silex PHP frameworks, gets $7 million capitalSensioLabs, creator of Symfony and Silex PHP frameworks, gets $7 million capital
  • PHPStorm: 42 Tips and Tricks (47min video talk by Mikhail Vink at Dutch PHP Conference 2015)PHPStorm: 42 Tips and Tricks (47min video talk by Mikhail Vink at Dutch PHP Conference 2015)
  • What’s new in PHPStorm 9What’s new in PHPStorm 9
  • Experimenting with HHVM at Etsy (Link)Experimenting with HHVM at Etsy (Link)
  • Dangerous Performance Myths in the Web (video talk by Thomas Lohner, PHPUG Latvia)Dangerous Performance Myths in the Web (video talk by Thomas Lohner, PHPUG Latvia)
  • How to setup / install PHP 5.6 on Ubuntu 14.04 LTSHow to setup / install PHP 5.6 on Ubuntu 14.04 LTS
bareboneframeworkminimini2PHP
Share this

25 Comments

  • Andrey
    January 12, 2022 10:42 am

    Hi, how to create new class ?

    Reply
  • Siegbert
    June 15, 2017 7:53 pm

    hi, when I rename the database from “mini” to as example: “mini_db” and change the Constant in config.php to it, why I get than these errors:

    Warning: PDOStatement::execute(): SQLSTATE[42S02]: Base table or view not found: 1146 Table ‘mini_db.song’ doesn’t exist in D:\UwAmp\www\PHP_Frameworks\mini_php-mvc\application\model\model.php on line 24

    Warning: PDOStatement::execute(): SQLSTATE[42S02]: Base table or view not found: 1146 Table ‘mini_db.song’ doesn’t exist in D:\UwAmp\www\PHP_Frameworks\mini_php-mvc\application\model\model.php on line 124

    Notice: Trying to get property of non-object in D:\UwAmp\www\PHP_Frameworks\mini_php-mvc\application\model\model.php on line 127

    Why PDO is looking now for a table named “mini_db.song” and not just for table “song” ??
    Is it not better not to restrict the database name to “mini” ?

    Reply
    • Kentut
      August 18, 2017 4:15 pm

      mini_db is your database and song is the table model.php trying to get (from database mini_db) which is does not exist, there’s no restriction on database name, you can change it to whatever you want

      Reply
      • Chris
        August 26, 2017 12:06 pm

        Sorry absolutly no idea what you want

        Reply
        • Kentut
          September 11, 2017 3:45 pm

          that’s not a question i’m just answering a question above “Why PDO is looking now for a table named “mini_db.song” and not just for table “song” ??
          Is it not better not to restrict the database name to “mini” ?”

          Reply
  • Mustufa
    April 21, 2017 1:23 pm

    Hey! How can i install MINI when i’m using Xampp Server and Adobe Dreamweaver for development. I really want to know this because im having an error.
    “Fatal error: Uncaught PDOException: SQLSTATE[HY000] [1045] Access denied for user ‘root’@’localhost’ (using password: YES) in C:\xampp\htdocs\mini-master\application\core\controller.php:37 Stack trace: #0 C:\xampp\htdocs\mini-master\application\core\controller.php(37): PDO->__construct(‘mysql:host=127….’, ‘root’, ‘your_password’, Array) #1 C:\xampp\htdocs\mini-master\application\core\controller.php(20): Controller->openDatabaseConnection() #2 C:\xampp\htdocs\mini-master\application\core\application.php(27): Controller->__construct() #3 C:\xampp\htdocs\mini-master\public\index.php(38): Application->__construct() #4 {main} thrown in C:\xampp\htdocs\mini-master\application\core\controller.php on line 37” !

    I’d really appreciate some help.

    Reply
    • Chris
      April 23, 2017 3:27 am

      Why would you do that ? Simply do exactly what the install tutorial says or use the auto-installer and everything will work very fine.

      Reply
  • sunco
    March 31, 2017 1:15 am

    Thanks for this, works great, easy to understand..

    I made some modifications to MINI3 – https://pastebin.com/HMpfvMBP

    Reply
  • ESTIVEN GARCIA PIEDRAHITA
    February 25, 2017 8:27 pm

    Hi, how can I add a login?

    Reply
  • Axel Weyer
    June 20, 2016 4:42 pm

    Hi Chris, thank you for the MINI project.

    How to have 2 parameters at a path ? Like “example.com/editsongs/17/thing” to call method editsongs(17,thing) ? This method with “/17/thing” doesn’t work

    Thanks

    Reply
  • imrandogra
    March 13, 2016 12:18 pm

    hi Chris

    mini is a great help to get comfortable with MCV. you have don marvelous. one question “extending function for pagination” please recommend any reading.

    Reply
  • Euclides Lavado
    February 16, 2016 1:38 am

    Hi Chris, in PHP-MVC, I added an external class in the “models” folder and I used it with “$var = $this->loadModel (“xternal_class’);”

    How I do that in PHP MINI ?

    Thanks in advance.

    Reply
  • yufieko
    December 3, 2015 1:03 am

    is it possible to make custom route on MINI ? if possible, how to achieve that ?

    thank you.

    Reply
  • Prathmesh Nalawade
    November 30, 2015 7:32 am

    Hi,

    I have url : index.php?id=64&age=23&xyz=abc&xyz1=abc1

    With this framework can we use this type of url, so that home controller of index page will load and it will have params like id,age etc

    Reply
    • Prathmesh Nalawade
      November 30, 2015 7:59 pm

      Hi, I had read the readme file, Everything works good but problem with query parameter of get string like index.php?id=64&age=23&xyz=abc&xyz1=abc1. The URL index.php/id=64/age=23/xyz=abc/xyz1=abc1 works fine.

      Reply
      • Chris
        December 1, 2015 2:25 pm

        Sorry I cannot give personal support, but have a look on how this framework works: mydomain.com/controllername/actionname/param1/param2/param3/ … will totally do the job. If you really need named parameters like age=32 then please use another framework. Mini is just for minimal purposes :)

        Reply
        • Prathmesh Nalawade
          December 2, 2015 6:29 am

          Hi Chris,
          Thanks for reply. I was not asking for any personal support, but wondering how get query string work with this framework. Any ways again thanks for response.

          Reply
  • Philip
    June 30, 2015 6:28 pm

    How do i use MINI to upload Images, i am having challenge uploading images. Kindly assist.

    Reply
  • Dino Baskovic
    February 13, 2015 8:08 pm

    So, is there an upgrade path to MINI from php-mvc? How easy or hard will it be to port my old projects?

    And if MINI2 is so great, are you saying we should not use MINI and wait for MINI2? Confused on both ends.

    Great that your product is evolving. Keep up the good work!

    Reply
    • Chris
      March 7, 2015 2:03 pm

      He Dino, MINI and MINI2 are quite different from architecture point, but do basically the same: MINI is a self-build barebone, MINI2 is also a barebone, but built on top of Slim, the popular micro framework (which unfortunatly comes without a structure, as it’s just one file).

      Good question, let me say it like this: MINI2 is totally okay for simple use cases, but if you need middleware, logging, unit tests, etc. then Slim/MINI2 might be better for you. :)

      Thanks too!

      Reply
      • Alex
        May 19, 2015 12:24 pm

        “MINI2 is totally okay for simple use cases” – You mean MINI?

        Reply
  • cody
    January 25, 2015 7:19 am

    yeah i love this frame work tini mini tiny. but the problem is you dont have a winy to rhyme with the projects. just kidding. thumbs up panique very easy codes to understand yeah very great oop. its just a perfect start for an mvc architecture guide. well commented. you ROCKKKKKKKKKKKKKK!!!! BOOMDAYAH.

    Reply
  • Daniel
    December 10, 2014 7:50 am

    Thank you, I’ll definitely check this out. Been looking for a very compact framework when I was starting with PHP, but there was nothing like that.

    I am already using your login/registration script and it works great, so I am sure this one will too!

    Reply

Leave A Comment Cancel reply

How to show memory usage (Ubuntu)

Always very useful: A simple command to show memory usage (Ubuntu / linux distros). cat /proc/meminfo   This article was

css3-chrome-font

Google rolls out Chrome 37, finally fixes horrible font-rendering

Google has rolled out Chrome 37 today, a legendary milestone that fixes one of the most annoying “bugs” in frontend

php

PHP 5.6 announced, statically typed (!) “new” PHP announced by Facebook devs

PHP 5.6 goes alpha PHP 5.6 has been announced for a while now, and these days things get serious (code-freeze,

harper reed about big data

Harper Reed – The magic and mystery of Big Data (30min video from Webstock’15)

Harper Reed is speaking, so nothing can go wrong. :) The former CTO of Threadless.com and Obama for America definitly

logo-internet-explorer

How to professionally test on old Internet Explorer versions

If you have (or want) to test your websites and applications on older versions of Internet Explorer, then there’s a

ubuntu-14.04-lts

First view: Ubuntu 14.04 LTS brings PHP 5.5 and Apache 2.4

Short first view on Ubuntu 14.04 LTS (= long term service): I just tried out the alpha1 version of the

github-logo-octocat

GitHub buys Easel.io, a code-free full website creator worth a look

GitHub has just aquired Easel.io (which is NOT easel.ly who offer something similar for infographics / powerpoint !), a powerful

hack-php

Wow! Facebook devs have rewritten and fixed PHP, releasing it as new language called “Hack” today

Exciting stuff is happening: Some years ago Facebook has released an early preview of HipHop, a virtual machine that precompiles

css4

Angelina Fabbro talks about “CSS4” in this excellent conference video

A very interesting talk about the future of CSS – let’s name it “CSS4” as we talk about spec level

microsoft-windows-azure-cloud-hosting

Microsoft’s Azure platform gives away high money prizes for “testing out” their cloud services

Disclaimer: DEV METAL does not get any money etc. for saying this, there is no affiliate link, no deal with

1/4

Categories

Search

mod-rewrite-ubuntu-14-04-lts
How to enable mod_rewrite in Ubuntu 14.04 LTS
php uk conference
Profiling PHP Applications by Bastian Hofmann (video from PHP UK Conference 2014)
Hacked french TV channel exposed passwords in TV interview (video, screenshots, links)
Create a fast, perfect and bootable 1:1 Windows backup (full clone of HDD) for SSD migration
php
How to install/setup latest version of PHP 5.5 on Debian Wheezy 7.0/7.1/7.2 (and how to fix the GPG key error)
php
12 tools for better PHP quality
symfony-framework-logo
SensioLabs, creator of Symfony and Silex PHP frameworks, gets $7 million capital
Beautiful, minimal WordPress theme ZUKI by Elmastudio (with 30% discount)
php
PHP 5.6 announced, statically typed (!) “new” PHP announced by Facebook devs
Awesome next-gen PS4 graphics in “The Order: 1886”
modern-php
Creator of Slim framework and “PHP – The right way” writes book: “Modern PHP”, available 2015
phpstorm 7.0 php
How to debug code on a remote server (or in vagrant box) with PHPStorm
ilovepreloaders – A tumblr collection of preloader animations
vagrant
How to copy Vagrant boxes (or duplicate them)
steam sale coupon voucher
-30% to -90% on Steam and Origin

Tags

apache bash centos composer conference coupon CSS debian fonts framework git GitHub hack HHVM HipHop HTML HTML5 IDE JavaScript JS LAMP laravel linux mod_rewrite MVC MySQL Nginx optimization PHP PHP 5.5 PHP 5.6 phpmyadmin PHPStorm security server SSD Ubuntu UI UX vagrant video virtual machine voucher VPS wordpress
Side-Project: Wordle-Solver:
www.wordle-helper.info

Pages

  • Privacy Policy
 
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie SettingsAccept
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT