Rapid Application Prototyping in PHP Using a Micro Framework (Silex) — Part2 Basic Concepts May 27, 2014Posted by Tournas Dimitrios in Silex.
Tags: framework, Slim
add a comment
Welcome back to the next part of the article series “Rapid Application Prototyping in PHP Using a Micro Framework (Silex)”. Understanding the basic concepts of this framework (Silex) will make you feel more comfortable and confident while using it . The goal of this article is to give you a high-level overview of how the Silex framework “works”. Internally, the Silex core is built on top of Pimple, a Dependency Injection Container, and a few components of the Symfony Framework. One of those components is the HttpFoundation component, which defines an object-oriented layer for the HTTP specification. HTTP has a set of predefined methods (also referred as verbs) to indicate what action should be performed on the identified recourse. Actually we don’t have to worry about HTTP-specifications (and all that boring technical detail) as the browser does all the heavy work behind the scenes to build a request according those specifications. The only thing that really interests us is how we should link a request to the corresponding resource (in technical jargon it is named : Routing). In Silex it’s possible to route in two ways . We can either route to a closure or a controller action. As we’ll be going over controllers in later parts of this article, let’s start by looking at how we can route to a closure .
Typically, browsers support only three verbs (methods) of the HTTP-specification : HEAD , GET and POST. Developers have invented a nice trick to emulate a couple other verbs (read more on the documentation page) .
When users click on a link or enters a URL in their address bar, they’re creating a GET request.When users submit a form, they’re typically creating a POST request. The browser starts to communicate with the server with a HEAD request .
Rapid Application Prototyping in PHP Using a Micro Framework (Silex) — Part1 Introduction May 22, 2014Posted by Tournas Dimitrios in Silex.
Tags: framework, Silex
add a comment
Setting up a Framework up to the point of a working web application isn’t difficult these days. We just have to open a terminal and take advantage of Composer’s functionality. Composer will handle the whole process of downloading the required package and its associated dependencies in just a couple minutes. Most popular Frameworks nowadays are distributed via packagist , a Composer package repository, which finds packages and lets Composer know where to get the code from. An important decision has to be made though, which framework should be chosen as a foundation for our web application ? Not a simple question indeed. An American pioneer of computer science (Kenneth Thompson) had proposed the “Unix philosophy” : >>> Building short, simple, clear, modular, and extendable code that can be easily maintained and repurposed by developers other than its creators <<< Many [PHP]frameworks follow the aforementioned philosophy very closely, their components are autonomous blocks of code which can be used by other frameworks as building blocks. A characteristic example is Symfony, its modules are used by many frameworks and libraries.
Silex is a PHP Micro-framework based on Symfony 2 components that has been built to focus on smaller web-applications. Silex handles basic tasks (ie routing , DI) without getting in our way, acting as a great base upon which we can architect the rest of our application. The “Application Class” (core component) of Silex is extending Pimple, it’s a [small] service container, more formally known as a Dependency Injection Container. If you are new to Pimple, reading up on it is going to be extremely important. Pimple’s documentation is pretty easily to follow. As many other dependency injection containers, Pimple is able to manage two different kind of data: services and parameters (key/value pairs). Developers familiar with Symfony’s Service_Container , Laravel’s IoC container or ZF2′ Service_Manager component will immediately recognize that some features which are inherent in those modules (and are required on large scale applications) are not available “out of the box” in Silex . This framework (Silex) is an excellent tool to build simple web applications, but also, it can be used for software prototyping and for building RESTfull API’s.
“Micro” does not mean that the whole web application has to fit into a single file, although it certainly can. Nor does it mean that Silex is lacking in functionality. The “micro” in micro-framework means Silex aims to keep the core simple but extensible. Silex won’t make many decisions for us, such as what database to use. Almost everything is configurable by the developer, so that Silex can be everything we need and nothing we don’t. Extending Silex’ functionality is simple as registering one of its build-in ServiceProviders, also, writing our own ServiceProviders (extensions) isn’t difficult to accomplish .