简而言之,我想知道使用视图作曲家是否比我当前的设置更好。
我有一个 ComposerServiceProvider
,带有以下boot()
代码:
view()->composer(['components.carousel'], function ($view) {
$carousels = Carousel::with('slides')->get();
$view->with(compact('carousels'));
});
我的组件非常简单:
<style type="text/css">
.carousel-item {
height: 100vh;
min-height: 300px;
background: no-repeat center center scroll;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
.carousel-caption {
background-color: rgba(0,0,0,0.1);
top: 40%;
bottom: unset;
}
</style>
@php
$carousel = $carousels->filter(function ($carousel, $key) use ($name) {
return ($carousel->name == $name);
});
@endphp
<header>
<div id="carouselIndicators" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
@foreach ($carousel[0]->slides as $slide)
<li data-target="#carouselIndicators"
data-slide-to="{{ $loop->index }}"
class="{{ $loop->index == 0 ? 'active' : '' }}"></li>
@endforeach
</ol>
<div class="carousel-inner" role="listbox">
@foreach ($carousel[0]->slides as $slide)
<div class="carousel-item {{ $loop->index == 0 ? 'active' : '' }}"
style="background-image: url('{{ Storage::url($slide->image) }}')">
@if ($slide->title || $slide->description || $slide->link)
<div class="carousel-caption d-none d-md-block">
<h3>{{ $slide->title }}</h3>
<p>{{ $slide->description }}</p>
<a class="btn btn-primary btn-sm" href="{{ $slide->link }}">Learn More</a>
</div>
@endif
</div>
@endforeach
</div>
<a class="carousel-control-prev" href="#carouselIndicators" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselIndicators" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</header>
使用组件:
@component('components.carousel', [
'name' => 'Super Awesome Carousel'
])
@endcomponent
这是困扰我的东西:
@php
$carousel = $carousels->filter(function ($carousel, $key) use ($name) {
return ($carousel->name == $name);
});
@endphp
我正在返回带有幻灯片关系的每个旋转木马,然后过滤旋转木马,只使用我需要的一个carousel->slides
关系。有没有办法让视图作曲家知道我需要的旋转木马的名称?还是有更好的方法来接近这一点?
谢谢,
您想将此php代码排除在您的视图中。最好做到这一点。有2个选项。您可以将其添加到型号中,以picture.php为例。采用您最常使用的型号。
public static function Carousel($carousels, $name) {
return $carousels->filter(function ($carousel, $key) use ($name) {
return ($carousel->name == $name);
});
}
在视图呼叫@php $carousel = Picture::Carousel($carousels, 'Super Awesome Carousel') @endphp
中,您可以删除components.carousel组件。
另一种做法是在appFunctionsfunctions.php
<?php
if ( ! function_exists("Carousel")) {
function Carousel($carousels, $name) {
return $carousels->filter(function ($carousel, $key) use ($name) {
return ($carousel->name == $name);
}
}
}
在composer.json
中添加此行:
"files": [
"app/Functions/functions.php"
],
运行composer dump-autoload
,并且功能可在您视图中的任何地方使用@php $carousel = Carousel($carousels, 'Super Awesome Carousel') @endphp
当您经常使用它时,我会选择functions.php选项。您应该在控制器中创建$ CAROUSEL对象并将其传递。您可以摆脱应有的观点。