如何在视图laravel4中调用资源控制器索引


how to call resource controller index in a view laravel 4

我正在尝试在产品编辑页面中列出产品变体的网格视图。我有一个单独的控制器和变量视图。

现在我需要知道如何在产品编辑页面中调用variant Controller索引方法,该方法将返回一个带有分页、搜索、筛选等的视图。

这是一件很难简单做到的事情,因为控制器是HTTP请求处理程序。因此,除非您正在发出另一个请求,否则不应该在视图中调用控制器方法,而且很难做到这一点,因为它们不应该以这种方式使用。

控制器应该接收请求,调用数据处理器(存储库、类),获取结果数据并将其发送到视图,获取视图结果并将其发回浏览器。控制者知道的很少,其他什么也不做。

视图应该接收数据并绘制它。大量数据没有问题,但它应该接收数据(对象很好)并绘制它们。

如果你需要用分页、搜索、过滤等方式绘制视图,你不需要控制器调用,你可以将其添加为子视图:

@include('products.partials.table')

并且您可以在任何视图中重用该视图部分。如果这些表只在某些时候显示,您可以添加条件:

@if ($showTable)
   @include('products.partials.table')
@endif

如果该部分需要数据,则在控制器中生成该数据:

<?php
class ProductsController extends BaseController {
    public function index()
    {
        $allProducts = $this->productRepository->all();
        $filteredProducts = $this->productRepository->filter(Input::all());
        $categories = $this->categoriesRepository->all();
        return View::make('products.index')
                ->with('products', compact('allProducts', 'filteredProducts', 'categories'))
    }
}

但是,尽管如此,你的控制者对你的业务了解得越少越好,所以你可以这样做:

<?php
class ProductsController extends BaseController {
    public function index()
    {
        $products = $this->dataRepository->getProductsFiltered(Input::only('filter'));
        return View::make('products.index')
                ->with('products', compact('products'))
    }
}

让存储库生成绘制数据所需的必要信息。