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.
I tried this four times, and all I gave is a “checking connection” at the install.php script. I have HHVM 3.4.1 (tested with 3.3.0 and 3.0.1 too), nginx 1.6.0 and MariaDB 10.0. Double checked the parameters and all is fine (it works well with slim solo and PDO). Maybe the whole install (I mean .htaccess) works with Apache only. 8-( Bad deal.
I am not check it using hhvm and nginx yet, but will try to build a vagrant box to test on different environment
— edit —
Just tested on fresh ubuntu 12.04, nginx 1.1.6, mariaDB 10, php-fpm 5.5 using below nginx configuration, and everything is running without problem
Thank you for sharing your wonderful work. I’d like to use it for my base app.
Hi, great work, i have the exatly idea!! But i have a little problem: installed via composer in xampp ( with virtual host ) on windows 7, i have all generated url with an “\” extra in them. Example: api.test.dev/\admin, api.test.dev/\login , so all css and js are broken. Why this? Have you any ideas? Thk’s in advance
Hi Ricardo,
I am reproducing the issue here, its weird, but I’ll try to push the patch soon
regards
Ikhsan
Hello
how far you have come with the group function? I can not wait to see how it works 🙂
Martin
Hi Martin, I can’t provide an exact date, but I will try to push it this weekend
Any update on the group functionality?