I share some variables from by BaseController to the header of a master layout for all views. This worked for every view via any controller I've made until now.
This is the error I get:
Undefined variable: basket_lines (View: /var/www/vhosts/lsigifts.co.uk/app/views/layouts/master.blade.php) (View: /var/www/vhosts/lsigifts.co.uk/app/views/layouts/master.blade.php)
The master.blade.php view doesn't give this error on any other page.
My BaseController:
class BaseController extends Controller {
public function __construct()
{
// basket stuff to share on all templates
// how many different products are in there?
$basket_lines = Cart::totalItems(true);
View::share('basket_lines', $basket_lines);
// what's the total ex vat?
$basket_total_exvat = number_format(Cart::total(false), 2);
View::share('basket_total_exvat', $basket_total_exvat);
// what's the total inc vat?
$basket_total_vat = number_format(Cart::total(), 2);
View::share('basket_total_vat', $basket_total_vat);
}
/**
* Setup the layout used by the controller.
*
* @return void
*/
protected function setupLayout()
{
if ( ! is_null($this->layout))
{
$this->layout = View::make($this->layout);
}
}
}
As I say, this feeds stuff to all views without a problem. However, this controller won't accept it:
Route:
Route::controller('search', 'SearchController');
SearchController:
class SearchController extends \BaseController {
/**
* @var The SOLR client.
*/
protected $client;
/**
* Constructor
**/
public function __construct()
{
// get config
$solr_config = Config::get('solr');
// create a client instance
$this->client = new \Solarium\Client($solr_config);
}
public function getIndex()
{
/*$ping = $this->client->createPing();
try {
$result = $this->client->ping($ping);
return var_dump($result);
} catch (Solarium\Exception $e) {
return 'hmmm';
}*/
if(Input::has('q')){
// create the query
$query = $this->client->createSelect();
// set the string
$query->setQuery('%P1%', array(Input::get('q')));
// execute the query
$resultset = $this->client->select($query);
// create the view
return View::make('search.index', [
'q' => Input::get('q'),
'resultset' => $resultset
]);
}
return 'No search term';
}
}
I have looked everywhere for a solution for hours, tried creating the views in different ways, tried putting test values in for these shared variables but to no avail.
The only difference I can see between this view and others is that it's a get view, rather than a post one but I've not found any reason as to why this would change anything. as the Laravel docs say, "You may also share a piece of data across all views".
How can I share these variables to this view?