dunice/laravel-view-components

在Laravel中将数据与视图渲染连接的更好方式

1.3.2 2020-11-30 23:56 UTC

This package is auto-updated.

Last update: 2024-09-29 05:56:09 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

变更日志

有关最近更改的更多信息,请参阅变更日志

贡献

有关详细信息,请参阅贡献

安全

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

鸣谢

许可协议

MIT 许可协议(MIT)。有关更多信息,请参阅 许可文件