Chapter 6. Taming Complexity with Annotations

In the previous chapter, you learned how to create a RESTful API that involved receiving a request from the Internet, routing it to the controllers, and processing it. In this chapter, you will learn how to use annotations in the DocBlock, a way of performing routing that requires even less code and can be a faster and more organized way of doing collaborative programming with a team.

Annotations will be demonstrated for:

  • Routing of HTTP requests such as GET, POST, and PUT
  • Turning a controller into a fully enabled CRUDL resource
  • Listening to events that are fired from commands
  • Adding middleware to controllers to limit or filter requests

Annotations are great mechanisms used in programming. Annotations are metadata that enhance other data. Since this may seem slightly confusing, so we need to first understand what the meaning of metadata is. The word metadata is a word that contains two parts:

  • meta: This is a Greek word that means transcending or encompassing.
  • data: This is a Latin word that means pieces of information.

Thus, metadata serves to enhance or extend the meaning of something.

Annotations in other programming languages

Next, we will discuss annotations that are used in computer programming. We will take a look at several examples from Java, C#, and PHP, and then finally, take a look at how annotations are used in Laravel.

Annotations in Java

Annotations were first proposed in Java Version 1.1 and added in Version 1.2. The following is an example of an annotation that is used to override an animal's speak method:

Java 1.2
/**
 * @author      Jon Doe <[email protected]>
 * @version     1.6               (current version number)
 * @since       2010-03-31        (version package...)
 */
public void speak() {
}

public class Animal {
    public void speak() {
    }
} 
public class Cat extends Animal {
    @Override
    public void speak() {
        System.out.println("Meow.");
    }
 }

Note that the @ symbol is used to signal the compiler that this annotation, @Override, is important.

Annotations in C#

In C#, annotations are called attributes and use square brackets instead of the more often used @ symbol:

[AttributeUsageAttribute(AttributeTargets.Property|AttributeTargets.Field, AllowMultiple = false, Inherited = true)]
public sealed class AssociationAttribute : Attribute

Annotations in PHP

Other PHP frameworks use annotations. Symfony makes extensive use of annotations. In Doctrine, which is Symfony's ORM and is similar to Laravel's Eloquent, annotations are used to define relationships. Symfony also uses annotations for routing. The Zend Framework (ZF) uses annotations as well. Both the testing tools Behat and PHPUnit use annotations. In the following example of Behat, an annotation is used to indicate that this method should be executed before the test suite:

/**
 * @BeforeSuite
 */
public static function prepare(SuiteEvent $event)
{
// prepare system for test suite
// before it runs
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset