过滤 laravel 中的数据并处理视图


Filtering data in laravel and handling the views

我有一个电视节目 netflix 式的项目,我正在构建一个Shows页面,我想过滤它的格式。每个节目都包含可以具有电视,DVD和bd格式的剧集。

目前,我正在使用扩展基本ShowsController的单独路由和控制器进行过滤。

Route::get('shows/view/{type}', ['as' => 'shows.viewtype', 'uses' => 'ShowsController@viewType',]);
Route::get('shows/bluray',['as' => 'shows.bluray','uses' => 'ShowsBlurayController@index']);
Route::get('shows/dvd',['as' => 'shows.dvd','uses' => 'ShowsDVDController@index']);
Route::get('shows/tv',['as' => 'shows.tv','uses' => 'ShowsTVController@index']);

格式控制器之一的示例

class ShowsBlurayController extends ShowsController
{
    public function index()
    {
        // Set user state for browsing bluray
        Session::push('user.showtype', 'bluray');
        $shows = $this->show->getBlurayPaginated(16);
        return $this->getIndexView(compact('shows'));
    }
}

我使用 getIndexView() 方法(在 ShowsController 中)来确定 2 个可用视图之一:posterlist

public function getIndexView($shows)
{
    $viewType = get_session_or_cookie('show_viewtype', 'list');
    if ($viewType == 'posters') {
        return View::make('shows.index', $shows)
            ->nest('showsView', 'shows.partials.posters', $shows);
    } else {
        return View::make('shows.index', $shows)
            ->nest('showsView', 'shows.partials.list', $shows);
    }
}

节目根据剧集进行过滤:

public function getBlurayPaginated($perPage)
{
    return $this->getByFormat('BD')->with('tagged')->paginate($perPage);
}
private function getByFormat($format)
{
    return $this->show->whereHas('episodes', function ($q) use ($format) {
        $q->whereHas('format', function ($q) use ($format) {
            $q->where('format', '=', $format);
        });
    });
}

问题是我想以一种干净的方式做到这一点。当用户选择格式时,将应用该筛选器。目前,所有这些都分散在控制器中,不太有意义。

我也想在routes.php做这样的事情:

Route::get('shows/format/{format}',['as' => 'shows.format','uses' => 'ShowsController@index']);

然后处理索引中的过滤,但这似乎也是一个奇怪的地方。

这种方法确实有效,但我不想以后用它搞砸自己。我正在计划一个简单的搜索,应该考虑到过滤器。

换句话说,我如何组织代码,以便从数据库中获取数据将考虑已设置的过滤器?(也许是会话状态?

Route::get('shows/format/{format}',[
    'as' => 'shows.format',
    'uses' => 'ShowsController@index'
]);

我认为你在这里走在正确的轨道上。 我甚至会生产一个工厂并将其注入控制器。 此工厂的目的是构造一个格式化程序,该格式化程序将为您的视图提供正确的数据:

// ShowController
public function __construct(ShowFormatFactory $factory, ShowRepository $shows)
{
    $this->factory = $factory;
    // NB: using a repository here just for illustrative purposes.
    $this->shows = $shows;
}
public function index($format = null) 
{
    $formatter = $this->factory->make($format);
    return View::make('shows.index', [
        'formatter' => $formatter,
        'shows' => $this->shows->all(),
    ]);
}
// ShowFormatFactory
class ShowFormatFactory
{
    public function make($format)
    {
        switch($format) {
            case 'blueray':
                return new BluerayFormat(); break;
            case 'dvd': /* Fallthrough for default option */
            default:
                return new BluerayFormat(); break;
        }
    }
}
// ShowFormatInterface
interface ShowFormatInterface
{
    public function format(Show $show);
}
// BluerayFormat
class BluerayFormat implements ShowFormatInterface
{
    public function format(Show $show)
    {
        return $show->blueray_format;
    }
}

那么在您看来,由于您保证有一个对象将为您提供给定节目所需的格式,只需调用它:

@foreach($shows as $show)
<div class="show">
    Chosen Format: {{ $formatter->format($show) }}
</div>
@endforeach

此解决方案是可测试的,可扩展将允许您稍后添加其他格式。 如果这样做,则需要在工厂中为每种不同的格式添加一个离散的case语句,并编写一个相当纤细的 ~5-7 行类来支持新格式。