1
votes

I created a dynamic route due to my complex routes my routes.php was unamaneagle.

Below I create my controller using $controller = new $controller; then $controller->$action(); The controller initialized extends BaseController which has a layout set with $this->layout. The problem is it seems when I manually create this controller, then I cannot access the layout object on BaseController to get data in my view.

this line

 $this->layout->dataGroup = $data;

gives me

   Attempt to assign property of non-object

routes.php

 Route::get('/{dcontroller}/view/{id}', function($dcontroller, $id) {
    $controller = ucfirst($dcontroller).'Controller';
    $action = 'getView';
    $controller = new $controller;
    return $controller->$action($id);
  });

BaseController.php

class BaseController extends Controller {
  protected $layout = 'layouts.master';

  protected function layoutFnc($path, $data) {
    // ********************* THIS FAILS!! *********************
    $this->layout->dataGroup = $data;
    // *********************
    $this->layout->content = View::make($path, $data);
  }
}

MyController.php

class MyController extends BaseController {

    public function getView($id) {
        $this->layoutFnc('layouts/test', aray('test', 'test2'));
    }
}
1

1 Answers

1
votes

Be sure that your method layoutFnc() is called after setupLayout() in the BaseController. ( setupLayout() is called when the route is executed )

[edit] After re-reading your post, I see that the above does not apply to your code if you still have the setupLayout() method in the BaseController.

Maybe you can debug the setupLayout() method like this:

protected function setupLayout()
{
    if (false === is_null($this->layout))
    {
        $this->layout = View::make($this->layout);
        dd(get_class($this->layout));
    }
}