PHPackages                             mustafarefaey/laravel-hybrid-spa - 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. [API Development](/categories/api)
4. /
5. mustafarefaey/laravel-hybrid-spa

AbandonedLibrary[API Development](/categories/api)

mustafarefaey/laravel-hybrid-spa
================================

Laravel hybrid SPA response helpers.

v1.0.3(5y ago)122MITPHPPHP ^7.3|^7.4CI failing

Since Dec 3Pushed 5y agoCompare

[ Source](https://github.com/mustafarefaey/laravel-hybrid-spa)[ Packagist](https://packagist.org/packages/mustafarefaey/laravel-hybrid-spa)[ Docs](https://github.com/mustafarefaey/laravel-hybrid-spa)[ RSS](/packages/mustafarefaey-laravel-hybrid-spa/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (4)Dependencies (5)Versions (5)Used By (0)

Laravel hybrid SPA response helpers.
====================================

[](#laravel-hybrid-spa-response-helpers)

[![Latest Version on Packagist](https://camo.githubusercontent.com/9b7003f223ba59aef6d398c98b757bf028f3b93f73dfd48e9a9d0151df3a22a3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d7573746166617265666165792f6c61726176656c2d6879627269642d7370612e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mustafarefaey/laravel-hybrid-spa)[![Tests](https://github.com/mustafarefaey/laravel-hybrid-spa/workflows/Tests/badge.svg?branch=master)](https://github.com/mustafarefaey/laravel-hybrid-spa/actions?query=branch%3Amaster+workflow%3ATests)[![Total Downloads](https://camo.githubusercontent.com/058f3f970e4b99ae0c70fe7aeed72f5b5c0596d544f3f51cc0d03e3e01f00cb4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d7573746166617265666165792f6c61726176656c2d6879627269642d7370612e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mustafarefaey/laravel-hybrid-spa)

Laravel HTTP Response classes, to help you build a hybrid SPA!

Contents table
--------------

[](#contents-table)

- [Installation](#installation)
- [ApiResponse](#apiresponse)

    - [Success response](#success-response)
        - [Success response optional parameters](#success-response-optional-parameters)
        - [Success response format](#success-response-format)
    - [Fail response](#fail-response)
        - [Fail response optional parameters](#fail-response-optional-parameters)
        - [Fail response format](#fail-response-format)
- [HybridResponse](#hybridresponse)

    - [HTML response content](#html-response-content)

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require mustafarefaey/laravel-hybrid-spa
```

You can publish the config file with:

```
php artisan vendor:publish --provider="MustafaRefaey\LaravelHybridSpa\LaravelHybridSpaServiceProvider" --tag="config"
```

This is the contents of the published config file:

```
return [
    /**
     * This should be a full HTTP/HTTPS URL to your JS app.
     * Example: 'https://example.com/app.js'
     */
    'js-app-url' => '',

    /**
     * This should be an array of full HTTP/HTTPS URLs to your stylesheets.
     * Example: ['https://example.com/app.css']
     */
    'stylesheets' => [],

    /**
     * This should be the ID of the element that mounts the JS app.
     * Example: 'app'
     */
    'js-app-id' => '',

    /**
     * This must be a full qualified class path, that implements
     * `MustafaRefaey\LaravelHybridSpa\RetrievesSharedState` interface
     */
    'shared-state-handler' => '\\MustafaRefaey\\LaravelHybridSpa\\SharedState',

    /**
     * This is the name of the global JS variable, that will be injected with the shared state
     * Example: '__SHARED_STATE__', will be exposed as `window.__SHARED_STATE__`
     */
    'shared-state-variable' => '__SHARED_STATE__',

    /**
     * This is the name of the global JS variable, that will be injected with the page state
     * Example: '__PAGE_STATE__', will be exposed as `window.__PAGE_STATE__`
     */
    'page-state-variable' => '__PAGE_STATE__',

    /**
     * This is an array of arrays, to describe favicons
     * Must be in this format:
     *  [
     *      ['href' => '', 'sizes' => '', 'type' => ''],
     *      ['href' => '', 'sizes' => '', 'type' => ''],
     *  ]
     *
     */
    'favicons' => [],
];
```

`artesaos/seotools` package is used to set meta tags. Please check their [configuration documentation](https://github.com/artesaos/seotools#4-configuration).

ApiResponse
-----------

[](#apiresponse)

Use this class in your controllers' actions to return a consistent JSON response.

```
use MustafaRefaey\LaravelHybridSpa\ApiResponse;
```

### **Success response**

[](#success-response)

Use `success` method When returning a successful response.

```
return ApiResponse::success();
```

#### **Success response optional parameters**

[](#success-response-optional-parameters)

`data`: an array of any data, this will be json encoded.

`messages`: an array of any messages, this will be json encoded.

`HTTP status code`: by default this will be 200, unless you specify it.

```
return ApiResponse::success(array $data = [], array $messages = [], int $code = 200);
```

#### **Success response format**

[](#success-response-format)

```
{
    "status": true,
    "data": [],
    "success_messages": []
}
```

### **Fail response**

[](#fail-response)

Use `fail` method When returning a failure response.

```
return ApiResponse::fail();
```

#### **Fail response optional parameters**

[](#fail-response-optional-parameters)

`data`: an array of any data, this will be json encoded.

`messages`: an array of any messages, this will be json encoded.

`HTTP status code`: by default this will be 400, unless you specify it.

```
return ApiResponse::fail(array $data = [], array $messages = [], int $code = 400);
```

#### **Fail response format**

[](#fail-response-format)

```
{
    "status": false,
    "data": [],
    "error_messages": []
}
```

HybridResponse
--------------

[](#hybridresponse)

Use this class in your controllers' actions to return an html page that loads the js app, passing it the initial page state.

If the request expects JSON response, it will return the page's state in an `ApiResponse`.

```
use MustafaRefaey\LaravelHybridSpa\HybridResponse;
```

```
return HybridResponse::make(array $pageState = []);
```

### HTML response content

[](#html-response-content)

- Inside **head** tag:
    1. **Usually needed meta tags**: ```

        ```
    2. **Favicons**: you can set them in the config file.
    3. **SEO meta tags**: This package uses `artesaos/seotools` package to set meta tags. Please review their [documentation](https://github.com/artesaos/seotools#usage).
- Inside **body** tag:
    1. **Div element that will mount the JS app**: you can set its ID in the config file.
    2. **5 global JS variables**:
        1. `window.__SHARED_STATE__`: This is where the shared state is injected.
            - You can rename this variable in the config file.
            - To control its value, You can create a class that extends the `MustafaRefaey\LaravelHybridSpa\RetrievesSharedState` interface, then update `shared-state-handler` in the config file, accordingly.
        2. `window.__PAGE_STATE__`: This is where the page state is injected.
            - You can rename this variable in the config file.
        3. `window.__SKIP_LOADING_PAGE_STATE_ONCE__`: This variable is used to help the JS app to skip calling the backe-end for the page state, and using `window.__PAGE_STATE__` directly. Its initial value is `true`, and you should set it to `false` once you used `window.__PAGE_STATE__`.
        4. `window.__SESSION_SUCCESS_MESSAGES__`: This is where the session success messages are injected.
        5. `window.__SESSION_ERROR_MESSAGES__`: This is where the session error messages are injected.
    3. **The JS app script**: you can set its URL in the config file.

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.

Credits
-------

[](#credits)

This package uses the package [artesaos/seotools](https://github.com/artesaos/seotools) to set meta tags.

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community2

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~12 days

Total

4

Last Release

1948d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/e5e79923eee56e244b86aada14d9122b0dc6b2654c87f88f1e4ef716560b4434?d=identicon)[mustafarefaey](/maintainers/mustafarefaey)

---

Tags

architecturehttpresponselaravelresponselaravelmustafarefaeylaravel-hybrid-spa

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

![Health badge](/badges/mustafarefaey-laravel-hybrid-spa/health.svg)

```
[![Health](https://phpackages.com/badges/mustafarefaey-laravel-hybrid-spa/health.svg)](https://phpackages.com/packages/mustafarefaey-laravel-hybrid-spa)
```

###  Alternatives

[flugger/laravel-responder

A Laravel Fractal package for building API responses, giving you the power of Fractal and the elegancy of Laravel.

8901.5M5](/packages/flugger-laravel-responder)[scalar/laravel

Render your OpenAPI-based API reference

6183.9k2](/packages/scalar-laravel)[dragon-code/laravel-json-response

Automatically always return a response in JSON format

1118.6k1](/packages/dragon-code-laravel-json-response)[kennedy-osaze/laravel-api-response

Renders consistent HTTP JSON responses for API-based projects

654.5k](/packages/kennedy-osaze-laravel-api-response)[dugajean/laravel-yaml

YAML parser for Laravel.

144.8k](/packages/dugajean-laravel-yaml)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
