PHPackages                             wuwx/laravel-plus-view - 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. [Templating &amp; Views](/categories/templating)
4. /
5. wuwx/laravel-plus-view

ActiveLibrary[Templating &amp; Views](/categories/templating)

wuwx/laravel-plus-view
======================

v9.3.0(1mo ago)33431MITPHPPHP ^7.3|^8.0CI failing

Since Jul 5Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/wuwx/laravel-plus-view)[ Packagist](https://packagist.org/packages/wuwx/laravel-plus-view)[ RSS](/packages/wuwx-laravel-plus-view/feed)WikiDiscussions master Synced today

READMEChangelog (10)Dependencies (6)Versions (28)Used By (0)

Laravel Plus View
=================

[](#laravel-plus-view)

[![Latest Stable Version](https://camo.githubusercontent.com/a88a13071480b0759fa759a4b7fa69662ead1cfcaecdd73e000b0ae40ea93d27/68747470733a2f2f706f7365722e707567782e6f72672f777577782f6c61726176656c2d706c75732d766965772f762f737461626c652e706e67)](https://packagist.org/packages/wuwx/laravel-plus-view) [![Total Downloads](https://camo.githubusercontent.com/85edd47d5beab4ad9ff4be39426615ea6b413dafbb0bf6db84c33984bae307f0/68747470733a2f2f706f7365722e707567782e6f72672f777577782f6c61726176656c2d706c75732d766965772f646f776e6c6f6164732e706e67)](https://packagist.org/packages/wuwx/laravel-plus-view)

一个 Laravel 扩展包，让你的应用能够根据请求格式自动选择对应的视图模板，轻松实现多格式响应（HTML、JSON、XML、JS 等）。

功能特性
----

[](#功能特性)

- 🎯 根据 `_format` 查询参数或 `Accept` 请求头自动选择视图模板
- 📦 支持多种格式：HTML、JSON、XML、JavaScript 等
- 🔧 自动设置正确的 `Content-Type` 响应头
- 🚀 零配置，安装即用（Laravel 5.5+ 自动发现）
- 💡 支持 Blade 模板和普通 PHP 模板

工作原理
----

[](#工作原理)

该扩展通过中间件拦截请求，根据格式参数动态注册视图扩展名，使 Laravel 能够自动查找对应格式的视图文件。

例如，当请求 `?_format=json` 时，Laravel 会优先查找 `view.json.blade.php` 或 `view.json.php` 文件。

环境要求
----

[](#环境要求)

- PHP 7.3 或更高版本（Laravel 13 需要 PHP 8.2+）
- Laravel 5.6 或更高版本（含 Laravel 13）

安装
--

[](#安装)

通过 Composer 安装：

```
composer require wuwx/laravel-plus-view
```

### Laravel 5.5+

[](#laravel-55)

扩展包会通过 Package Discovery 自动注册，无需手动配置。

### Laravel 5.4 及以下版本

[](#laravel-54-及以下版本)

需要在 `config/app.php` 中手动注册服务提供者：

```
'providers' => [
    // ...
    Wuwx\LaravelPlusView\LaravelPlusViewServiceProvider::class,
],
```

使用方法
----

[](#使用方法)

### 创建多格式视图

[](#创建多格式视图)

在 `resources/views` 目录下创建不同格式的视图文件：

**resources/views/users/index.html.blade.php**

```

    用户列表

    用户列表

        @foreach($users as $user)
            {{ $user->name }}
        @endforeach

```

**resources/views/users/index.json.blade.php**

```
{!! json_encode($users) !!}
```

**resources/views/users/index.xml.blade.php**

```

    @foreach($users as $user)

        {{ $user->id }}
        {{ $user->name }}

    @endforeach

```

**resources/views/users/index.js.blade.php**

```
var users = {!! json_encode($users) !!};
console.log(users);
```

### 控制器代码

[](#控制器代码)

控制器代码保持不变，只需返回视图：

```
public function index()
{
    $users = User::all();
    return view('users.index', compact('users'));
}
```

### 请求示例

[](#请求示例)

**方式一：使用 `_format` 查询参数**

```
# 返回 HTML 格式（默认）
curl http://localhost:8000/users

# 返回 JSON 格式
curl http://localhost:8000/users?_format=json

# 返回 XML 格式
curl http://localhost:8000/users?_format=xml

# 返回 JavaScript 格式
curl http://localhost:8000/users?_format=js
```

**方式二：使用 `Accept` 请求头**

```
# 返回 JSON 格式
curl http://localhost:8000/users -H "Accept: application/json"

# 返回 XML 格式
curl http://localhost:8000/users -H "Accept: application/xml"

# 返回 JavaScript 格式
curl http://localhost:8000/users -H "Accept: application/javascript"
```

支持的格式
-----

[](#支持的格式)

扩展包支持所有 Laravel 内置的 MIME 类型，包括但不限于：

- `html` - text/html
- `json` - application/json
- `xml` - application/xml
- `js` - application/javascript
- `css` - text/css
- `txt` - text/plain
- `csv` - text/csv
- 等等...

视图文件命名规则
--------

[](#视图文件命名规则)

视图文件命名格式：`{视图名称}.{格式}.{扩展名}`

- Blade 模板：`index.json.blade.php`
- 普通 PHP 模板：`index.json.php`

测试
--

[](#测试)

运行测试套件：

```
composer test
```

或

```
vendor/bin/phpunit
```

许可证
---

[](#许可证)

MIT License. 详见 [LICENSE](LICENSE) 文件。

###  Health Score

53

—

FairBetter than 97% of packages

Maintenance90

Actively maintained with recent releases

Popularity18

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity78

Established project with proven stability

 Bus Factor1

Top contributor holds 98% of commits — single point of failure

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 ~122 days

Recently: every ~244 days

Total

27

Last Release

50d ago

Major Versions

v4.0.0 → v5.0.02021-03-19

v5.0.1 → v6.0.02022-02-09

v6.0.0 → v7.0.02022-11-06

v7.0.0 → v8.0.02023-07-18

v8.0.0 → v9.0.02024-05-29

### Community

Maintainers

![](https://www.gravatar.com/avatar/9c1977bba4586c70a4bdb6faaee4823184904ff3bd3246e79069871465500611?d=identicon)[wuwx](/maintainers/wuwx)

---

Top Contributors

[![wuwx](https://avatars.githubusercontent.com/u/4401?v=4)](https://github.com/wuwx "wuwx (50 commits)")[![lxlkuc](https://avatars.githubusercontent.com/u/9978778?v=4)](https://github.com/lxlkuc "lxlkuc (1 commits)")

---

Tags

laravellaravelview

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/wuwx-laravel-plus-view/health.svg)

```
[![Health](https://phpackages.com/badges/wuwx-laravel-plus-view/health.svg)](https://phpackages.com/packages/wuwx-laravel-plus-view)
```

###  Alternatives

[guilhermegonzaga/presenter

Implementation for Laravel of the presenter design pattern.

47242.1k](/packages/guilhermegonzaga-presenter)[torann/device-view

Provides support for device based view layouts in Laravel.

146.6k](/packages/torann-device-view)[delatbabel/viewpages

Support rendering/view of Laravel pages and templates from a database.

121.4k](/packages/delatbabel-viewpages)

PHPackages © 2026

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