1

I have a view that is rendered with its controller. The function that calls the view is linked in my routes. It works fine when directly accessing the route, but obviously my controller is not included when I include it in my template.

How do I use my controller when I include my view?

I'm on Laravel 3.

Right now I have my controller :

public function get_current()
{
// $sales = ...
return View::make('sale.current')->with('sales',$sales);
}

My route (which obv only work on GET /current) :

Route::get('current', 'sale@current');

My master view

@include('sale.current')

Then my sale.current view calls $sales

@foreach($sales as $sale)

Thanks!

veksen
  • 6,863
  • 5
  • 20
  • 33

2 Answers2

1

So this is the case when you want to call some laravel controller action from view to render another partial view. Although you can find one or another hack around it. However, please note that laravel controllers are not meant for that.

When you encounter this scenario when you want to reuse the same view again but don't want to supply all necessary data again & again in multiple controller actions, it's the time you should explore the Laravel View Composers.

Here is the official documentation link : https://laravel.com/docs/master/views#view-composers

Here is the more detailed version of it : https://scotch.io/tutorials/sharing-data-between-views-using-laravel-view-composers

This is the standard way of achieving it without any patch work.

Dhrumil Bhankhar
  • 1,301
  • 1
  • 14
  • 15
0

Your question is still unclear but I can try to help you. I did a small example with the requirements you gave. I create a route to an action controller as follows:

Route::get('test', 'TestController@test');

In TestController I define the action test as follows:

public function test()
{
  return View::make('test.home')->with('data', array('hello', 'world', '!'));
}

According to your asking, you defined a view who includes content from another view (layout) and in that layout you use the data passed for the action controller. I create the views as follows:

// home.blade.php
<h1>Message</h1>
@include('test.test')

and

// test.blade.php
<?php print_r($data); ?>

When I access to "test" I can see print_r output. I don't know if that is what you are doing, but in my case works fine.

I hope that can help you.

Darwing
  • 833
  • 1
  • 12
  • 23
  • Thanks for the reply, I am however trying to do the opposite. Think of your `test` view as a module that could be anywhere. The 'test' view needs the possibility to be included on any page, but it doesn't mean it will get included. So what would be the best way to achieve that? Passing my data on my home for every request can't be good, and I already have a specific controller for my `test` view. – veksen Aug 24 '13 at 02:08
  • Maybe defining a `section` that contains the common data can help you. I think nested views are related to what you want achieve. [Here](http://three.laravel.com/docs/views) you can see all possible uses of views and responses in Laravel 3. – Darwing Aug 24 '13 at 05:17