Slim PHP App

Slim

by Slimframework

Micro framework for PHP developing
Helps with: PHP
Similar to: ThinkPHP App Cloudways PHP SDK App Laravel App Drupal App More...
Source Type: Open
License Types:
MIT
Supported OS:
Languages: Other

What is it all about?

Slim is a PHP micro framework that enabling quickly writing of simple and powerful web applications and APIs. Slim is easy to use and optimized for beginners as for advanced. Includes convenient and highly documented.

Key Features

* HTTP Router: Slim provides a fast and powerful router that maps route callbacks to specific HTTP request methods and URIs. It supports parameters and pattern matching. * Middleware: Build your application with concentric middleware to tweak the HTTP request and response objects around your Slim app. * PSR-7 Support: Slim supports any PSR-7 HTTP message implementation so you may inspect and manipulate HTTP message method, status, URI, headers, cookies, and body. * Dependency Injection: Slim supports dependency injection so you have complete control of your external tools. Use any Container-Interop container.


Pricing

Yearly
Monthly
Lifetime
Free
Freemium
Trial With Card
Trial No Card
By Quote

Description

free - see site

Product Analysis

PHP

PHP frameworks and libraries

Slim micro framework for PHP

Slim micro framework for PHP

By Victor Carlo Jacaban III | 5/29/2016 | Product Analysis |Beginners

Slim is an open-source micro-framework for PHP application development that helps you quickly write simple yet powerful web applications and APIs. It is a simple PHP framework that concentrates on web application core necessities. The Slim Framework also provides features such as helper methods and middleware while enabling the user to choose or create custom templates like other PHP frameworks. It also features secure cookies, error handling and debugging, and very simple configuration.

Advantages of using the Slim Framework for building web application include:

Simplicity – the Slim framework provides a simple set of tools and represents an easy to understand and easy to read platform that really helps new web developers easily understand the framework. It's also great for veteran web developers who want to develop an application in a short period of time. 

Lightweight – As the product name implies, the Slim framework is slim and lightweight—lighter than most of its rival framework. The code footprint is also smaller which means maintenance is lower as well.

Easy to set up – One of the advantage of the Slim framework is that it works with composer  which makes it very accessible, easy and fast to set up. We can download and install using the Bash command line as shown below. For quick and easy installation, try using the Composer dependency manager.

 

composer require slim/slim "^3.0"

 

Let's assume that we want to create a project named "blog". After the above bash command was executed successfully, it will download the Slim framework and its dependency. If you don’t have Composer installed, you can get it on their website at https://getcomposer.org. You’ll see the steps on how to download it in any environment you might have.

 

Great Documentation – Slim PHP gives you a step by step tutorial on how the framework works and also provides a walk through tutorial on building a simple application. This is a great place to start and will help you go from the basics and on to more complicated things in web development.

 

Middleware Feature - Just like other big and established PHP frameworks, Slim provides a new feature called middleware. Middleware is a mechanism that filters HTTP requests that enter your web application. Middleware in Slim is different from the way middleware works in other frameworks. Slim’s middleware was added as concentric layers surrounding the core application, tweaking its response object and HTTP request around your Slim application.

 

Example middleware application on slim.

<?php	
$app = new \Slim\App();
 
$app->message(function ($request, $response, $next) {
                $response = $next($request, $response);
                $response->getBody()->write('World');
 
                return $response;
});
 
$app->get('/', function ($request, $response, $args) {
                $response->getBody()->write(' Hello ');
            return $response;
});
$app->run();

In the example above, we've added middleware to our slim application. Add application with Slim application instances message method. It will output ‘Hello World’. There’s a lot more that you can do using middleware on Slim such as Route middleware which will be invoked if its routes matches the current HTTP request and URI and Group middleware that allows individual routes internally and is invoked only if its routes matche one of defined HTTP request and URI.

 

 

CSRF Protection Feature – Just like other Full stack PHP frameworks, Slim micro framework also has a CSRF protection feature. CSRF is an attack in which requests are sent from a site where a specific user visits to other sites. Requests will be routed to the target site where a user is currently authenticated. With CSRF protection, Slim will generate a unique token for POST requests from HTML form validation.

 

Example

 

 

<?php      

$app = new \Slim\App();

 

$app->get('/bar', function ($request, $response, $args) {

  

    $name = $request->getAttribute('csrf_name');

    $value = $request->getAttribute('csrf_value');

 

    $html = <<<EOT

 

<!DOCTYPE html>

<html>

<body>

    <form method="POST" action="/validate">

        <input type="hidden" name="csrf_name" value="$name">

        <input type="hidden" name="csrf_value" value="$value">

        <input type="text" name="name" placeholder="Name">

        <input type="submit" value="Go">

    </form>

</body>

</html>

EOT;

 

From the example above, when implementing it you can see token values on hidden input fields that will change every time you reload the page. It will give you assurance that you can submit the form without any problems.

 

You can also customize CSRF error messages which make it look more user friendly.

 

 

 

<?php 

 

$app = new \Slim\App();

 

$shield = new Slim\Csrf\Guard();

$shield->setFailureCallable(function ($request, $response, $next) {

    return $response->write(<<<EOT

<!DOCTYPE html>

<html>

<body>

    <h3>Process failed</h3>

    <p>An error occurred upon form submission.</p>

</body>

</html>

EOT);

});

$app->add($shield);

 

HTTP Caching feature – Slim framework provides built-in HTTP caching support with the use of Etag. Etag is a mechanism that HTTP provides for web cache validation. It prevents Slim applications from serving entire mark up on URI that makes cache more effective and saves bandwidth.

 

 

<?php

 

$app = new \Slim\App();

 

$app->get('/home', function () use ($app) {

    $app->etag('unique-etag-id');

    echo "hello world";

});

 

From the above example, we set Etag on the Slim application by invoking the etag method on a route callback and pass the unique etag id as the argument.

 

HTTP caching on Slim also offers other service providers methods such as withExpires() and withLastModified(). The withExpires method used to create a response object with Expires header.

 

Example.

 

 

<?php

 

$app = new \Slim\App();

 

$app->get('/home',function ($req, $res, $args) {

    $resWithExpires = $this->cache->withExpires($res, time() + 7200);

 

    return $resWithExpires;

});

 

While withLastModified method used to create a Response object with last modified header.

 

Example.

 

 

<?php

 

$app = new \Slim

 

$app->get('/foo',function ($req, $res, $args) {

    $resWithLastModification = $this->cache->withLastModified($res, time() - 7200);

 

    return $resWithLastMod;

});

 

Can use Eloquent - Eloquent provides simpler ActiveRecord implementation when working on databases and allows models to correspond to a table on your databases making working with database relationships easy to implement.

 

Example.

 

$query = $this->Blogs->where('title', 'like', '%sample%')->get();

 

From the example above, assuming we have Blogs table and we would like to search a blog post where the title is like sample.

 

Query on controller

 

 

public function __invoke(Request $request, Response $response, $args)

{

        $query = $this->Blog->get();

 

        $this->view->render($response, 'app/home.twig', [

            'widgets' => $query

        ]);

 

        return $response;

  }

 

Conclusion

 

Whether one should use a fullstack or a microframeworks depends on the scope and goal of the application. Slim's microframework is a small framework with very powerful features and is considered to be one of the fastest micro frameworks available today. With a lot of support from developers around the globe, no wonder this microframework will soon be recognized and popular like some other PHP frameworks that are now rocking the web development industry.

 

 

By Victor Carlo Jacaban III | 5/29/2016 | Product Analysis

{{CommentsModel.TotalCount}} Comments

Your Comment

{{CommentsModel.Message}}

Top DiscoverSDK Experts

User photo
3355
Ashton Torrence
Web and Windows developer
GUI | Web and 11 more
View Profile
User photo
1490
Ronan McCarthy
Cross-Platform & Web developer.
Web | Mobile and 6 more
View Profile
User photo
1230
Gary Green
Expert programmer, Ask what you want
Web | JavaScript and 3 more
View Profile
User photo
1130
Harry Gold
Experienced Web developer.
Web | JavaScript and 3 more
View Profile
Show All

Interested in becoming a DiscoverSDK Expert? Learn more

X

Compare Products

Select up to three two products to compare by clicking on the compare icon () of each product.

{{compareToolModel.Error}}

Now comparing:

{{product.ProductName | createSubstring:25}} X
Compare Now