> ## Documentation Index
> Fetch the complete documentation index at: https://kawax.biz/llms.txt
> Use this file to discover all available pages before exploring further.

# View

> 說明如何建立 Laravel view 並從路由或 controller 回傳，以及傳遞資料的方式。

## 前言

從路由或 controller 以字串形式回傳整份 HTML 文件並不實際。
使用 view 就能將所有 HTML 寫在獨立檔案。

View 將 controller、應用邏輯與呈現邏輯分離，存放於 `resources/views` 目錄。
Laravel 通常以 [Blade 樣板語言](/zh-TW/blade) 撰寫 view。

```blade theme={null}
<!-- resources/views/greeting.blade.php -->
<html>
    <body>
        <h1>Hello, {{ $name }}</h1>
    </body>
</html>
```

此 view 存於 `resources/views/greeting.blade.php`，可用全域 `view` helper 回傳。

```php theme={null}
Route::get('/', function () {
    return view('greeting', ['name' => '太郎']);
});
```

## 建立 View

要建立 view，在 `resources/views` 目錄放上 `.blade.php` 副檔名的檔案，或使用 Artisan 指令。

```shell theme={null}
php artisan make:view greeting
```

`.blade.php` 副檔名告訴框架該檔案含有 [Blade 樣板](/zh-TW/blade)。

## 回傳 View

建立 view 後，可從路由或 controller 用全域 `view` helper 回傳。

```php theme={null}
Route::get('/', function () {
    return view('greeting', ['name' => '太郎']);
});
```

也能用 `View` facade 回傳。

```php theme={null}
use Illuminate\Support\Facades\View;

return View::make('greeting', ['name' => '太郎']);
```

`view` helper 的第 1 個引數對應 `resources/views` 目錄中的 view 檔名。
第 2 個引數為 view 內可用的資料陣列。

### 巢狀 view 目錄

view 可放入 `resources/views` 的子目錄中。
以「點」記法參照巢狀 view。

例如放在 `resources/views/admin/profile.blade.php` 的 view 可如下回傳：

```php theme={null}
return view('admin.profile', $data);
```

<Warning>
  view 的目錄名不可包含 `.` 字元。
</Warning>

### 回傳第一個存在的 view

用 `View` facade 的 `first` 方法，可從 view 陣列中回傳第一個存在的 view。

```php theme={null}
use Illuminate\Support\Facades\View;

return View::first(['custom.admin', 'admin'], $data);
```

### 確認 view 是否存在

要確認 view 是否存在，用 `View` facade 的 `exists` 方法。

```php theme={null}
use Illuminate\Support\Facades\View;

if (View::exists('admin.profile')) {
    // ...
}
```

## 傳遞資料給 View

如前例所示，可將資料陣列傳入 view，於 view 中使用該資料。

```php theme={null}
return view('greetings', ['name' => '花子']);
```

此方式的資料需為鍵值對陣列。
於 view 內可用資料的 key 存取每個值。

也可用 `with` 方法逐個新增資料。
`with` 方法回傳 view 物件實例，可用 method chain。

```php theme={null}
return view('greeting')
    ->with('name', '花子')
    ->with('occupation', '工程師');
```

## 對所有 view 共享資料

若要對所有 view 共享資料，使用 `View` facade 的 `share` 方法。
通常寫在 service provider 的 `boot` 方法中。

```php theme={null}
<?php

namespace App\Providers;

use Illuminate\Support\Facades\View;

class AppServiceProvider extends ServiceProvider
{
    /**
     * 應用服務啟動處理
     */
    public function boot(): void
    {
        View::share('appName', 'MyApp');
    }
}
```

## View 的最佳化

預設 Blade 樣板 view 會在需要時編譯。
在請求時執行 view 編譯處理，效能上可能會有些微影響。

用 `view:cache` Artisan 指令可預先編譯應用所使用的所有 view。
建議作為部署流程的一部分執行。

```shell theme={null}
php artisan view:cache
```

要清除 view 快取，用以下指令：

```shell theme={null}
php artisan view:clear
```

## 後續步驟

<Card title="Blade 樣板" icon="code" href="/zh-TW/blade">
  學習用 Blade 語法建構動態 view 的方式。
</Card>


## Related topics

- [Laravel 套件開發](/zh-TW/advanced/package-development.md)
- [回應（Response）](/zh-TW/responses.md)
- [路由](/zh-TW/routing.md)
- [分頁](/zh-TW/pagination.md)
- [從 Laravel 12 升級到 13 指南](/zh-TW/blog/upgrade-12-to-13.md)
