It’s easy to see why it’s beneficial to use the individual components, or packages, that are
available to PHP developers. With packages, someone else is responsible for developing and
maintaining an isolated piece of code that has a well-defined job, and in theory that person has
a deeper understanding of this single component than you have time to have.
Frameworks like Laravel — and Symfony, Silex, Lumen, and Slim — prepackage a
collection of third-party components together with custom framework “glue” like
configuration files, service providers, prescribed directory structures, and application
bootstraps. So, the benefit of using a framework in general is that someone has made
decisions not just about individual components for you, but also about how those components
should fit together.
“I’ll Just Build It Myself”
Let’s say you start a new web app without the benefit of a framework. Where do you begin?
Well, it should probably route HTTP requests, so you now need to evaluate all of the HTTP
request and response libraries available and pick one. Then a router. Oh, and you’ll probably
need to set up some form of routes configuration file. What syntax should it use? Where
should it go? What about controllers? Where do they live, and how are they loaded? Well,
you probably need a dependency injection container to resolve the controllers and their
dependencies. But which one?
Furthermore, what if you do take the time to answer all those questions and successfully
create your application — what’s the impact on the next developer? What about when you
have four such custom-framework–based applications, or a dozen, and you have to remember
where the controllers live in each, or what the routing syntax is?
Consistency and Flexibility
Frameworks address this issue by providing a carefully considered answer to the question
“Which component should we use here?” and ensuring that the particular components chosen
work well together. Additionally, frameworks provide conventions that reduce the amount of
code a developer new to the project has to understand — if you understand how routing
works in one Laravel project, for example, you understand how it works in all Laravel
projects.
When someone prescribes rolling your own framework for each new project, what they’re
really advocating is the ability to control what does and doesn’t go into your application’s
foundation. That means the best frameworks will not only provide you with a solid
foundation, but also give you the freedom to customize to your heart’s content. And this, as
I’ll show you in the rest of this book, is part of what makes Laravel so special.
No comments:
Post a Comment