PHPackages                             rateb/structure - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Search &amp; Filtering](/categories/search)
4. /
5. rateb/structure

ActiveLibrary[Search &amp; Filtering](/categories/search)

rateb/structure
===============

strucure to filter and search

v2.8(1y ago)140MITPHPPHP &gt;=8.1

Since Oct 5Pushed 1y ago1 watchersCompare

[ Source](https://github.com/RatebAlsaour/laravel-structure-craft)[ Packagist](https://packagist.org/packages/rateb/structure)[ RSS](/packages/rateb-structure/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)DependenciesVersions (19)Used By (0)

laravel-structure-craft
=======================

[](#laravel-structure-craft)

- Package for Organizing Code with Design Patterns and SOLID Principles -This package is designed to help structure your code using the Repository and DTO (Data Transfer Object) patterns. It also implements SOLID principles to maintain clean, maintainable, and scalable business logic, especially for handling search and filter operations.

Table of Contents
-----------------

[](#table-of-contents)

- [Installation](#installation)
- [Usage](#usage)
- [Contact](#contact)

Getting started
---------------

[](#getting-started)

### Installation

[](#installation)

To install the package, run the following command:

```
composer require rateb/structure
```

Registering the Service Provider
--------------------------------

[](#registering-the-service-provider)

To use the features provided by the `rateb-structure` package, you need to register the `StructureServiceProvider`. Follow these steps:

1. Open the `config/app.php` file in your Laravel project.
2. Locate the `providers` array in the file.
3. Add the following line to the array:

    ```
    \RatebSa\Structure\StructureServiceProvider::class,
    ```

Example of the Providers Array
------------------------------

[](#example-of-the-providers-array)

After adding the `StructureServiceProvider`, your `providers` array in the `config/app.php` file should look something like this:

```
'providers' => [
    /*
     * Laravel Framework Service Providers...
     */
    Illuminate\Auth\AuthServiceProvider::class,
    Illuminate\Broadcasting\BroadcastServiceProvider::class,
    Illuminate\Routing\RouteServiceProvider::class,
    // ... other Laravel service providers

    /*
     * Application Service Providers...
     */
    App\Providers\AppServiceProvider::class,
    App\Providers\AuthServiceProvider::class,
    App\Providers\EventServiceProvider::class,
    App\Providers\RouteServiceProvider::class,

    // Add the StructureServiceProvider
    \RatebSa\Structure\StructureServiceProvider::class,
],
```

Key Concepts
------------

[](#key-concepts)

### 1. Repository Pattern:

[](#1-repository-pattern)

The Repository layer provides an abstraction for handling data persistence and retrieval. Each Model in the system will have its own Repository to encapsulate the business logic related to that model. This structure promotes separation of concerns, where the controller handles user requests, the Repository manages the data access, and DTO handles data transfer.

### 2. Data Transfer Object (DTO):

[](#2-data-transfer-object-dto)

The DTO is responsible for transporting data between different layers of the application, such as from the Request to the Repository. This ensures that the data is clean, validated, and transformed as needed before it reaches the business logic layer.

### 3. Filters and Search:

[](#3-filters-and-search)

To facilitate flexible data retrieval, the package supports filtering and searching via pre-defined filter classes and searchable fields. This enables users to query and filter models without bloating the controller with query logic.

### 4. SOLID Principles:

[](#4-solid-principles)

The package adheres to SOLID principles, particularly:

- **Single Responsibility Principle (SRP)**: Each class has a well-defined role. Repositories handle data logic, controllers handle requests, and DTOs manage data transfer.
- **Open-Closed Principle (OCP)**: The package is designed to be easily extendable without modifying existing code, enabling new filters and search criteria to be added seamlessly.

Usage
-----

[](#usage)

Project Structure
=================

[](#project-structure)

Routing Configuration
---------------------

[](#routing-configuration)

In your `routes/web.php` (or `routes/api.php` depending on your application structure), add the following routes to handle user creation and retrieval:

Explanation of Routes
---------------------

[](#explanation-of-routes)

- **`create`**: This route is set up to handle POST requests for creating a new user. It calls the `create` method in the `UserController`. This method is responsible for processing the incoming user data and storing it in the database.
- **`index`**: This route handles POST requests to retrieve a filtered and searchable list of users. It calls the `index` method in the `UserController`. This method facilitates the retrieval of user data based on specified filters, ensuring efficient data management and response.

### UserController.php

[](#usercontrollerphp)

The `UserController` handles requests from the user and interacts with the `UserRepo` repository for retrieving or storing user data.

```
use App\Http\Controllers\UserController;

Route::post('create', [UserController::class, 'create']);
Route::post('index', [UserController::class, 'index']);
```

### UserController.php

[](#usercontrollerphp-1)

```
