Download the PHP package davodm/dgcontent-ci without Composer
On this page you can find all versions of the php package davodm/dgcontent-ci. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download davodm/dgcontent-ci
More information about davodm/dgcontent-ci
Files in davodm/dgcontent-ci
Package dgcontent-ci
Short Description A simple CodeIgniter 4 package to interact with DGTTeam Content API
License MIT
Informations about the package dgcontent-ci
DG Content for Codeigniter 4
A robust CodeIgniter 4 package to interact with the DGTTeam Content API, featuring caching mechanisms, secure API key management via environment variables.
Features
- Seamless API Integration: Interact with the DGTTeam Content API effortlessly.
- Caching Mechanism: Reduce API calls and improve performance with customizable caching.
- Secure Configuration: Manage API keys and sensitive data using environment variables.
- Flexible Usage: Easily fetch posts, single posts, categories, and update post statistics.
- Error Handling: Gracefully handle API errors with meaningful messages and logging.
- Extensible: Easily extend the package to include additional API endpoints or features.
Prerequisites
Before integrating DGContent into your project, ensure you have the following:
- PHP >= 7.4
- Composer installed globally.
- CodeIgniter 4 project set up.
- Familiarity with OOP PHP and CodeIgniter 4.
Installation
You can install the package via Composer. Run the following command in your CodeIgniter 4 project directory:
Configuration
- Environment Variables Store sensitive information like the API key in your .env file to keep it secure and out of version control.
Add the following entries to your .env file:
- Configuration File
The package comes with a configuration file located at
src/Config/DGContentCI.php
. You can customize default settings by modifying this file if necessary.
Usage
You can initialize the library directly in your controller.
Functions
The package provides the following functions to interact with the DGTTeam Content API:
getPosts($params)
Fetch posts based on the specified parameters and return total
(as total number for pagination) and posts
array.
Parameters:
limit
: Number of posts to fetch (default: 10).offset
: Offset for pagination (default: 0).category
: Filter posts by category slug. could be an array or string with comma separated values. (optional)tags
: Filter posts by tags. could be an array or string with comma separated values. (optional)language
: Filter posts by language code e.g. fa, en, es, fr. (optional)
Returns: An array with total
(total number of posts) and posts
array containing post objects.
getPost($params)
Fetch a single post by ID or slug and return the post object.
Parameters:
id
: Post ID. (one ofid
orslug
is required)slug
: Post slug.
Returns: A single post object.
getPage($slug)
Fetch a single page by slug and return the page object.
Returns: A single page object.
getCategories(bool $websiteOnly = false)
Fetch all available categories. If $websiteOnly
is set to true
, it will only return categories that are attached to the website specified in the environment variables.
Returns: An array of category objects.
updateStats(string $type, string $id, int $count=1)
Update post statistics (views, likes, dislikes).
Parameters:
type
: Type of statistic to update (views, likes, dislikes).id
: Post ID.count
: Number of counts to add (default: 1).
Returns: true
if the statistics are updated successfully, false
otherwise.
setCacheDuration(int $duration)
Set the cache duration for API responses in seconds.
setProcessResult(bool $processResult)
Set whether to process the API response before returning it. Default is true
.
clearCache()
Clear the cache for the API responses.
Post Object
The post object returned by the getPosts
and getPost
functions contains the following properties, which may vary based on the API response:
Error Handling
The package gracefully handles API errors and logs them for debugging purposes. If an error occurs during API requests, the package will throw an exception with a meaningful error message and you can catch it in your application to log or display the error.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Author
Davod Mozafari - Twitter