Getting Started With Laravel 4, Chapter 1: Meeting Laravel

The need for frameworks

Out of the box PHP is easy to use, but it is prone to allow mistakes by beginners.  It doesn't lend itself to separation of concerns.  If you have ever worked on an old PHP project, you've probably spent more time screaming and yelling at the developer than you did writing code.  This is why frameworks are useful and needed.

The limitations of homemade tools

If you have done any significant projects in PHP.  You probably have amassed a pile of files and functions that you carry over from one project to the next.  This is another reason why we need frameworks.  Frameworks allow other developers to be able to jump in immediately and start helping write code.

Laravel to the rescue

This is where Laravel comes to the rescue.  It is an MVC (model-view-controller) paradigm.

A new approach to developing PHP applications

PHP is better than Ruby and Python for Web development.  Both of those languages have had classes and frameworks added on to make development easier, but neither was developed for the web.

A more robust HTTP function

Laravel uses Symfony and a bunch of other libraries to make life easier.  It uses Swiftmailer for emails, Carbon for dates and times, and Doctrine for abstraction.
Links:

Embracing PHP 

  • Namespaces: allow us to avoid collisions when functions from different libraries have the same name
  • Interfaces: (aka Contracts) define methods that a class should provide
  • Anonymous functions: (aka Closures) reminiscent of JavaScript, they help to produce shorter code, and are used in multiple places when building a Laravel app
  • Overloading: (aka dynamic or magic) allows you to call methods that are not defined in the class
  • Shorter Array Syntax: This book will use the old syntax, but there is a new shorter syntax for creating arrays

Laravel's main features and sources of inspiration

  • Modularity: built on 20 different libraries, and relies on Composer Dependency Manager, which makes it easy to keep updated.
  • Testability: built from the ground up with testing in mind
  • Routing: lots of flexibility in route defining, takes inspiration from Silex and Sinatra
  • Configuration management: lets you define different settings for different environments
  • Query Builder and ORM: ships with Eloquent, has built in ORM (object relational mapper) and works with most of the popular databases
  • Schema Builder, migrations, and seeding: Keeps track of database changes and allows you to populate selected tables
  • Template Engine: Ships with Blade, a lightweight template language
  • Emailing: Ships with SwiftMailer, wrapped up in a new MAIL class
  • Authentication: provides tools to make this much easier, and more secure than trying to roll your own.
  • Redis: Works with Redis.  
  • Queues: integrateswith several queue services to allow large tasks to run in the background
Links:

Expressions and simplicity

Classes have been named to effectively convey what they do in plain English.

Prettifying PHP

  • More expressive: older functions have been wrapped to make them more intuitive
  • More consistent: older functions have been wrapped to make the names more consistent
  • More feature complete: new functions have been added to plug holes that the existing ones don't cover

Responsibilities, naming and conventions

 Laravel gives you predefined places to put your code, thereby enforcing separation.  No more business logic in your views.
  • Models: Most often ends up being the tables in your database.
  • Controllers or routes
    Standard Controllers: 
    like the controllers in Codeigniter.  Allows you to call them like /controller.php/params
    Resource Controllers:
    allows RESTful routing using HTTP Verbs
  •  Views or Templates: can be built using the Blade template language or can be standard php views

Helping you become a better developer

Just learning Laravel, will teach you the skills you need to be code in other PHP frameworks, and also in other languages.

Structure of a Laravel application

The structure should be intuitive and the naming structure makes this true.

The application container and request life cycle

How an HTTP Request gets routed in Laravel can be a mystery.  The biggest thing to remember, when a request comes in, it looks in /public.  It then uses the Autoloader class to load the class it needs.

Exploring Laravel

The best way to learn about Laravel is to browse the API.
Links:

Moving from Version 3 to Version 4

  • packages are the new bundles.  Older bundles won't work with Laravel 4
  • Composer all over
  • new coding conventions
  • Dependency injections
  • Documentation
  • Renamed methods

Comments

Popular posts from this blog

Go Programming Blueprints, Chapter 2, Adding User Accounts

Successful Big Game Hunting Chapter 10