Pair program with me! profile for carousel at Stack Overflow, Q&A for professional and enthusiast programmers

2/22/2014

Symfony in Laravel

Last couple of days i spent reading this tutorial. It is Fabien Potencier (creator of Symfony) online guide on how to create your own PHP framework on top of Symfony components. 

The reason I was reading this is because Laravel is heavily dependent on Symfony. I was trying to find out why Taylor Otwell (Laravel creator) choose Symfony to rely on.

I also watched some Fabien Potenciers video presentations and read some of his articles. I have to say, that this guy is not ordinary PHP geek, who is just a creator of one successful framework. There are some really great ideas in his talks and I highly recommend you to read something like this article. I was impressed wich some ideas like MVC is not a part of web, and Symfony doesn't have domain logic layer. If you thinks about this, there is a lot of truth inside. I found myself beeing amazed how much I learned from Symfony. 

So lets see what and why is Laravel using. The most important part of Symfony in Laravel is a HTTPKernelInterface. As a developer you know that PHP is interacting with server through SAPI (server application  programming interface). SAPI in PHP is basically implemented in the form of header and echo method. Through SAPI, PHP is sending its response to server. On Apache, default SAPI for PHP currently is Apache2handler. There is one more interface, for interacting with server - CLI SAPI or command-line interface. This is how PHP interact with server through command line. HTTPKernelInterface is build on top of idea that PHP needs better way to interact with server and clients. HTTPKernelInterface has just one method - handle. And that is all. Good programming practice suggest that interfaces should follow interface segregation principle. That principle states that no client should depend on the method that it doesn't use. So, that why there is only one method inside, that should provide all response logic.

Laravel is implementing this interface in the main Application class.There is a lot of things happening inside this implementation - from route collection lookup, to custom response. When you run app->run method, you are triggering stack that will end with returning response to client. Application has some bootstrap process before method run is triggered, but this method is the heart of Laravel HTTP handling logic.

It should be no surprise that both - Laravel request and response  are extensions of Symfony HTTPFoundation classes. So, Laravel is built on top of Symfony - I guess Taylor Otwell was first reading that tutorial :)

Web appllications are complex to develop, due to HTTP stateless nature.
On one end, we as developers have to take care about transport mechanism (protocol), and on another end we have to deal with domain logic (MVC). On native deskop applications, there is no HTTP. Everything is managed locally, and you can develop application in one consistent environment. I think that is the main reason for popularity of Single Page Web Applications. They are trying to simulate desktop environment, with moving most of logic to client. We will see how that trend is going to evolve.

Symfony ecosystem is huge. It is established PHP framework, so Laravel is gaining much of its strength from Symfony. So, if you want to learn more about Laravel, take some time and git clone Symfony. Besides weird yaml format (Laravel is using json) and some Symfony specific configurations, you can learn something new about architecture and design.

Thanks for reading. 



No comments:

Post a Comment