• This repository has been archived on 19/Sep/2022
  • Stars
    star
    177
  • Rank 215,263 (Top 5 %)
  • Language
    PHP
  • License
    MIT License
  • Created almost 12 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Controller extensions for the Slim Framework, used by help.fortrabbit.com and blog.fortrabbit.com

SlimController

SlimController is an extension for the Slim Framework providing the C of MVC.

With Slim alone, you can create great applications very, very quickly. Sometimes things get out of hand an you just need a bit more structure - or at least I do. That's what SlimController is for.

Latest Stable Version Total Downloads

Build Status

Install via composer

Create a composer.json file

{
    "require": {
        "slimcontroller/slimcontroller": "0.4.3"
    },
    "autoload": {
        "psr-0": {
            "MyApp": "src/"
        }
    }
}

Run installation

composer.phar install --dev

Mini HowTo

If you know how Slim works, using SlimController shouldn't be a big deal.

Example Structure

Setup a structure for your controller and templates (just a suggestion, do as you like):

mkdir -p src/MyApp/Controller templates/home

Controller

Create your first controller in src/MyApp/Controller/Home.php

<?php

namespace MyApp\Controller;

class Home extends \SlimController\SlimController
{

    public function indexAction()
    {
        $this->render('home/index', array(
            'someVar' => date('c')
        ));
    }

    public function helloAction($name)
    {
        $this->render('home/hello', array(
            'name' => $name
        ));
    }
}

Templates

Here are the two corresponding demo templates:

templates/home/index.php

This is the SlimController extension @ <?= $someVar ?>

templates/home/hello.php

Hello <?= $name ?>

Boostrap index.php

Minimal bootstrap file for this example

<?php

// define a working directory
define('APP_PATH', dirname(__DIR__)); // PHP v5.3+

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

// init app
$app = New \SlimController\Slim(array(
    'templates.path'             => APP_PATH . '/templates',
    'controller.class_prefix'    => '\\MyApp\\Controller',
    'controller.method_suffix'   => 'Action',
    'controller.template_suffix' => 'php',
));

$app->addRoutes(array(
    '/'            => 'Home:index',
    '/hello/:name' => 'Home:hello',
));

$app->run();

Run

php -S localhost:8080

Controller

Configuration

controller.class_prefix

Optional class prefix for controller classes. Will be prepended to routes.

Using \\MyApp\\Controller as prefix with given routes:

$app->addRoutes(array(
    '/'            => 'Home:index',
    '/hello/:name' => 'Home:hello',
));

Translates to

$app->addRoutes(array(
    '/'            => '\\MyApp\\Controller\\Home:index',
    '/hello/:name' => '\\MyApp\\Controller\\Home:hello',
));

controller.class_suffix

Optional class suffix for controller classes. Will be appended to routes.

Using Controller as suffix with given routes:

$app->addRoutes(array(
    '/'            => 'Home:index',
    '/hello/:name' => 'Home:hello',
));

Translates to

$app->addRoutes(array(
    '/'            => 'HomeController:index',
    '/hello/:name' => 'HomeController:hello',
));

controller.method_suffix

Optional method suffix. Appended to routes.

Using Action as suffix with given routes:

$app->addRoutes(array(
    '/'            => 'Home:index',
    '/hello/:name' => 'Home:hello',
));

Translates to

$app->addRoutes(array(
    '/'            => 'Home:indexAction',
    '/hello/:name' => 'Home:helloAction',
));

controller.template_suffix

Defaults to twig. Will be appended to template name given in render() method.

Extended Examples

Routes

// how to integrate the Slim middleware
$app->addRoutes(array(
    '/' => array('Home:index', function() {
            error_log("MIDDLEWARE FOR SINGLE ROUTE");
        },
        function() {
            error_log("ADDITIONAL MIDDLEWARE FOR SINGLE ROUTE");
        }
    ),
    '/hello/:name' => array('post' => array('Home:hello', function() {
            error_log("THIS ROUTE IS ONLY POST");
        }
    ))
), function() {
    error_log("APPENDED MIDDLEWARE FOR ALL ROUTES");
});

Controller

<?php

namespace MyApp\Controller;

class Sample extends \SlimController\SlimController
{

    public function indexAction()
    {

        /**
         * Access \SlimController\Slim $app
         */

        $this->app->response()->status(404);


        /**
         * Params
         */

        // reads "?data[foo]=some+value"
        $foo = $this->param('foo');

        // reads "data[bar][name]=some+value" only if POST!
        $bar = $this->param('bar.name', 'post');

        // all params of bar ("object attributes")
        //  "?data[bar][name]=me&data[bar][mood]=happy" only if POST!
        $bar = $this->param('bar');
        //error_log($bar['name']. ' is '. $bar['mood']);

        // reads multiple params in array
        $params = $this->params(array('foo', 'bar.name1', 'bar.name1'));
        //error_log($params['bar.name1']);

        // reads multiple params only if they are POST
        $params = $this->params(array('foo', 'bar.name1', 'bar.name1'), 'post');

        // reads multiple params only if they are POST and all are given!
        $params = $this->params(array('foo', 'bar.name1', 'bar.name1'), 'post', true);
        if (!$params) {
            error_log("Not all params given.. maybe some. Don't care");
        }

        // reads multiple params only if they are POST and replaces non given with defaults!
        $params = $this->params(array('foo', 'bar.name1', 'bar.name1'), 'post', array(
            'foo' => 'Some Default'
        ));


        /**
         * Redirect shortcut
         */

        if (false) {
            $this->redirect('/somewhere');
        }


        /**
         * Rendering
         */

        $this->render('folder/file', array(
            'foo' => 'bar'
        ));

    }
}

More Repositories

1

teutonic-css

A modern CSS framework β€” versatile, well documented.
SCSS
314
star
2

quotes

Quotes for developers, used in fortrabbit's dashboard
PHP
84
star
3

craft-copy

Deployment tools for Craft on fortrabbit
PHP
73
star
4

phpco-docker

Simple docker build for running PHPCompatibility (https://github.com/PHPCompatibility/PHPCompatibility)
Dockerfile
55
star
5

beelzebub

A PHP framework for writing (forked) multi process daemons
PHP
16
star
6

Custom-HTTP-Error-Pages

Common HTTP Status Apache error pages with some additional infos for humans and even some custom errors.
HTML
11
star
7

datafilter

A data validation (sanitation) module for PHP
PHP
10
star
8

help

Contents of the fortrabbit help in markdown format
9
star
9

validator-loader

Load Laravel validator definitions from files or directories
PHP
9
star
10

legal

Legal documents of fortrabbit in markdown with trackable changes
8
star
11

Pharify

Command line tool to create PHP phar packages easily.
PHP
7
star
12

message-signer

A flexible message signing and verification framework. Includes Guzzle Plugin.
PHP
7
star
13

craft-asset-bundler

Better asset handling for load balanced environments.
PHP
5
star
14

memcached-cli

PHP CLI to interact with Memcached
PHP
5
star
15

laravel-object-storage

A Laravel flysystem driver that integrates with fortrabbit's S3 compatible Object Storage
PHP
4
star
16

craft-auto-migrate

Run Craft migrations automatically
PHP
4
star
17

validator-loader-laravel

Integrates Validator Loader as Service Provider into Laravel
PHP
4
star
18

craft-object-storage

A Craft 3 plugin that integrates with fortrabbit's S3 compatible Object Storage
PHP
3
star
19

craft-s3-fortrabbit

Craft 2 plugin to support the fortrabbit Object Storage
PHP
2
star
20

yii-memcached

Yii2/Craft3 Memcached enabler
PHP
1
star
21

interview-task-k8s

1
star
22

phan-docker

Simple docker build for running PHP static analyser phan (https://github.com/phan/phan)
Dockerfile
1
star