bgaze/silex-skeleton

This package is abandoned and no longer maintained. No replacement package was suggested.

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

dev-master 2017-08-27 16:50 UTC

This package is auto-updated.

Last update: 2020-01-17 16:48:05 UTC


README

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

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

This document contains information on how to start using the Silex Skeleton.

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).

Browsing the Demo Application

To configure Silex on your local webserver, see Webserver configuration documentation.

To use the PHP built-in web server run :

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

Then, browse to http://localhost:8888/index_dev.php/

Included service providers.

Read the Providers documentation for more details about Silex Service Providers.

The Silex Skeleton is configured with the following service providers:

  • 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.

Please find in this section more details about the organisation.

Views.

Application views reside into /src/views folder.

Two templates are included :

  • 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.

The application already contains a main controller mounted on the application root path.

Basically, to add a new controller :

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

Example :

<?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.

Console.

Usage.

Console executable is /bin/console file :

$ php bin/console your:command

To get a list of available commands, just call it with no arguments :

$ php bin/console

This implementation is shipped with two commands :

  • 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.

Custom commands.

Custom commands reside into /src/commands folder.
Any PHP file in this folder is automatically loaded when console is invoked.

Basically, to create a command :

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

Example :

<?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.

Documentation: ConsoleServiceProvider, Symfony Console Component.