Install and trying Slimframework

Hi and hallo. Long time I not write article again and now I back again with new article. Previously I using and learning laravel, and now Laravel has reached version 5. For now why I choose slim and not laravel again?

Because I want to use framework with small size and simple. Laravel is big framework (framework full stack). Laravel has many functions and features, but all unused by me and the features it becomes useless if not used.

So now I want share how to install and trying slimframework. The first, you must install composer. Follow this link https://getcomposer.org/download/. After you success intall composer, now open slimframework site and follow installation guide from there. Or you can open cmd if you’re using windows and open terminal if you using Linux/unix and follow this instruction:

// Windows
D:\>cd workspaces\slim // example I save my website code in directory D:\workspaces
D:\>composer require slim/slim

// Linux
$ cd ~/workspaces/slim
$ composer require slim/slim

And this is example result after run composer:

$ ls
vendor

Look result above, we just have core of slim. The question is how to create web app and where the app directory?
For the answer, we must create public directory and create index.php file inside.
Look this picture:
Directory slim
And now insert this code on your index.php:

<?php
// Configuration for error reporting, useful to show every little problem during development
error_reporting(E_ALL);
ini_set("display_errors", 1);

require '../vendor/autoload.php';

// Initialize Slim (the router/micro framework used)
$app = new \Slim\Slim();

# Route
$app->get('/', function() {
	echo "Hello now I'am using slim";
});

$app->run();

Explanations:

  1. Line 3-4 is used for debugging php file. If the code is error, php will echo error result.
  2. Line 6 is used to include core of slimframework
  3. Line 8 is used to instance object/core of slim
  4. Line 12-14 is used to route. So if we open url http://localhost/workspaces/slim (line 8), so slim will execute function echo “Hello…..”
  5. Line 16 is used to run slimframework

Fore more info, you can read on this url http://docs.slimframework.com/. The run your slimframework with url http://localhost/workspaces/slim/public. If your configurations is right, you will get result like this:
Localhost
And now the question again is how to load the view file from views directory?
The answer is, first we must create views directory. Example I create a folder views in the app folder like this:
views directory
The create a home.php file in views directory and insert this code:

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<title>Hello</title>
	<link rel="stylesheet" href="">
</head>
<body>
	Test load the views
</body>
</html>

And now we must change index.php to be like this:

<?php
// Configuration for error reporting, useful to show every little problem during development
error_reporting(E_ALL);
ini_set("display_errors", 1);

require '../vendor/autoload.php';

// Initialize Slim (the router/micro framework used)
$app = new \Slim\Slim([
		'templates.path' => '../App/views'
	]);

# Route
$app->get('/', function() use ($app) {
	$app->render('home.php');
});

$app->run();

Explanations:

  1. Line 10 is slim configuration to add template path or you can read on this site http://docs.slimframework.com/#Application-Settings
  2. Line 14 we add code “use ($app)”, so that code is used for lin 15. So on line 16, we can use “$app” object.
  3. Line 15 is a function to rendering view file

Then run your code and if right, you can get result like this:
Slim render view.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s