Laravel is great but... where's the consideration for HATEOAS?

Asked
Active3 hr before
Viewed126 times

3 Answers

greatlaravel
90%

It is much easier to manage dependencies in Laravel. Let’s consider the same example:,Service Container is a great component, and it helps to build your application following SOLID design principles.,Symfony 2 was released in 2011, but it must not be confused with Symfony 1, which was a totally different framework with different underlying principles. Fabien Potencier created Symfony 2, and the current version is 3.2, which is an incremental version of Symfony 2. Therefore, they are often called simply Symfony2/3.,There’s no clear winner between Laravel and Symfony, as everything depends on your final goal.

Symfony installation is as simple as the following:

# Downloading Symfony installer
sudo curl - LsS https: //symfony.com/installer -o /usr/local/bin/symfony
   # Granting permissions to execute installer
sudo chmod a + x / usr / local / bin / symfony
# Creating new Symfony project
symfony new symfony_project
# Launching built - in server
cd symfony_project / && php bin / console server: start
load more v
88%

Most projects these days are written using level 2. If we would like to go for the perfect RESTful API, we should consider HATEOAS.,Learn new tools and approaches with the community of tech enthusiasts,This is the response we get after invoking such controller:,Summarizing the HATEOAS concept, it consists of a few pros and cons.

@Entity
public class Movie {
   @Id
   @GeneratedValue
   private Long id;
   private String title;
   private int year;
   private Rating rating;
   @ManyToOne
   private Director director;
}

@Entity
public class Director {
   @Id
   @GeneratedValue
   @Getter
   private Long id;
   @Getter
   private String firstname;
   @Getter
   private String lastname;
   @Getter
   private int year;
   @OneToMany(mappedBy = "director")
   private Set<Movie> movies;
}
load more v
72%

HATEOAS allows you to expose the authorization logic of your REST API. This package makes it easy to add HATEOAS links to your Laravel API resources. ,The package defines an artisan command for creating a new HATEOAS class, which contains methods that will generate links JSON responses:,You can also checkout the article for executing single migration by clicking on the link below.,Laravel provides user authentication package to manage complete authentication like User Register, Login, Forgot Password, Email Verification. UI Auth…

The package defines an artisan command for creating a new HATEOAS class, which contains methods that will generate links JSON responses:

class MessageHateoas {
   use CreatesLinks;

   /**
    * Get the HATEOAS link to view the message.
    *
    * @param \App\Message $message
    *
    * @return null|\GDebrauwer\Hateoas\Link
    */
   public
   function self(Message $message) {
      if (!auth() - > user() - > can('view', $message)) {
         return;
      }

      return $this - > link('message.show', ['message' => $message]);
   }

   /**
    * Get the HATEOAS link to delete the message.
    *
    * @param \App\Message $message
    *
    * @return null|\GDebrauwer\Hateoas\Link
    */
   public
   function delete(Message $message) {
      if (!auth() - > user() - > can('delete', $message)) {
         return $this - > link('message.archive', ['message' => $message]);
      }

      return $this - > link('message.destroy', ['message' => $message]);
   }
}

Then in your Laravel resources class, you can include the links using this package’s HasLinks trait:

class MessageResource extends JsonResource {
   use HasLinks;

   /**
    * Transform the resource into an array.
    *
    * @param \Illuminate\Http\Request $request
    *
    * @return array
    */
   public
   function toArray($request) {
      return [
         'id' => $this - > id,
         'text' => $this - > text,
         '_links' => $this - > links(),
      ];
   }
}

 Finally, the result of the above example might look like the following JSON response:

{
   "data": [{
      "id": 1,
      "text": "Hello world!",
      "_links": [{
            "rel": "self",
            "type": "GET",
            "href": "http://localhost/message/1"
         },
         {
            "rel": "delete",
            "type": "DELETE",
            "href": "http://localhost/message/1"
         }
      ]
   }]
}
load more v