O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Getting Started with Laravel 4

Book Description

Ideal for web developers who want a crash course in Laravel, this tutorial covers all the fundamentals to help you build robust applications with less code. The approach is practical with masses of real-world examples.

In Detail

PHP powers many of the largest websites on the planet. Yet, even though it was specifically created for the Web, its shortcomings never cease to frustrate developers. This is where a tool like Laravel comes in. Rather than reinventing the wheel, Laravel reuses tried and tested components and principles and bundles them to form a cohesive whole and makes PHP development enjoyable again.

Getting Started with Laravel 4 is a practical and concise introduction to the Laravel PHP framework. It covers its fundamental concepts and presents the many features that will boost your productivity when developing web applications. After introducing the key concepts and installing Composer, you will build a CRUD application and add more features to it in each successive chapter.

This book introduces you to a different and more enjoyable way of writing PHP applications. You will start by learning about the key principles and the same development practices that Laravel encourages. Then, in subsequent chapters, you will create and successively add more features to a web application.

You will learn how to use the arsenal of tools at your disposal and probably pick up some useful techniques along the way. Indeed, everything you will learn in this book is highly transferrable and applicable to other MVC frameworks. Laravel's routing mechanism, templating language, and object-relational mapper will have no more secrets for you. You will learn how to authenticate users, write tests, and create command line utilities that interact with your application with disconcerting ease. In addition to this, you will probably be surprised by the simplicity and expressiveness of your code.

What You Will Learn

  • Install Laravel and other PHP packages with Composer
  • Sketch out and structure your application with routes and controllers
  • Create hierarchical templates with Blade
  • Define models and how to issue database queries in PHP with Eloquent
  • Authenticate users securely with minimal effort
  • Avoid security issues and write bulletproof code
  • Develop with confidence with the help of tests
  • Set up and create powerful command line utilities

Table of Contents

  1. Getting Started with Laravel 4
    1. Table of Contents
    2. Getting Started with Laravel 4
    3. Credits
    4. About the Author
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers and more
        1. Why Subscribe?
        2. Free Access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. Meeting Laravel
      1. The need for frameworks
        1. The limitations of homemade tools
        2. Laravel to the rescue
      2. A new approach to developing PHP applications
        1. A more robust HTTP foundation
        2. Embracing PHP
      3. Laravel's main features and sources of inspiration
        1. Expressiveness and simplicity
          1. Prettifying PHP
        2. Responsibilities, naming, and conventions
        3. Helping you become a better developer
      4. Structure of a Laravel application
        1. The application container and request lifecycle
        2. Exploring Laravel
        3. Moving from Version 3 to Version 4
      5. Summary
    9. 2. Composer All Over
      1. Working with the command line
      2. How does Composer work?
      3. Installation
        1. Unix (Mac OS, Linux)
        2. Windows
      4. Creating a new Laravel application
      5. Finding and installing new packages
      6. Additional advice
      7. Summary
    10. 3. Your First Application
      1. Sketching out the application
        1. Entities, relationships, and attributes
        2. The map of our application
      2. Starting the application
        1. Using the built-in development server
      3. Writing the first routes
        1. Restricting the route parameters
        2. Catching the missing routes
        3. Handling redirections
        4. Returning views
      4. Preparing the database
        1. Creating the Eloquent models
        2. Building the database schema
        3. Seeding the database
      5. Mastering Blade
        1. Creating a master view
      6. Back to the routes
        1. The overview page
        2. Displaying a cat's page
        3. Adding, editing, and deleting cats
      7. Summary
    11. 4. Authentication and Security
      1. Authenticating users
        1. Creating the user model
        2. Creating the necessary database schema
        3. Authentication routes and views
        4. Validating user input
      2. Securing your application
        1. Cross-site request forgery
        2. Escaping content to prevent cross-site scripting – XSS
        3. Avoiding SQL injection
        4. Using mass-assignment with care
        5. Cookies – secure by default
        6. Forcing HTTPS when exchanging sensitive data
      3. Summary
    12. 5. Testing – It's Easier Than You Think
      1. The benefits of testing
      2. The anatomy of a test
      3. Unit testing with PHPUnit
        1. Defining what you expect with assertions
        2. Preparing the scene and cleaning up objects
        3. Expecting exceptions
        4. Testing interdependent classes in isolation
      4. End-to-end testing
        1. Testing – batteries included
        2. Framework assertions
        3. Impersonating users
        4. Testing with a database
        5. Inspecting the rendered views
      5. Summary
    13. 6. A Command-line Companion Called Artisan
      1. Keeping up with the latest changes
      2. Inspecting and interacting with your application
        1. Fiddling with the internals
        2. Turning the engine off
        3. Fine-tuning your application
      3. Installing third-party commands
        1. Speeding up your workflow with generators
          1. Generating migrations
          2. Generating HTML forms
          3. Generating everything else
        2. Deploying with a single command
          1. Deployment, the old-school way
      4. Rolling out your own artisan commands
        1. Creating the command
        2. The anatomy of a command
        3. Writing the command
      5. Summary
    14. 7. Architecting Ambitious Applications
      1. Moving from simple routing to powerful controllers
        1. Favoring explicit routing
        2. Straightforward REST routing
      2. Supercharging your models
        1. Simple performance tricks
          1. Eager loading records
          2. Selecting only what you need
          3. Profiling your queries
        2. Foolproof models with soft deletes
        3. More control with SQL
        4. Listening for model events
        5. The handy paginator class
      3. Environment configuration made easy
        1. Environments and Artisan
        2. Adding your own configuration settings
      4. Bringing in your own classes
      5. Playing nice with the frontend
      6. Summary
    15. A. An Arsenal of Tools
      1. Array helpers
        1. The usage examples of array helpers
      2. String and text manipulation
        1. Boolean functions
        2. Transformation functions
        3. Inflection functions
      3. Dealing with files
        1. File uploads
        2. File manipulation methods
      4. Sending e-mails
      5. Easier date and time handling with Carbon
        1. Instantiating Carbon objects
        2. Outputting user-friendly timestamps
        3. Boolean methods
        4. Carbon for Eloquent DateTime properties
      6. Don't wait any longer with queues
        1. Creating a job and pushing it onto the queue
        2. Listening to a queue and executing jobs
        3. Getting notified when a job fails
        4. Queues without background processes
      7. Where to go next?
    16. Index