Fork me on GitHub

Basic Project

Silex-MVC project has the file structure is shown below.

|-- app/
|  |-- Console/         # Console application
|  |-- Controllers/     # Controllers
|  |-- Forms/           # Forms
|  |-- Models/          # Models (AR, DBAL, ORM)
|  |-- Providers/       # Providers
|  |-- Resources/       # Resources (configuration, database localization)
|  |-- Services/        # Services (additional classes)
|  |-- Views/           # Displaying data
|  `-- Bootstrap.php    # Boot loader class
|     
|-- data/               # Data (cache, logs)
|-- library/            # Libraries (AR, Pagerfanta)
|-- public/
|  |-- css/             # Cascading styles
|  |-- fonts/           # Fonts
|  |-- img/             # Images
|  |-- js/              # Java Scripts 
|  |-- schemes/         # Schemes
|  |-- .htaccess
|  |-- favicon.ico
|  `-- index.php        # Entrance to application
|-- vendor/             # External libraries
|-- LICENSE.md
|-- README.md
|-- README-RU.md
|-- composer.json
`-- composer.lock    

Silex-MVC consists of two independent applications. This is Web application and Console application. These applications are configured differently, although they have a common configuration files and shared libraries.


Structure of web application (app)

The application has the following file structure.

|-- app/
    |-- Console/         # Console application
    |-- Controllers/     # Controllers
    |-- Forms/           # Forms
    |-- Models/          # Models (AR, DBAL, ORM)
    |-- Providers/       # Providers
    |-- Resources/       # Resources (configuration, database localization)
    |-- Services/        # Services (additional classes)
    |-- Views/           # Displaying data
    `-- Bootstrap.php    # Boot loader class

Loader (Bootstrap.php) for web application

Execution sequence web applications.

Request --> index.php --> Bootstrap.php --> Controller --> Response

The loader performs the following tasks:

  • Creating an application
// Create app
$app = new Silex\Application();
$this->app = $app;
  • Creating handlers middleware
  • Initialization profiler
  • Initialize the application configuration
  • Initialization providers
  • Initialization services
  • Initialization controllers
  • Initialization of the application itself

Controllers

Used to process requests from the client and solve a series of tasks:

  • Handling errors (app/Controllers/ErrorController.php)
  • Common tasks: change the language, change the color scheme, license, readme (app/Controllers/IndexController.php)
  • Management blog (app/Controllers/BlogController.php)
  • Registration, authentication and authorization users (app/Controllers/SecurityController.php)
  • Task list management (app/Controllers/TodoController.php)
  • Testing the service UBKI (app/Controllers/UbkiController.php)
  • Demonstrating some of the capabilities (app/Controllers/TestController.php)

All these controllers inherit from common class app/Controllers/BaseController.php, that defines methods common to all the controller.

Forms

Silex uses Symfony 2 components that facilitate the creation, display, and validation of forms.

|-- app/
    |-- Forms/          # Forms
      |-- Constraints/  # Rules form validation
      |-- PostForm.php  # Form for adding/editing user posts
      `-- RegForm.php   # Form for the registration of a new user

Models

A set of classes, which enables operation with a database. For testing and verification is implemented three technologies work with database:

Providers

Providers app/Providers allow the developer to reuse part of the application in other parts of the application. Silex provides two types of interfaces: ServiceProviderInterface and ControllerProviderInterface.

Providers that are used in this application, ensure operation with database, application configured, building models and other actions...

Resources

They are used to store configuration files, database and also serve to localize application.

|-- app/
    |-- Resources/          # Resources
        |-- Config/         # Configuration files
        |-- db/             # Database `SqlLite` (`app.db`)
        |-- fakedata/       # Data to populate the database when creating applications
        |-- translations/   # Files for localization of application

Services

Silex is not only a framework, it is also a service container. It does this by extending Pimple which provides a very simple service container.

Our application contains different kinds of services app/Services/My: to work with arrays, strings, XML, HTTP, Markdown. And services: Zend-Filter, Zend-Json and other vendor/zendframework.

|-- app/
    |-- Services/           # Services
        |-- My/             # Services for working with XML, HTTP, strings, arrays, and other...
        |-- MyService.php   # The base class for adding services
        `-- Zf2Service.php  # Class to add `Zend` services

Displaying data

The application uses a template Twig

|-- app/
    |-- Views/          # Displaying data
        |-- Controller/ # Display when accessing the controller
        |-- Form/       # Displaying the form
        |-- Include/    # Some of the views
        `-- Layout/     # Layout

Twig - modern template engine for PHP

Twig as the templating syntax somewhat similar to Smarty and XSLT is not inferior in flexibility.

Its main features:

  • Fast: compiles templates in PHP code.
  • Secure: has a "sandbox" mode to test the questionable code templates. This allows the use Twig as template language for applications where users can change the design templates.
  • Flexible: It is based on the plastic parser. This allows developers to define their own tags and filters, to create your DSL.

Structure of web application (public)

Public data have the following file structure.

|-- public/
|  |-- css/             # Cascading styles
|  |-- fonts/           # Fonts
|  |-- img/             # Images
|  |-- js/              # Java Scripts 
|  |-- schemes/         # Schemes
|  |-- .htaccess
|  |-- favicon.ico
|  `-- index.php        # Entrance to application

The entry point to the application is file - index.php.

Java Scripts

This is a set of libraries, frameworks and services at the client level.

|-- public/
   |-- js/                  # Java Scripts 
        |-- app/            
        |   |-- bb-todo/    # Components implement the test example `ToDo` 
                            # based framework `Backbone`
        |   |-- services/   # Services: `Datepicker`, `FormValidation`, `MaskInput`, `Highlight`
        |   |-- app.js      # `App` class 
        |   |-- lang.js     # `Lang` class
        |   `-- system.js   # `System` class
        |
        |-- lib/            # Libraries
        `-- main.js         # Initialization Requirejs

Structure of console application

Console application uses components that facilitate the creation of user-friendly command-line interface.

|-- app/
    |-- Console/        # Console application
      |-- Commands/     # Command set to work with the database, and others.
      |-- Views/        # The text data using the template `Twig`
      |-- scripts/      # A set of command files
      |-- Bootstrap.php # Boot loader class
      `-- index.php     # Entrance to console application

Console application performs the following sequence of operations.

script.bat --> index.php --> Bootstrap.php --> Command(s) --> Output

The configuration file app/Resources/Config/parameters.yml is used to configure the console application, as well as app/Resources/Config/console.yml, which inherits configuration settings from a file app/Resources/Config/config.yml.