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

查看:64
本文介绍了如何实现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.

做到这一点的最佳方法是什么?

What is the best way of doing this?

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

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.php filters.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部分视图-将数据绑定到部分视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆