未加星标

Building and Testing RESTful APIs With Laravel

字体大小 | |
[开发(php) 所属分类 开发(php) | 发布者 店小二04 | 时间 2019 | 作者 红领巾 ] 0人收藏点击收藏

Experts across the globe agree that a RESTful API is among the best options available in the market for developing an interactive interface for communication between a company's data and audience. Moreover, what better framework to build RESTful APIs than Laravel. Created by Taylor Otwell, the Laravel open-source framework aims to improve the developer's efficacy by prioritizing convention over configuration. It also comes integrated with a plethora of unmatched features and novelties such as real-time communication, API authentication, job queues, and more.

Now, before we delve into the nitty-gritty of building a RESTful API with Laravel, let's quickly go through some basic facts about the latter.

REST a.k.a. Representational State Transfer refers to a programming standard for communication between applications via a stateless protocol. In RESTful APIs, resources designate endpoints while HTTP methods denote actions.

Here's a list of the actions for initiation with their mode of access:

GET: Fetch resource

POST: Build a new resource

PUT: Update resource

DELETE: Delete resource

PUT vs POST

Between POST, PUT, and sometimes even PATCH, developers are usually divided over which action to use for data storage, no matter if it is for creating a new resource or updating an existing one. Hence, to avoid any confusion, we have used PUT in this tutorial. One must remember that one of PUT's characteristic feature is its idempotence. It doesn’t matter how many requests you send using the same data; it will result in only one change.

Configuring the Project

The Laravel framework needs Composer to install and manage dependencies. Now, to install Laravel use the following command:

$ composer global require laravel/installer

Once the installation is complete, scaffold a new application in the following manner:

$ laravel new myapp

However, for this command to work, you must have ~/composer/vendor/bin in your $PATH. You can also bypass that by creating a new project with Composer. For that, use the following command:

$ composer create-project --prefer-dist laravel/laravel myapp

Moreover, after this start with the server and see if everything is working as it should with the following command:

$php artisan serve

Laravel development server started: <http://127.0.0.1:8000>

Listed below are some of the pointers to help you with the project requirement:

Database seeding: It refers to populating a database or even a database table in relational systems with test data, which is usually generated dynamically with the random principle.

Migrations: A crucial facet before you even write the first migration is creating a database for this app. Then add its credentials to the .env file available in the project's root.

Resource controller: If the project has more than one resource controller, it is advisable to create a base resource controller and then enables other resource controllers to inherit from it. It allows you to leverage the base behavior in each controller while still allowing specific actions to be overwritten for individual controllers.

At a time when single-page applications and rich internet applications (RIA) are prevalent in the market, it is imperative to utilize regular communication channels, such as RESTful APIs. Combine that with Laravel development, and you can rest assured that you have a winner on your hands!

本文开发(php)相关术语:php代码审计工具 php开发工程师 移动开发者大会 移动互联网开发 web开发工程师 软件开发流程 软件开发工程师

代码区博客精选文章
分页:12
转载请注明
本文标题:Building and Testing RESTful APIs With Laravel
本站链接:https://www.codesec.net/view/628525.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(php) | 评论(0) | 阅读(107)