megaads / apify
A pretty library to help developers build RESTful APIs lightly, quickly and properly even without writing code
Installs: 1 548
Dependents: 0
Suggesters: 0
Security: 0
Stars: 15
Watchers: 11
Forks: 11
Open Issues: 0
Requires
- php: >=5.6.4
- php-amqplib/php-amqplib: ^2.7
- dev-master
- 1.5.9
- 1.5.8
- 1.5.7
- 1.5.6
- 1.5.5
- 1.5.4
- 1.5.3
- 1.5.2
- 1.5.1
- 1.5.0
- 1.4.13
- 1.4.12
- 1.4.11
- 1.4.10
- 1.4.9
- 1.4.8
- 1.4.7
- 1.4.6
- 1.4.5
- 1.4.4
- 1.4.3
- 1.4.2
- 1.4.1
- 1.3.7
- 1.3.6
- 1.3.5
- 1.3.4
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3
- 1.2.99
- 1.2.98
- 1.2.97
- 1.2.96
- 1.2.95
- 1.2.93
- 1.2.92
- 1.2.91
- 1.2.9
- 1.2.8
- 1.2.7
- 1.2.6
- 1.2.5
- 1.2.4
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.1
- 1.1.0
- 1.0.4.1
- 1.0.4
- 1.0.3.1
- 1.0.3
- 1.0.2.1
- 1.0.2
- 1.0.1
- 1.0.0
- 0.9.45
- 0.9.4
- 0.9.3
- 0.9.2
- 0.9.1
- dev-feature-upgrade-for-laravel-11
This package is auto-updated.
Last update: 2024-11-23 04:35:41 UTC
README
A pretty library to help developers build RESTful APIs
lightly, quickly and properly even without writing code.
It's always easy to customize to suit any need such as defining data relationships, authorization, caching, communicating or integrating into other systems.
Features
- Serves RESTful APIs for any MySql database
- Pagination
- Sorting
- Selection
- Grouping, Having
- Filtering
- Relationships
- Metadata
- Supports Event Bus
Using Apify
- Apify client for PHP: https://github.com/megaads-vn/apify-client-php
- Use HTTP clients like Postman to invoke RESTful API calls.
- Combine with API Gateway is also recommended to build a completely development environment for microservice.
Installation
Apify is packed as a composer package. So it's installed quickly in 2 steps
-
Require the composer package
composer require megaads/apify
-
Register the provider:
Megaads\Apify\ApifyServiceProvider
System requirements
- PHP: >= 5.6
- Laravel/ Lumen Framework: 5.4.*
- MySQL
- Message queue server: optional
API Overview
Pagination
/api/post?page_id=2&page_size=20
Sorting
Order by multiple columns using sorts
parameter
Sort ascending
/api/post?sorts=user_id
Sort descending
/api/post?sorts=-created_at
Sort by multiple columns
/api/post?sorts=user_id,-created_at
Selection
Select columns from the records using fields
parameter. SQL aggregate functions such as COUNT
, MAX
, MIN
, SUM
, AVG
, SQL aliases are also available
/api/post?fields=id,content,user_id,sum(view_count) as view_sum
Group By
Group the result-set by one or more columns using groups
parameter and combine with aggregate functions using Selection
/api/post?fields=user_id,sum(view_count)&groups=user_id
Filtering
Apify supports filtering records based on more than one AND
, NOT
condition by using comma. For example:
/api/post?filters=user_id=1,status={enabled;pending},tile~hello,view_count!=null
Complex conditions that combine AND
, OR
and NOT
will be available soon.
Scope functions
/api/order?scopes=orderMeta(values=[adwords,123123];keys=[from,campaign_id]),customer(keyword=nguyen van nam)
namespace App\Models; class Order extends \Megaads\Apify\Models\BaseModel { public function scopeCustomer($query, $params) { if (isset($params['keyword'])) { //do something } } public function scopeOrderMeta($query, $params) { if (isset($params['keys']) && isset($params['values'])) { //do something } } }
Entity conventions
Apify works by a simple mechanism, looking for a model class that correspond to the API entity, otherwise the API entity will be matched to a suitable DB table. That means no model class is required to create, do it only in the case of defining relationships, customizing.
So API entity name should follow one of the conventions:
-
The API entity name is the same as a model class name
-
Or the API entity name in
snake_case
that correspond to a model class with the name inCamelCase
-
Or the API entity name is the same as a DB table name
Relationships
Apify is packed into a Laravel
/ Lumen
package so relationships also are defined as methods on Eloquent
model classes.
See Laravel docs for details: https://laravel.com/docs/5.6/eloquent-relationships
Let's consider the following relationship definations:
- A
Nation
has manyCity
(one-to-many relationship)
namespace App\Models; class Nation extends \Apify\Models\BaseModel { protected $table = 'location_nation'; public function cities() { return $this->hasMany('App\Models\City', 'nation_id', id); } }
- A
City
belongs to aNation
(many-to-one relationship) - A
City
has manyDistrict
(one-to-many relationship)
namespace App\Models; class City extends \Apify\Models\BaseModel { protected $table = 'location_city'; public function nation() { return $this->belongsTo('App\Models\Nation', 'nation_id'); } public function districts() { return $this->hasMany('App\Models\District', 'city_id', id); } }
- A
District
belongs to aCity
(many-to-one relationship)
namespace App\Models; class District extends \Apify\Models\BaseModel { protected $table = 'location_district'; public function city() { return $this->belongsTo('App\Models\City', 'city_id'); } }
Selection on relationships
Apify provides the ability to embed relational data into the results using embeds
parameter
For example
/api/nation?embeds=cities
/api/city?embeds=nation,districts
/api/district?embeds=city
Even nested relationships
/api/nation?embeds=cities.districts
/api/district?embeds=city.nation
Filtering on relationships
/api/city?filters=nation.location_code=EU,districts.name~land
Metric
metric=get (by default): Retrieve all records that match the query
/api/post
or
/api/post?metric=get
Response format
{ "meta": { "has_next": true, "total_count": 100, "page_count": 2, "page_size": 50, "page_id": 0 }, "result": [], "status": "successful" }
metric=first: Retrieve the first record that matchs the query
/api/post?metric=first
Response format
{ "result": {}, "status": "successful" }
metric=count: Retrieve the number of records that match the query
/api/post?metric=count
Response format
{ "result": 50, "status": "successful" }
metric=increment/ decrement: Provides convenient methods for incrementing or decrementing the value of a selected column
/api/post?metric=increment&fields=view_count
Response format
{ "result": 1, "status": "successful" }
Event Bus
Is being updated ...
.env configurations
Authenticate Apify & authorize Apify?
Read docs here : https://github.com/megaads-vn/apify/blob/master/README-AUTH.md
License
The Apify is open-sourced software licensed under the MIT license
Contact us/ Instant feedback
Email: phult.contact@gmail.com
Skype: phult.bk
If you find a bug, please report it here on Github.