0
votes

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?

1
What happens if the basket is empty/never set? Could that cause the issue? - Laurence
When it's empty, the view still works on any other page, it just tells you that it's empty! - Scott Brown
If you define the variable manually in the controller - and attach it to the view manually - does it work then? - Laurence
Just tried that as it goes and, yeah, it loads fine from the controller itself. I could just leave it in there I suppose but I'd love to understand why it's not working as expected - Scott Brown

1 Answers

1
votes

Add parent::__construct(); to your SearchController's __construct method. And all should work. Like;

public function __construct()
{

    parent::__construct();

    //Rest of your code
}