I want to create a page that will act as a sidebar on all pages. This sidebar shows some widgets which should get records from the database. I don't want to have to copy and paste this sidebar code into every single other view but rather want to require or include it.
I am not sure how to go about this but what I tried was:
Created a folder in the views folder called "widgets" and put an index.php file in it.
Created a Widgets.php file in the controllers folder
Created a Widget.php file in the models folder
Send the data from the model to the controller and sent to the view.
If I go directly to the index.php file in the widgets view, it works fine and the database records are displayed as I want.
However, if I try and include this index file in another view somewhere else, I get an "undefined index" and "invalid argument supplied foreach()".
So, what is the proper way to achieve this?
PS.I am not using a framework and this is my controller:
public function index()
$widgets = $this->widgetModel->getWidgets();
$data = [
'widgets' => $widgets