沃梦达 / 编程问答 / php问题 / 正文

如何实现 Laravel 4 部分视图 - 将数据绑定到部分视图

How to implement Laravel 4 Partial Views - Binding data to partial views(如何实现 Laravel 4 部分视图 - 将数据绑定到部分视图)

本文介绍了如何实现 Laravel 4 部分视图 - 将数据绑定到部分视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑是否应该使用 Laravel 来建立一个在线商店.

I am debating whether I should use Laravel to build an online store.

要求 - 在侧边栏中显示购物车,并在主要区域显示产品列表.我需要将数据绑定到我的局部视图.

Requirement - Show a shopping cart in sidebar with product listing in main area. i need to bind data to my partial views.

我创建了一个 PartialController 来显示部分视图.

I have created a PartialController to display partial views.

class PartialController extends BaseController {

    public function showCartSummary()
    {
        $cartItems = Cart::all();   
        return View::make('partials.cartsummary', array(
            'cart' => $cartItems,
        ));
    }

    public function showProducts()
    {
        $products = Products::all();
        return View::make('partials.products', array(
            'products' => $products,
        ));
    }
}

我创建了一个商店索引视图来拉入部分视图

I have created a shop index view to pull in the partial views

Shop.Index.Blade.php

Shop.Index.Blade.php

@extends('master')

@section('content')
    @include('partials.cart')
@stop

@section('side1')
    @include('partials.products')
@stop

问题是没有数据传递到这些视图,因为没有从它们自己的控制器调用 partials.cart 和 partials.products.

The problem is that no data is passed on to these views because partials.cart and partials.products are not being called from their own controllers.

我的解决方法是在 ShopController 中查询数据库并将其传递给 shop.index 视图.

My workaround involves querying the database within the ShopController and passing this to the shop.index view.

ShopController.php

ShopController.php

我还创建了一个 ShopController

I have also created a ShopController

    public function showIndex()
    {
        $cartItems = Cart::all();   
        $products = Product::all();

        return View::make('shop.index', array(
            'cartItems' => $cartItems,
            'products' => $products
        ));
    }

当然,我现在重复我的数据库查询,我不想在使用多个视图的每个控制器方法中重复相同的查询.

Of course, I am now repeating my db queries and I don't want to have to repeat the same queries in every controller method where multiple views are used.

最好的方法是什么?

注意:出于这个问题的目的,我已经过度简化了数据库调用.代码中可能存在一两个拼写错误/语法错误,但对于这个问题并不重要.

NB: I have over simplified database calls for the purposes of this question & there may be one or two typos / syntax errors in code, but not important for this question.

迭代 2:

我发现我可以使用 view composers 来创建视图模型/演示者.

I have found that I can use view composers to create viewmodels / presenters.

shop.blade.php

shop.blade.php

@extends('master')
@section('content')
    @include('partials.products')
@stop
@section('side1')
    @include('partials.cartitems')
@stop

现在将数据传递给局部视图:首先我放弃 PartialController.php,然后修改 filters.phpfilters.php

Now to pass the data to the partial views: Firstly I ditch the PartialController.php, then amend filters.php filters.php

App::before(function($request)
{
    View::composer('partials.products', 'ProductComposer');
    View::composer('partials.cartitems', 'CartComposer');
});

class ProductComposer {
    public function compose($view)
    {
        $view->with('products', Product::all()); 
    }
}

class CartComposer {
    public function compose($view)
    {
        $view->with('cartitems', Cart::all());    
    }
}

这仍然很混乱,我不想将我所有的部分视图都塞进 filters.php 文件中……有没有合适的/官方的方法来做这件事?有什么例子吗?

This is still very messy, i don't want to stuff all my partial views into the filters.php file... Is there a proper / official way of doing this? Any examples?

推荐答案

在你的 app/目录下创建一个 composers.php 文件,并通过 app/start/global.php 包含它.在这个文件中,执行 View::composer 调用(您不需要将它们放在 App::before 中).

Make a composers.php file in your app/ directory and include it via app/start/global.php. In this file, do the View::composer calls (you do not need to put them inside App::before).

将 composer 类移动到一个新的 app/composers/目录中,并将该目录添加到 composer.json 中的自动加载器.

Move the composer classes into a new app/composers/ directory and add the directory to the autoloader in your composer.json.

除此之外,您对作曲家的使用是正确的.

Other than that, your use of composers is correct.

这篇关于如何实现 Laravel 4 部分视图 - 将数据绑定到部分视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何实现 Laravel 4 部分视图 - 将数据绑定到部分视图

基础教程推荐