PHP Slim MVC Skeleton/Boilerplate Project

Installs: 70

Dependents: 0

Stars: 13

Watchers: 3

Forks: 8

Language: PHP

v1.0.0 2014-07-03 14:12 UTC


This is a project about MVC Skeleton/Boilerplate for the Slim Framework.

Install Composer

If you have not installed Composer, install it as following,

Install the Application

After you install Composer, run below command from the directory in which you want to install.

(assumed you install composer as /usr/bin/composer globally, or please replace composer with php composer.phar),

  composer create-project zacao/slimvc [app-name]

Replace [app-name] with the directory name of your new application, and then do as below steps:

  • Set your virtual host document root to the public/ directory.
  • Ensure folders under var/ directory are writeable for your web server user, such as log, cache, and temp.


your can download the latest code package from github directly to your local machine

Folder Structure

  • app
    • controllers - controller classes
    • models - model classes
    • views - template files
    • routers - Slim routes group by feature, should be named as feature_name.router.php
    • middlewares - Slim customized middlware classes
    • etc - configuation file, e.g. config.php
  • lib - your customized lib classes against with the official composer classes in PSR-4 standard
  • public - document root & the public assetic files, such as images, css and js
  • var - writable folder, such as log, cache, temp and so on


Here should be the main folder which stores your own codes, such as controllers, models, views, middlewares and so on.


Slim routes group by feature, and names in .router.php format. differenct with the Slim official example, we using <optional-namespace>\<class_name>:<method_name> format to define a router, against with uing Clouse in Slim official doc.


$app->get('/article/:id', 'ArticleController:get');
$app->delete('/article/:id', 'ArticleController:delete');
  1. router files are loaded & sorted in alphanumeric order, you can priority routers by proper file names, such as, 0.default.router.php, 1.products.router.php (Thanks Wout's comments here)

  2. you can also call contollers with namespace specified, e.g.

    $app->get('/admin/article/:id', 'Admin\ArticleController:get');

Stores controller classes files which defined in router. It MUST be one class per file, and the filename should be same as the controller class name.


class IndexController extends Controller
    public function index()
        $data = array(
            'title' => 'It works!',
            'content' => 'Just have fun with the Slim in MVC way now!'

        $this->render("index/index.phtml", $data);

As the Slim configurate format, please refer to the original Slim configuration doc


return array(
    'debug' => true,
    'templates.path' => BASE_DIR .'/app/views',
    'pdo' => array(
        'default' => array(
            'dsn' => 'mysql:host=localhost;dbname=test',
            'username' => 'root',
            'password' => '',
            'options' => array()

About the standard Slim middleware classes, Please refer to the original Slim middleware doc


The model classes should be here. You can implement the model classes as you like.


class ArticleModel extends Model
    public function delete($id)
        // TODO related DML lines here
        $dbh = $this->getWriteConnection();

        return true;

Template files in default Slim format, Please refer to the original Slim middleware doc


Put your customize classes files here, in this sample, we using PSR-4 as autoloading standard, please refer to composer.json and files under lib folder for detail.


"autoload": {
    "psr-4": {
        "": [
        "Slimvc\\": "lib/Slimvc"

There are Slimvc\Core\Controller, Slimvc\Core\Model sample classes created under the lib folder.


namespace Slimvc\Core;

abstract class Controller
    protected $appname = "default";

    protected function getApp()
        return \Slim\Slim::getInstance($this->appname);

    protected function getConfig()
        return $this->getApp()->container['settings'];

    protected function render($template, $data = array(), $status = null)
        $this->getApp()->render($template, $data, $status);


Here is the document root (.htaccess & index.php) and repository for public static assets, such as images, css and javascripts


Location for writable entires, such as logs, caches and temporary files



This project is released under the MIT public license.