I have been laravel user for a year and half, since Laravel 4 beta was published. I love how it handle things with simple and elegant syntax, make a lot difficult task done faster, and easier. Laravel is great when I need to build middle-to-large project. But then I feel it is too bloated when I work on simple project, so I start looking at simpler solution using micro-framework like Slim or Silex to build my simple project.

I start with Slim and start to search composer package to create simple MVC environment with Slim. Since Slim only provide dependency container, router and some basic but nice feature, including the View part, I need to find a way to build the Model, and the Controller part to work nicely with Slim.

The Model Part

Due to my familiarity with Laravel environment, I decide to use Eloquent ORM for the model part. Eloquent offer a lot of easy-to-use and powerful feature. We can easily register eloquent instance to the Slim container, so we can make them work together.

use Illuminate\Database\Capsule\Manager as Capsule;

$config = array();
$config
['database'] = array( 'driver' => 'mysql', 'host' => 'localhost', 'database' => 'database', 'username' => 'root', 'password' => '', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', ); /** * Instantiate Slim framework */ $app = new Slim\Slim(); $app->hook('slim.before', function() use ($app, $config){ try{ /* * Register Eloquent as singleton to slim container * since we will use the same instance across the request cycle */ $app->container->singleton('db',function(){ return new Capsule;; }); $app->db->addConnection($config['database']); $app->db->setAsGlobal(); $app->db->bootEloquent(); }catch(PDOException $e){ /** Do some stuff to handle exception */ } });

The View Part

Fortunately the creator of Slim already provide Slim-Views package, that made Slim have the View part of MVC which support Twig and Smarty natively, I prefer to use Twig for personal reasons, but you can switch to smarty or build custom Views as you wish.

/**
 * Initialize Slim application
 */

$view = $app->view();
$view->parserOptions = $config['twig'];

$view->parserExtensions = array(
    new \Slim\Views\TwigExtension(),
);

The Controller Part

Recent Slim release now support routing to the controller method instead only route to closure. Here, I am using Slim-Facades, which provide simple static API to access Slim features and link the Slim router to the controller, so we can work with Slim in Laravel like environment :D. You can point route path to controller similar to Laravel route

Route::get('/login', 'Admin\AdminController:login')->name('login');
Route::get('/logout', 'Admin\AdminController:logout')->name('logout');

Route::get('/', function(){
    View::display('welcome.twig');
});

Additional Package

As almost all of my application need an authentication system, so I decide to add Cartalyst Sentry as authentication provider. You can also add and configure more package via composer.json

The SlimStarter was built

Finally, after few hours mixing and matching the composer package, I write a glue code to make Slim and other package work together in MVC environment, you can grab it on my github repository and start writing your MVC application with Slim

https://github.com/xsanisty/SlimStarter

It still in early stage of development, and need many improvement, architectural design and unknown bug, but at least it works for now 😀

Configuration

You can configure SlimStarter by creating new configuration file or edit the existing in app/config path. All configuration file will be loaded on while SlimStarter is bootstrapped.

Routing

Route can be configured in app/routes.php, similar to Laravel routing

/** get method */
Route::get('/', 'SomeController:someMethod');

/** post method */
Route::post('/post', 'PostController:create');

/** put method */
Route::put('/post/:id', 'PostController:update');

/** delete method */
Route::delete('/post/:id', 'PostController:destroy');

/** route middleware */
Route::get('/admin', function(){
    //check user login or redirect
}, 'AdminController:index');

/** Route group */
Route::group('/book', function(){
    /** GET /book/ */
    Route::get('/', 'BookController:index');

    /** GET /book/:id */
    Route::get('/:id', 'BookController:show');

    /** GET /book/:id/edit */
    Route::get('/:id/edit', 'BookController:edit');

    /** PUT /book/:id */
    Route::put('/:id', 'BookController:update');

    /** DELETE /book/:id */
    Route::delete('/:id', 'BookController:destroy');

});

/** Route resource */
Route::resource('/book', 'BookController');

 Model

You can write model in anywhere as long as the directory is registered in composer json, but to be more clear, let’s put it in the app/models directory. Since Eloquent is used as the ORM, it has almost all of Eloquent feature, but please noted that some of Eloquent features are not included to keep the application size as small as possible, method like remember(), validate(), and paginate() will throw an exception when invoked.

<?php

Class Post extends Model{}

 Views

All template and views file remain in app/views directory, and can be rendered via View::display() method

View::display('template.twig', $data);

You can try the live demo on shared hosting here or pagodabox cloud hosting here.

I’d love to hear your tough, suggestion, advice, and critics.