在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):nilportugues/laravel5-jsonapi开源软件地址(OpenSource Url):https://github.com/nilportugues/laravel5-jsonapi开源编程语言(OpenSource Language):PHP 100.0%开源软件介绍(OpenSource Introduction):Laravel 5 JSON API Server PackageCompatible with Laravel 5.0, 5.1 & 5.2
InstallationUse Composer to install the package:
Now run the following artisan command:
Configuration (Laravel 5 & Lumen)For the sake of having a real life example, this configuration will guide you on how to set up 7 end-points for two resources, Both Furthermore, Configuration for Laravel 5Step 1: Add the Service ProviderOpen up 'providers' => [
//...
NilPortugues\Laravel5\JsonApi\Laravel5JsonApiServiceProvider::class,
], Step 2: Defining routesWe will be planning the resources ahead its implementation. All routes require to have a name. This is how our <?php
Route::group(['namespace' => 'Api'], function() {
Route::resource('employees', 'EmployeesController');
Route::get(
'employees/{employee_id}/orders', [
'as' => 'employees.orders',
'uses' => 'EmployeesController@getOrdersByEmployee'
]);
});
//... Step 3: DefinitionFirst, let's define the Models for Employees (Eloquent Model) <?php namespace App\Model\Database;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Validation\ValidatesRequests;
class Employees extends Model
{
public $timestamps = false;
protected $table = 'employees';
protected $primaryKey = 'id';
protected $appends = ['full_name'];
/**
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function latestOrders()
{
return $this->hasMany(Orders::class, 'employee_id')->limit(10);
}
/**
* @return string
*/
public function getFullNameAttribute()
{
return $this->first_name.' '.$this->last_name;
}
} Employees SQL CREATE TABLE `employees` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`company` varchar(50) DEFAULT NULL,
`last_name` varchar(50) DEFAULT NULL,
`first_name` varchar(50) DEFAULT NULL,
`email_address` varchar(50) DEFAULT NULL,
`job_title` varchar(50) DEFAULT NULL,
`business_phone` varchar(25) DEFAULT NULL,
`home_phone` varchar(25) DEFAULT NULL,
`mobile_phone` varchar(25) DEFAULT NULL,
`fax_number` varchar(25) DEFAULT NULL,
`address` longtext,
`city` varchar(50) DEFAULT NULL,
`state_province` varchar(50) DEFAULT NULL,
`zip_postal_code` varchar(15) DEFAULT NULL,
`country_region` varchar(50) DEFAULT NULL,
`web_page` longtext,
`notes` longtext,
`attachments` longblob,
PRIMARY KEY (`id`),
KEY `city` (`city`),
KEY `company` (`company`),
KEY `first_name` (`first_name`),
KEY `last_name` (`last_name`),
KEY `zip_postal_code` (`zip_postal_code`),
KEY `state_province` (`state_province`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
INSERT INTO `employees` (`id`, `company`, `last_name`, `first_name`, `email_address`, `job_title`, `business_phone`, `home_phone`, `mobile_phone`, `fax_number`, `address`, `city`, `state_province`, `zip_postal_code`, `country_region`, `web_page`, `notes`, `attachments`)
VALUES
(10, 'Acme Industries', 'Smith', 'Mike', '[email protected]', 'Horticultarlist', '0118 9843212', NULL, NULL, NULL, '343 Friary Road', 'Manchester', 'Lancs.', 'M3 3DL', 'United Kingdom', NULL, NULL, NULL);
Orders (Eloquent Model) <?php namespace App\Model\Database;
use Illuminate\Database\Eloquent\Model;
class Orders extends Model
{
public $timestamps = false;
protected $table = 'orders';
protected $primaryKey = 'id';
/**
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function employee()
{
return $this->belongsTo(Employees::class, 'employee_id');
}
} Orders SQL CREATE TABLE `orders` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`employee_id` int(11) DEFAULT NULL,
`customer_id` int(11) DEFAULT NULL,
`order_date` datetime DEFAULT NULL,
`shipped_date` datetime DEFAULT NULL,
`shipper_id` int(11) DEFAULT NULL,
`ship_name` varchar(50) DEFAULT NULL,
`ship_address` longtext,
`ship_city` varchar(50) DEFAULT NULL,
`ship_state_province` varchar(50) DEFAULT NULL,
`ship_zip_postal_code` varchar(50) DEFAULT NULL,
`ship_country_region` varchar(50) DEFAULT NULL,
`shipping_fee` decimal(19,4) DEFAULT '0.0000',
`taxes` decimal(19,4) DEFAULT '0.0000',
`payment_type` varchar(50) DEFAULT NULL,
`paid_date` datetime DEFAULT NULL,
`notes` longtext,
`tax_rate` double DEFAULT '0',
`tax_status_id` tinyint(4) DEFAULT NULL,
`status_id` tinyint(4) DEFAULT '0',
PRIMARY KEY (`id`),
KEY `customer_id` (`customer_id`),
KEY `employee_id` (`employee_id`),
KEY `id` (`id`),
KEY `shipper_id` (`shipper_id`),
KEY `tax_status` (`tax_status_id`),
KEY `ship_zip_postal_code` (`ship_zip_postal_code`),
KEY `fk_orders_orders_status1` (`status_id`),
CONSTRAINT `fk_orders_employees1` FOREIGN KEY (`employee_id`) REFERENCES `employees` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=82 DEFAULT CHARSET=utf8;
INSERT INTO `orders` (`id`, `employee_id`, `customer_id`, `order_date`, `shipped_date`, `shipper_id`, `ship_name`, `ship_address`, `ship_city`, `ship_state_province`, `ship_zip_postal_code`, `ship_country_region`, `shipping_fee`, `taxes`, `payment_type`, `paid_date`, `notes`, `tax_rate`, `tax_status_id`, `status_id`)
VALUES
(82, 10, NULL, '2015-03-12 00:00:00', '2015-03-12 00:00:00', NULL, NULL, '43, Borrowed Drive', 'New Oreleans', 'Louisiana', '4322', 'USA', 1.4000, 0.0000, NULL, NULL, NULL, 0, NULL, 0);
Follow up, we'll be creating Transformers. One Transformer is required for each class and it must implement the We will be placing these files at EmployeesTransformer <?php namespace App\Model\Api;
use App\Model\Database\Employees;
use NilPortugues\Api\Mappings\JsonApiMapping;
class EmployeesTransformer implements JsonApiMapping
{
/**
* Returns a string with the full class name, including namespace.
*
* @return string
*/
public function getClass()
{
return Employees::class;
}
/**
* Returns a string representing the resource name
* as it will be shown after the mapping.
*
* @return string
*/
public function getAlias()
{
return 'employee';
}
/**
* Returns an array of properties that will be renamed.
* Key is current property from the class.
* Value is the property's alias name.
*
* @return array
*/
public function getAliasedProperties()
{
return [
'last_name' => 'surname',
];
}
/**
* List of properties in the class that will be ignored by the mapping.
*
* @return array
*/
public function getHideProperties()
{
return [
'attachments'
];
}
/**
* Returns an array of properties that are used as an ID value.
*
* @return array
*/
public function getIdProperties()
{
return ['id'];
}
/**
* Returns a list of URLs. This urls must have placeholders
* to be replaced with the getIdProperties() values.
*
* @return array
*/
public function getUrls()
{
return [
'self' => ['name' => 'employees.show', 'as_id' => 'id'],
'employees' => ['name' => 'employees.index'],
'employee_orders' => ['name' => 'employees.orders', 'as_id' => 'id']
];
}
/**
* Returns an array containing the relationship mappings as an array.
* Key for each relationship defined must match a property of the mapped class.
*
* @return array
*/
public function getRelationships()
{
return [];
}
} Same goes for OrdersTransformer <?php namespace App\Model\Api;
use App\Model\Database\Orders;
use NilPortugues\Api\Mappings\JsonApiMapping;
class OrdersTransformer implements JsonApiMapping
{
/**
* {@inheritDoc}
*/
public function getClass()
{
return Orders::class;
}
/**
* {@inheritDoc}
*/
public function getAlias()
{
return 'order';
}
/**
* {@inheritDoc}
*/
public function getAliasedProperties()
{
return [];
}
/**
* {@inheritDoc}
*/
public function getHideProperties()
{
return [];
}
/**
* {@inheritDoc}
*/
public function getIdProperties()
{
return ['id'];
}
/**
* {@inheritDoc}
*/
public function getUrls()
{
return [
'self' => ['name' => 'orders.show', 'as_id' => 'id'],
'employee' => ['name' => 'employees.show', 'as_id' => 'employee_id'],
];
}
/**
* {@inheritDoc}
*/
public function getRelationships()
{
return [];
}
/**
* List the fields that are mandatory in a persitence action (POST/PUT).
* If empty array is returned, all fields are mandatory.
*/
public function getRequiredProperties()
{
return [];
}
} Step 4: UsageCreate file <?php
use App\Model\Api\EmployeesTransformer;
use App\Model\Api\OrdersTransformer;
return [
EmployeesTransformer::class,
OrdersTransformer::class,
]; Configuration for LumenStep 1: Add the Service ProviderOpen up $app->register(\NilPortugues\Laravel5\JsonApi\Laravel5JsonApiServiceProvider::class);
$app->configure('jsonapi'); Also, enable Facades by uncommenting: $app->withFacades(); Step 2: Defining routesWe will be planning the resources ahead its implementation. All routes require to have a name. This is how our <?php
$app->group(
['namespace' => 'Api'], function($app) {
$app->get(
'employees', [
'as' => 'employees.index',
'uses' =>'EmployeesController@index'
]);
$app->post(
'employees', [
'as' => 'employees.store',
'uses' =>'EmployeesController@store'
]);
$app->get(
'employees/{employee_id}', [
'as' => 'employees.show',
'uses' =>'EmployeesController@show'
]);
$app->put(
'employees/{employee_id}', [
'as' => 'employees.update',
'uses' =>'EmployeesController@update'
]);
$app->patch(
'employees/{employee_id}', [
'as' => 'employees.patch',
'uses' =>'EmployeesController@update'
]);
$app->delete(
'employees/{employee_id}', [
'as' => 'employees.destroy',
'uses' =>'EmployeesController@destroy'
]);
$app->get(
'employees/{employee_id}/orders', [
'as' => 'employees.orders',
'uses' => 'EmployeesController@getOrdersByEmployee'
]);
}
);
//... Step 3: DefinitionSame as Laravel 5. Step 4: UsageSame as Laravel 5. JsonApiControllerWhether it's Laravel 5 or Lumen, usage is exactly the same. Let's create a new controller that extends the Lumen users must extends from 全部评论
专题导读
上一篇:GeneaLabs/laravel-model-caching: Eloquent model-caching made easy.发布时间:2022-07-07下一篇:arrilot/laravel-widgets: Widgets for Laravel发布时间:2022-07-07热门推荐
热门话题
阅读排行榜
|
请发表评论