2017 © Pedro Peláez
 

project silex-skeleton

A pre-configured skeleton for the Silex microframework, based on fabpot/silex-skeleton

image

bgaze/silex-skeleton

A pre-configured skeleton for the Silex microframework, based on fabpot/silex-skeleton

  • Sunday, August 27, 2017
  • by Bgaze
  • Repository
  • 1 Watchers
  • 0 Stars
  • 10 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Silex Skeleton

Welcome to the Silex Skeleton - a fully-functional Silex application that you can use as the starting point for your Silex applications., (*1)

A great way to start learning Silex is via the Documentation, which will take you through all the features of Silex., (*2)

This document contains information on how to start using the Silex Skeleton., (*3)

Install.

$ composer create-project bgaze/silex-skeleton:dev-master path/to/install

Then give to PHP write permissions on /var directory (for logs and cache)., (*4)

Browsing the Demo Application

To configure Silex on your local webserver, see Webserver configuration documentation., (*5)

To use the PHP built-in web server run :, (*6)

$ cd path/to/install
$ COMPOSER_PROCESS_TIMEOUT=0 composer run

Then, browse to http://localhost:8888/index_dev.php/, (*7)

Included service providers.

Read the Providers documentation for more details about Silex Service Providers., (*8)

The Silex Skeleton is configured with the following service providers:, (*9)

  • ValidatorServiceProvider : Provides a service for validating data. It is most useful when used with the FormServiceProvider, but can also be used standalone.
  • ServiceControllerServiceProvider - As your Silex application grows, you may wish to begin organizing your controllers in a more formal fashion. Silex can use controller classes out of the box, but with a bit of work, your controllers can be created as services, giving you the full power of dependency injection and lazy loading.
  • TwigServiceProvider - Provides integration with the Twig template engine.
  • WebProfilerServiceProvider - Enable the Symfony web debug toolbar and the Symfony profiler in your Silex application when developing.
  • MonologServiceProvider - Enable logging in the development environment.
  • FinderServiceProvider - Find files and directories via an intuitive fluent interface.
  • ConsoleServiceProvider - A CLI application service provider for Silex.

Usage.

This Silex implementation follows several convention.
Mainly, currently modified files should reside into /src folder., (*10)

Please find in this section more details about the organisation., (*11)

Views.

Application views reside into /src/views folder., (*12)

Two templates are included :, (*13)

  • base.html.twig : a raw base template defining main blocks in a HTML page.
  • layout.html.twig : an implementation of Bootstrap 4 Stater Template.
    It can be used to quick start your app, and as a pattern to build your own layout.

Controllers.

Controllers reside into /src/controllers folder.
Any PHP file in this folder is automatically loaded., (*14)

The application already contains a main controller mounted on the application root path., (*15)

Basically, to add a new controller :, (*16)

  • Use controllers_factory to generate a new controller.
  • Add your actions.
  • Mount the controller with the path you want.

Example :, (*17)

<?php
# src/controllers/demo.php

// Create controller.
$controller = $app['controllers_factory'];

// Add actions.

$controller->get('/', function () use ($app) {
    // ...
})->bind('demo.home');

$controller->get('/page1', function () use ($app) {
    // ...
})->bind('demo.page1');

$controller->get('/page2', function () use ($app) {
    // ...
})->bind('demo.page2');

$controller->mount('/sub-section', function ($sub) {
    $controller->get('/', function () use ($app) {
        // ...
    })->bind('demo.sub.home');

    $controller->get('/page1', function () use ($app) {
        // ...
    })->bind('demo.sub.page1');

    $controller->get('/page2', function () use ($app) {
        // ...
    })->bind('demo.sub.page2');
});

// Mount controller.
$app->mount('/demo', $controller);

/*
 * RESULTING URI :
 * /demo
 * /demo/page1
 * /demo/page2
 * /demo/sub-section
 * /demo/sub-section/page1
 * /demo/sub-section/page2
 */

Documentation: [Organizing Controllers][13]., (*18)

Console.

Usage.

Console executable is /bin/console file :, (*19)

$ php bin/console your:command

To get a list of available commands, just call it with no arguments :, (*20)

$ php bin/console

This implementation is shipped with two commands :, (*21)

  • cache:clear : erases application cache.
    Run this command to see your changes into PROD environment.
  • demo:command : a pattern for your custom commands.

If you don't need to build custom commands, you can safely remove /src/commands folder., (*22)

Custom commands.

Custom commands reside into /src/commands folder.
Any PHP file in this folder is automatically loaded when console is invoked., (*23)

Basically, to create a command :, (*24)

  • Store your command into /src/commands.
  • Make it extend Bgaze\Silex\Console\Command\AbstractCommand.
  • Register it right after the class.

Example :, (*25)

<?php
# src/commands/mycommand.php

use Bgaze\Silex\Console\Command\AbstractCommand;

class MyCommand extends AbstractCommand {

    // ...

}

$app['console']->add(new DemoCommand());

Please see /src/commands/demo.php for a more detailed example., (*26)

Documentation: ConsoleServiceProvider, Symfony Console Component., (*27)

The Versions