Book Image

Getting Started with Laravel 4

By : Raphaël Saunier
Book Image

Getting Started with Laravel 4

By: Raphaël Saunier

Overview of this book

<p>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.</p> <p>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.</p> <p>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.</p> <p>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.</p>
Table of Contents (15 chapters)
Getting Started with Laravel 4
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Don't wait any longer with queues


Queues allow you to defer the execution of functions without blocking the script. They can be used to run all sorts of functions, from e-mailing a large number of users to generating PDF reports.

As of Version 4.1, Laravel is compatible with the following queue drivers:

  • Beanstalkd, with the pda/pheanstalk package

  • Amazon SQS, with the aws/aws-sdk-php package

  • IronMQ, with the iron-io/iron_mq package

Each queue system has its advantages. Beanstalkd can be installed on your own server; Amazon SQS might be more cost-effective and require less maintenance, as will IronMQ, which is also cloud-based. The latter also lets you set up push queues, which are great if you cannot run background jobs on your server.

Creating a job and pushing it onto the queue

A job is nothing more than a class with a fire method that accepts two parameters for the name of the job and the data and that either deletes the job or releases it back to the queue:

class Job { 
  public function fire($job, $data){
    // Perform job…
    // If the job was successful, delete it
    $job-delete(); 
    // Put it back onto the queue and try to execute it again
    $job->release($seconds); 
  } 
}

Push a job onto the queue. When called, this will execute the fire method:

Queue::push('Job', $data);

Push a job onto the queue but execute a different method:

Queue::push('Job@method', $data);

You can store your jobs anywhere in your application folder as long as the class is resolvable (see Chapter 7, Architecting Ambitious Applications). A potential location would be app/jobs. When developing, it is also fine to push anonymous functions onto the queue:

Queue::push(function($job) use ($vars, $from, $outer, $scope){
  // Perform job
});

Listening to a queue and executing jobs

The following are the functions used for listening to a queue and executing jobs:

  • We can listen to the default queue:

    $ php artisan queue:listen
  • We can specify the connection on which to listen:

    $ php artisan queue:listen connection
  • We can specify multiple connections in the order of their priority:

    $ php artisan queue:listen important,not-so-important

The queue:listen command has to run in the background in order to process the jobs as they arrive from the queue. To make sure that it runs permanently, you have to use a process control system such as forever (https://github.com/nodejitsu/forever) or supervisor (http://supervisord.org/).

Getting notified when a job fails

To get notified when a job fails, we use the following functions and commands:

  • The following event listener is used for finding the failed jobs:

    Queue::failing(function($job, $data) {
      // Send email notification
    });
  • Any of the failed jobs can be stored in a database table and viewed with the following commands:

    $ php artisan queue:failed-table // Create the table 
    $ php artisan queue:failed // View the failed jobs
    

Queues without background processes

Push queues do not require a background process but they only work with the iron.io driver. After signing up for an account on iron.io and adding your credentials to app/config/queue.php, you use them by defining a POST route that receives all the incoming jobs. This route calls Queue::marshal(), which is the method responsible for firing the correct job handler:

Route::post('queue/receive', function() {
  return Queue::marshal();
});

This route then needs to be registered as a subscriber with the queue:subscribe command:

$ php artisan queue:subscribe queue_name http://yourapp.example.com/queue/receive

Once the URL is subscribed on http://iron.io/, any newly created jobs with Queue::push() will be sent from Iron back to your application via a POST request.