Backend development often relies on the MVC (Model-View-Controller) architecture, with frameworks like Laravel's Eloquent ORM providing a solid foundation. However, scaling complex projects presents challenges: managing reusable SQL logic, adapting queries for diverse output formats, handling intricate entity relationships, and designing scalable APIs. These hurdles inspired the creation of DataForge, a Laravel-based framework that transcends the limitations of traditional MVC. DataForge champions a product-centric approach, prioritizing modularity, scalability, and maintainability.
Eloquent simplifies database interactions, but often necessitates repetitive code for varying query needs (e.g., retrieving lists versus single rows). DataForge addresses this with reusable SQL classes, enabling modular select types for efficient handling of multiple use cases.
Example:
<code class="language-php">$query = new Query('ProductList'); $query->select('list', 'p.id, p.name, p.price, c.name AS category'); $query->select('item', 'p.id, p.name, p.description, p.price, c.name AS category'); $query->select('options', 'p.id, p.name'); $query->select('total', 'COUNT(p.id) AS totalCount'); $query->filterOptional('p.category_id = {category_id}'); $query->filterOptional('p.name LIKE {%keyword%}'); $query->order('{sort}', '{order}');</code>
This allows retrieval of:
$products = Sql('Product:list', ['select' => 'list'])->fetchRowList();
$product = Sql('Product:list', ['select' => 'item', 'id' => 123])->fetchRow();
$count = Sql('Product:list', ['select' => 'total'])->fetchColumn();
Eloquent models frequently blend data access and business logic, impacting maintainability. DataForge's Entity class improves modularity by cleanly separating these concerns, incorporating advanced features:
Lazy Loading: Attributes load only when accessed, optimizing performance by avoiding unnecessary queries. $product = DataForge::getProduct(123); echo $product->Price;
(loads getPrice()
only when accessed).
Inter-Entity Connections: Effortless relationship management between entities for streamlined data retrieval. $product = DataForge::getProduct(123); $category = $product->Category;
(directly fetches the related Category entity).
Example: Product Entity
<code class="language-php">class Product extends Entity { function init($id) { return \Sql('Product:list', ['id' => $id, 'select' => 'entity'])->fetchRow(); } function getCategory() { return DataForge::getCategory($this->category_id); } }</code>
DataForge offers seven robust API endpoints, providing a structured, scalable approach to backend interaction:
Example: Paginated product list: /api/list/Product:list?keyword=test&pageNo=1&limit=10&sort=price&order=asc
Example Response (JSON):
<code class="language-php">$query = new Query('ProductList'); $query->select('list', 'p.id, p.name, p.price, c.name AS category'); $query->select('item', 'p.id, p.name, p.description, p.price, c.name AS category'); $query->select('options', 'p.id, p.name'); $query->select('total', 'COUNT(p.id) AS totalCount'); $query->filterOptional('p.category_id = {category_id}'); $query->filterOptional('p.name LIKE {%keyword%}'); $query->order('{sort}', '{order}');</code>
This modular approach simplifies API development while maintaining flexibility and scalability.
Traditional frameworks often require distinct methods for fetching lists, single rows, or aggregated data. DataForge's modular select types in SQL classes allow dynamic adjustment of queries to return the desired data format without redundant coding.
Integrating DataForge into Laravel projects offers:
filterOptional
and filterAnyOneRequired
.DataForge is a comprehensive backend toolkit designed for building structured, scalable products efficiently.
We encourage feedback and discussion on how DataForge compares to your current tools.
Let's collaborate to redefine backend development.
The above is the detailed content of Beyond MVC: Redefining Backend Development with DataForge. For more information, please follow other related articles on the PHP Chinese website!