spatie/laravel-view-components

此包已被放弃且不再维护。未建议替代包。

连接 Laravel 中视图渲染和数据的一种更好的方式

1.3.1 2020-03-18 20:54 UTC

This package is auto-updated.

Last update: 2020-03-24 08:46:33 UTC


README

本包已被放弃

连接 Laravel 中视图渲染和数据的一种更好的方式

Latest Version on Packagist GitHub Workflow Status StyleCI Quality Score Total Downloads

视图组件是一种帮助组织与视图相关逻辑的方法,类似于 视图组合器

namespace App\Http\ViewComponents;

use Illuminate\Http\Request;
use Illuminate\Contracts\Support\Htmlable;

class NavigationComponent implements Htmlable
{
    /** \Illuminate\Http\Request */
    private $request;

    /** @var string */
    private $backgroundColor;

    public function __construct(Request $request, string $backgroundColor)
    {
        $this->request = $request;
        $this->backgroundColor = $backgroundColor;
    }

    public function toHtml(): string
    {
        return view('components.navigation', [
            'activeUrl' => $this->request->url(),
            'backgroundColor' => $this->backgroundColor,
        ]);
    }
}
@render('navigationComponent', ['backgroundColor' => 'black'])

视图组件可以是任何实现 Laravel 的 Htmlable 接口的实体,因此您不必一定使用 Blade 视图来渲染组件。这对于包装第三方 HTML 包很有用,如 spatie/laravel-menu

namespace App\Http\ViewComponents;

use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Contracts\Auth\Guard;
use Spatie\Menu\Laravel\Menu;

class MainMenuComponent implements Htmlable
{
    /** @var \Illuminate\Contracts\Auth\Guard */
    private $guard;

    /** @var string */
    private $class;

    public function __construct(Guard $guard, string $class = null)
    {
        $this->guard = $guard;
        $this->class = $class;
    }

    public function toHtml(): string
    {
        $menu = Menu::new()
            ->addClass($this->class)
            ->url('/', 'Home')
            ->url('/projects', 'Projects');

        if ($this->guard->check()) {
            $menu->url('/admin', 'Adminland');
        }

        return $menu;
    }
}
@render('mainMenuComponent', ['class' => 'background-green'])

与视图组合器相比的优势在于,数据渲染逻辑在组件中明确地结合在一起,而不是之后连接。它们还允许您无缝地组合属性和依赖注入。

此包基于 介绍 Laravel 中的视图组件,视图组合器的替代品,由 Jeff Ochoa 撰写。

支持我们

我们投入了大量资源来创建 一流的开放源代码包。您可以通过 购买我们的付费产品之一 来支持我们。

我们非常感谢您从家乡寄来明信片,注明您正在使用我们的哪个包。您可以在 我们的联系页面 上找到我们的地址。我们将所有收到的明信片发布在我们的 虚拟明信片墙上

安装

您可以通过 composer 安装此包

composer require spatie/laravel-view-components

无需额外设置。Laravel 将自动发现并注册服务提供者。

可选地,您可以使用以下命令发布配置文件:

php artisan vendor:publish --provider="Spatie\ViewComponents\ViewComponentsServiceProvider" --tag="config"

这是将发布到 config/view-components 的文件的默认内容

return [
    /*
     * The root namespace where components reside. Components can be referenced
     * with camelCase & dot notation.
     *
     * Example: 'root_namespace' => App\Http\ViewComponents::class
     *
     * `@render('myComponent')
     *     => `App\Http\ViewComponents\MyComponent`
     */
    'root_namespace' => 'App\Http\ViewComponents',

    /*
     * Register alternative namespaces here, similar to custom view paths.
     *
     * Example: 'navigation' => App\Services\Navigation::class,
     *
     * `@render('navigation::mainMenu')`
     *     => `App\Services\Navigation\MainMenu`
     */
    'namespaces' => [
        // 'navigation' => App\Services\Navigation::class,
    ],
];

用法

《@render》指令

@render Blade 指令接受两个参数:第一个是视图组件的路径或类名,第二个是额外的属性集(可选)。

您可以选择通过路径或类名引用组件。

@render('myComponent')
@render(App\Http\ViewComponents\MyComponent::class)

参数将注入到视图组件的 __construct 方法中。组件使用 Laravel 容器实例化,因此未由 render 提供的参数将自动注入。

use Illuminate\Http\Request;

class MyComponent implements Htmlable
{
    public function __construct(Request $request, string $color)
    {
        $this->request = $request;
        $this->color = $color;
    }

    // ...
}
@render('myComponent', ['color' => 'red'])

在上面的示例中,显式设置了 $color,并且 Laravel 将注入一个 $request 对象。

配置

根命名空间

通过配置root_namespace,您可以定义大部分视图组件的位置。默认情况下,它位于App\Http\ViewComponents

app/
  Http/
    ViewComponents/
      MyComponent.php
      Nested/
        NestedComponent.php

上述组件可以使用@render('myComponent')@render('nested.nestedComponent')进行渲染。

附加命名空间

您可以在namespaces配置中注册附加命名空间,类似于视图路径。

return [
    'namespaces' => [
        'navigation' => App\Services\Navigation::class,
    ],
];
app/
  Services/
    Navigation/
      Menu.php

现在可以使用@render('navigation::menu')渲染上述Menu组件。

测试

composer test

变更日志

请参阅CHANGELOG以获取有关最近更改的更多信息。

贡献

请参阅CONTRIBUTING以获取详细信息。

安全

如果您发现任何与安全相关的问题,请通过电子邮件freek@spatie.be联系,而不是使用问题跟踪器。

鸣谢

许可

MIT许可(MIT)。请参阅许可文件以获取更多信息。