The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

Slim bootstrap app

IcyPantry's Avatar

IcyPantry

05 Dec, 2012 02:40 AM

I created an bootstrap file for slim (index.php).
It supports multi lang in urls (no translater app) taken from: http://nesbot.com/2012/6/26/multilingual-site-using-slim
It has a helper to load some functions outside the autoloader
It's configurable.

Could you give me your opinion about it? What should i improve, what should i add, etc...

<?php

    /**
     *
     * Edit these settings
     *
     ******************************************************************/

    // No need to touch this
    define( 'DS',                   DIRECTORY_SEPARATOR                 );
    // Set your root direcory, wich also should contain the Slim directory
    define( 'ROOT_DIR',             '/home/eddie/htdocs'                );

    // Set to '\ExtendedSlim' to use ExtendedSlim
    // Set to '\Slim\Slim' to use default Slim
    use \ExtendedSlim as Slim;
    // Set to production or development, for error reporting
    define( 'SLIM_MODE',            'development'                       );
    // Set yur templates directory
    define( 'SLIM_TEMPLATES_PATH',  ROOT_DIR.DS.'Themes'                );

    // Set your import directory
    define( 'IMPORT_DIR',           ROOT_DIR.DS.'Kernel'                );
    // Set your import directory
    define( 'HELPER_DIR',           IMPORT_DIR.DS.'Helper'              );

    // Set available site languages, separated wity only dots
    define( 'SLIM_LANGUAGES',       'nl.fy.en'                          );

    // Do not edit below these lines.

    // Optional: The system wil automaticaly include the following files from IMPORT_DIR if they exist:
        // defines.inc, config.inc, hooks.inc (in the same order)

    // Warning: routes.inc is a mandatory file wich wil also need to be in your IMPORT_DIR



    /**
     *
     * Enable autoload
     *
     ******************************************************************/
    require ROOT_DIR.DS.'Slim'.DS.'Slim.php';
    \Slim\Slim::registerAutoloader();



    /**
     *
     * Setup multilang and Helper
     *
     ******************************************************************/

    class ExtendedSlim extends \Slim\Slim {

        // This will add the current language to the requested url
        public function urlFor( $name, $params = array() )
        {
            return sprintf( '/%s%s', $this->getLang(), parent::urlFor( $name, $params ) );
        }

        // This wil get the current language (string)
        public function getLang()
        {
            return parent::config( 'currentLanguage' );
        }

        // This wil get the default language (string)
        public function getDefaultLang()
        {
            // Get the array with available languages
            $mDefault = parent::config( 'availableLanguages' );
            // Set first occurence as default
            $mDefault = $mDefault[0];

            return $mDefault;
        }

        // This wil get the current language (string)
        public function setLang( $lang )
        {
            parent::config( 'currentLanguage', $lang );
        }

        // This wil set the available languages (array)
        public function setAvailableLangs( $availableLangs )
        {
            parent::config( 'availableLanguages', $availableLangs );
        }

        // This wil get the available languages (array)
        public function getAvailableLangs( $availableLangs )
        {
            return parent::config( 'availableLanguages', $availableLangs );
        }

        // This will load the requested function from HELPER_DIR
        public function loadHelper( $mHelper )
        {

            if( is_array( $mHelper ) ) :

                foreach( $mHelper as $sHelper ) :
                    self::loadHelper( $sHelper );
                endforeach;

            else :
                $aHelpers = parent::config( 'Helper.Loaded' );

                if ( $aHelpers === null ) :
                    parent::config( 'Helper.Loaded', array() );
                    $aHelpers = array();
                endif;

                if( !in_array( $mHelper, $aHelpers ) ) :

                    if( file_exists( HELPER_DIR.DS.$mHelper.'.inc' ) ) :
                        include HELPER_DIR.DS.$mHelper.'.inc';

                        $aHelpers = array_merge( $aHelpers, array( $mHelper ) );
                        parent::config( 'Helper.Loaded', $aHelpers );

                    endif;

                endif;

            endif;

        }

    }



    /**
     *
     * Configure Slim!
     *
     ******************************************************************/


    // This wil setup Slim
    $oSlim = new Slim(
        array(
            'mode' => SLIM_MODE,
            'templates.path' => SLIM_TEMPLATES_PATH
        )
    );

    // Only invoked if SLIM_MODE is "production"
    $oSlim->configureMode( 'production', function () use( $oSlim )
    {
        $oSlim->config (
            array(
                'log.enable' => true,
                'debug' => false
            )
        );

        $oSlim->applyHook( 'slim.configure.production' );
    } );

    // Only invoked if SLIM_MODE is "development"
    $oSlim->configureMode( 'development', function() use( $oSlim )
    {
        $oSlim->config(
            array(
                'log.enable' => false,
                'debug' => true
            )
        );

        $oSlim->applyHook( 'slim.configure.development' );
    } );

    // Set requested language
    $oSlim->hook( 'slim.before', function() use( $oSlim )
    {
        $oEnv = $oSlim->environment();
        $sCurrentLanguage = $oSlim->getDefaultLang();

        // Set $pathInfo with end slash
        if( substr( $oEnv['PATH_INFO'], -1 ) !== '/' ) :
            $pathInfo = $oEnv['PATH_INFO'].'/';
        else :
            $pathInfo = $oEnv['PATH_INFO'];
        endif;

        foreach( $oSlim->config( 'availableLanguages' ) as $sLanguage ) :
            $sMatch = '/'.$sLanguage;

            // Check if language prefix is in url
            if( strpos( $pathInfo, $sMatch.'/' ) === 0 ) :
                $sCurrentLanguage = $sLanguage;

                $oEnv['PATH_INFO'] = substr( $oEnv['PATH_INFO'], strlen( $sMatch ) );

                if( strlen( $oEnv['PATH_INFO'] ) == 0 ) :
                    $oEnv['PATH_INFO'] = '/';
                endif;

            endif;

        endforeach;

        // Save the language settings in Slim
        $oSlim->setLang( $sCurrentLanguage );

    } );

    $oSlim->setAvailableLangs( explode( '.', SLIM_LANGUAGES ) );



    /**
     *
     * Fetching Defines, Config, Hooks, Classes, Functions and Routes!
     *
     ******************************************************************/

    // Get defines
    if( file_exists( IMPORT_DIR.DS.'defines.inc' ) ) :
        require_once IMPORT_DIR.DS.'defines.inc';
    endif;

    // Get config
    if( file_exists( IMPORT_DIR.DS.'config.inc' ) ) :
        require_once IMPORT_DIR.DS.'config.inc';
    endif;

    // Get hooks
    if( file_exists( IMPORT_DIR.DS.'hooks.inc' ) ) :
        require_once IMPORT_DIR.DS.'hooks.inc';
    endif;

    // Get routes
    require_once IMPORT_DIR.DS.'routes.inc';



    /**
     *
     * Everything has been set up, run the app!
     *
     ******************************************************************/

    $oSlim->run();

?>
  1. IcyPantry closed this discussion on 05 Dec, 2012 02:40 AM.

  2. IcyPantry re-opened this discussion on 05 Dec, 2012 02:41 AM

  3. 1 Posted by IcyPantry on 05 Dec, 2012 11:53 AM

    IcyPantry's Avatar

    For MVC you can do

    'route.inc'

    <?php
        global $oSlim;
    
        $oSlim->get('/', function ()
        {
            \Module\Home\Controller::getHome();
        } );
    ?>
    

    'Module/Home/Controller.php'

    <?php namespace Module\Home;
    
        class Controller extends Model
        {
    
            public static function getHome()
            {
                // put here your home stuff
            }
        }
    ?>
    
  4. Andrew Smith closed this discussion on 27 Mar, 2013 02:46 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac