简体   繁体   中英

how to call resource controller index in a view laravel 4

I am trying to list the grid view of the product variants in product edit page. I have a separate controller and view for variants.

Now I need to know How can I call the variant Controller index method in products edit page, which will return a view with pagination ,search , filter etc.

This is a hard thing to do simple because controllers are HTTP request handlers. So, unless you are making another request, you should not call aa controller method inside your view and it will be hard to do it because they weren't meant to be used this way.

A controller should receive a request, call data processors (repositories, classes), get the result data and send them to the view, get the view result and send it back to the browser. A controller knows very little and does nothing else.

A view should receive data and plot it. There is no problem in being lots and lots of data, but it should receive data (objects are good) and plot them.

If you need to plot a view with pagination pagination, search, filter etc., you don't need a controller call to do it, you can add it as a subview:

@include('products.partials.table')

And you can reuse that view partial in any views. If those tables must be shown only sometimes, you can add conditions to it:

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

If that partial requires data, you produce that data in your controller:

<?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'))
    }

}

But, still, the less your controller knows about your business, the better, so you could just do:

<?php

class ProductsController extends BaseController {

    public function index()
    {
        $products = $this->dataRepository->getProductsFiltered(Input::only('filter'));

        return View::make('products.index')
                ->with('products', compact('products'))
    }

}

And let the repository produce the necessary information you need to plot your data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM