PHP boilerplate for quick start projects using Slim Framework and Eloquent.

Overview

Lassi Build Status

PHP boilerplate for quick projects using Slim Framework and Eloquent database.

Lassi

Lassi is a small PHP boilerplate to use Slim Framework with Eloquent database components – enabling you to quickly start building your PHP projects with an MVC design pattern and datastore in no time.

Warnning: Project is in alpha status. For more see issues tracker.

Installation and Setup

Install with composer create-project command. This will install Lassi and all of it's dependencies i.e. Slim Framework and Eloquent database.

$ composer create-project jabranr/lassi

Configuration

Lassi uses .env files to setup it's configuration. There is such sample file .sample.env packaged with it. Lassi will look for .dev.env, .dist.env or .env respectively at the run time or throws NotFoundException.

Charset & Collation

By default .sample.env file has charset and collation configurations set to UTF-8 mb4 to support various type of characters encoding. You can update it with your own choice, of course. For more on best encoding practices, read Working with UTF-8 at PHP: The Right Way.

Routing

Use the routes.php in root directory to setup routes. You would setup routes as you do in Slim Framework. Afterall it is using Slim Framework in background. For more on setting up routes, see Slim Framework Documentation.

Structure

Controllers: The Controllers are to be saved in controller/ directory. All Controllers must extend Lassi\App\Controller base controller class and pass the LassiLassi instance to its constructor using LassiLassi::getInstance() method. You can also add relevant Model(s) using useModel(string|array $model) method. You can name the controller as you like but do keep up with best practices.

Models: All relevant Models are saved in model/ directory and must extend the Illuminate\Database\Eloquent\Model class. You would use models as you do in Eloquent. For more on setting up models and use other options, see Eloquent database quick start guide.

There is an example controller and model in mentioned directories for you to get started with.

Views: All views/templates are saved in view/ directory.

Assets: All assets are saved in public/ directory.

Example:

Create project

Create a project using Composer create-project command and cd into project directory.

$ composer create-project jabranr/lassi
$ cd path/to/lassi

Update configuration

Update configurations as required in .dev.env file.

Start server

Start the PHP built-in server and navigate browser to http://localhost:8000.

$ php -S localhost:8000 -t public

Setup routes

  • Add hello route

Add a new route hello in routes.php and try it in browser by navigating to http://localhost:8000/hello

$app->get('/hello', function() use ($app) {
	$app->response->write('Hello World');
});
  • Add goodbye route

Add a new route goodbye in routes.php to render a template. Create a new file goodbye.php with basic HTML and save in /view directory.

<!DOCTYPE html>
<html>
	<head>
		<title>Lassi goodbye template</title>
	</head>
	<body>
		<h1>Goodbye!</h1>
	</body>
</html>

Call this template to render directly from a route's definition or by using a Controller.

Directly from a route's definition

$app->get('/goodbye', function() use ($app) {
	return $app->render('goodbye.php');
});

Using a controller

Add a new public method goodbye() to WelcomeController.php in /controller directory.

class WelcomeController extends Lassi\App\Controller {
	...

	public function goodbye() {
		return $this->app->render('goodbye.php');
	}
}

Modify the route's definition to use controller.

$app->get('/goodbye', 'Lassi\Controller\WelcomeController:goodbye');

For complete reference, see Slim Framework documentation

Using Eloquent

To setup any database connection fill in the required information in relevant *.env file.

Setup SQLite database

At minimum it requires an absolute URL to SQLite file and db_driver value set to sqlite.

db_driver	 = sqlite				(Required)
db_name		 = path/to/foo.sqlite	(Required)
db_prefix	 = lassi_				(Optional)

Setup MySQL, SQL, MSSQL or Sybase database

db_driver	 = mysql		(Required)
db_host		 = localhost	(Required)
db_name		 = lassi		(Required)
db_username	 = root			(Required)
db_password	 = p@ssword		(Required)
db_prefix	 = lassi_		(Optional)

Using Eloquent is straight forward after a connection is established. To learn more on how to use Eloquent, see Official Eloquent Documentation.

Create a table using Eloquent

You can use the Illuminate\Database\Capsule\Manager::schema() method to setup database migrations. Here is an example to create a lassi_users table.

class WelcomeController extends Lassi\App\Controller {
	...

	public function makeUserTable() {
		Illuminate\Database\Capsule\Manager::schema()->create('users', function($table) {
			$table->increments('id');
			$table->string('name');
			$table->string('email')->unique();
			$table->timestamps();
		});
	}
}

Calling Lassi\Controller\WelcomeController->makeUserTable() will create a new table in database.

A model can be added to a controller using useModel() method in controller's constructor i.e.

class WelcomeController extends Lassi\App\Controller {
	public function __construct() {
		parent::__construct(LassiLassi::getInstance());
		$this->useModel('user');
	}
}

or it can directly be accessed using Lassi\Model namespace i.e.

class WelcomeController extends Lassi\App\Controller {
	...

	public function create() {
		$user = new Lassi\Model\User;
		$user->name = 'Jabran Rafique';
		$user->email = '[email protected]';
		$user->save();
	}
}

Getting info from Eloquent and pass it to template.

class WelcomeController extends Lassi\App\Controller {
	...

	public function goodbye() {
		$user = Lassi\Model\User::find(1);
		return $this->app->render('goodbye.php', array('user' => $user));
	}
}

Issue tracking

Please report any issues to repository issue tracker.

Contribution

I would love to get some help and extend this boilerplate further so it can be useful to a vast audience. If you think you can improve the boilerplate then fork the project and submit pull request at your convenience.

License

MIT License © 2015 – 2016 Jabran Rafique (@jabranr) | Contributors

You might also like...
CORS Middleware for PHP Slim Framework

CorsSlim Cross-origin resource sharing (CORS) Middleware for PHP Slim Framework. Usage Composer Autoloader Install with Composer Update your composer.

PHP slim framework middleware to minify HTML output

slim-minify Slim middleware to minify HTML output generated by the slim PHP framework. It removes whitespaces, empty lines, tabs beetween html-tags an

Slim Framework - Prerequisite Checker
Slim Framework - Prerequisite Checker

Slim Framework - Server Configuration Checker Upload the file check.php to your webserver Browse to the file: https://example.com/check.php Check the

Slim Framework skeleton application with MVC Schema

Slim Framework skeleton application with MVC Schema

This repository contains a library of optional middleware for your Slim Framework application

Slim Framework Middleware This repository contains a library of optional middleware for your Slim Framework application. How to Install Update your co

Slim Framework 3 Skeleton Application + PagSeguro Lib

Slim Framework 3 Skeleton Application + PagSeguro Lib Aplicação simples para geração do Token para pagamentos no PagSeguro (método transparente) e env

My personal blog developed on the Slim Framework

nesbot.com I am making the source code of my personal site available publicly in case it helps anybody. It's developed using the Slim Framework. I am

Plates Template Integration for Slim micro framework 3

Plates Template Integration for Slim micro framework 3 Render your Slim 3 application views using Plates template engine. Install Via Composer $ compo

Juliangut Slim Framework Doctrine handler middleware

Juliangut Slim Framework Doctrine handler middleware Doctrine handler middleware for Slim Framework. Slim3 version Doctrine integration service for Sl

Comments
  • Introducing unit tests

    Introducing unit tests

    refs #4.

    I've created all files needed for automate testing with Travis and code coverage with Coveralls. See:

    • Travis: https://travis-ci.org/Art4/lassi
    • Coveralls: https://coveralls.io/github/Art4/lassi

    Like in #6 described I rename the folder because Unix is case-sensitive. I also moved the namespace Lassi into a separate folder. This keeps it easier for testing and code coverage.

    I also corrected the dependendy from PHP 5.4 to PHP 5.5.9 because illuminate/database requires it at a minimum.

    I write also a first test for Lassi::getInstance(). The Lassi code isn't easy to test because of the dependencies inside the classes. If you would allow to set all dependencies from outside (inversion of control) this would make it possible to mock specific classes and write real unit tests.

    However this PR is ready for being merged. It allows to write more tests in the future. Let me know what you think

    enhancement Hacktoberfest 
    opened by Art4 2
  • Problem with Travis CI unit testing

    Problem with Travis CI unit testing

    There seems to be a problem with Travis CI while running unit tests. The environment cannot properly use the autoloader and returns error with Lassi\App\Util class not found. Currently tests are only available in dev-master branch.

    For more details, please see build history at Travis CI at https://travis-ci.org/jabranr/lassi/builds

    bug help wanted 
    opened by jabranr 2
Owner
Jabran Rafique⚡️
Front-end Tech Lead at RatedPeople
Jabran Rafique⚡️
REST APIs using Slim framework. Implemented all CRUD operations on the MySql database

PHP REST API using slim framework and CRUD operations ?? Hi there, this is a simple REST API built using the Slim framework. And this is for the folks

Hanoak 2 Jun 1, 2022
This Slim Framework middleware will compile LESS CSS files on-the-fly using the Assetic library

This Slim Framework middleware will compile LESS CSS files on-the-fly using the Assetic library. It supports minification and caching, also via Asseti

Gerard Sychay 22 Mar 31, 2020
Slim Framework using Jade for templates

Welcome to Slim-Jade What? This is a boilerplate. It's the Slim Framework with jade.php to render views Why? I like the Sinatra style MVC and love Jad

Joe Fleming 23 Oct 16, 2019
A easy way to install your basic yii projetc, we have encrypt database password in phpfile, my class with alot funtions to help you encrypt and decrypt and our swoole server install just run ./yii swoole/start and be happy!

Yii 2 Basic Project Template with swoole and Modules Yii 2 Basic Project Template is a skeleton Yii 2 application best for rapidly creating small proj

null 3 Apr 11, 2022
Slim is a PHP micro framework that helps you quickly write simple yet powerful web applications and APIs.

Slim Framework Slim is a PHP micro-framework that helps you quickly write simple yet powerful web applications and APIs. Installation It's recommended

Slim Framework 11.5k Jan 4, 2023
A curated list of awesome tutorials and other resources for the Slim micro framework

Awesome Slim A curated list of awesome tutorials and other resources for the Slim micro framework Table of Contents Essentials Tutorials Packages and

Sawyer Charles 466 Dec 8, 2022
Slim Framework HTTP cache middleware and service provider

Slim Framework HTTP Cache This repository contains a Slim Framework HTTP cache middleware and service provider. Install Via Composer $ composer requir

Slim Framework 107 Dec 15, 2022
A Slim PHP MVC framework built just for fun!

Aura Framework A Slim PHP MVC framework built just for fun! en: Note: This repository only contains the core code of the Aura framework. If you want t

Murilo Magalhães Barreto 2 Dec 16, 2021
The Slim PHP micro framework paired with Laravel's Illuminate Database toolkit.

Slim & Eloquent The Slim PHP micro framework paired with Laravel's Illuminate Database toolkit. Getting started # Download composer curl -s https://ge

Kyle Ladd 111 Jul 23, 2022