CodeIgniter Forums
auto load header menu data to each page - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: auto load header menu data to each page (/thread-77110.html)



auto load header menu data to each page - cilux - 07-20-2020

Hello,
I have header menu json data(from MenuModel.php):

PHP Code:
[{"text":"Opcion3","href":"","icon":"fas fa-bell","target":"_self","title":""},{"text":"Home","href":"http://home.com","icon":"fas fa-home","target":"_top","title":"My Home"},{"text":"Opcion4","href":"","icon":"fas fa-crop","target":"_self","title":""},{"text":"Opcion6","href":"","icon":"fas fa-map-marker","target":"_self","title":""},{"text":"Opcion7","href":"","icon":"fas fa-search","target":"_self","title":""}] 

Now, I need to auto load this data to all template(header) for show menu on top of each page using jquery.

what is best solution for auto load this data to all page?!

thanks for any help.


RE: auto load header menu data to each page - marcogmonteiro - 07-20-2020

You want to autoload this model inside your BaseController.php Then you get the data into a class property so you can access it in all your controllers that extend to BaseController.php.

Something like:


PHP Code:
$model = new ArticleModel();
$this->data['header'] = $model->yourFunction(); 


Then in your controllers you just send the data into your views.

PHP Code:
return view('foo'$this->data); 



RE: auto load header menu data to each page - cilux - 07-20-2020

thanks, your mean is:

In BaseController.php:

PHP Code:
$model = new ArticleModel();
$this->data['header'] = $model->yourFunction(); 


and in Home.php or another controller:

PHP Code:
return view('foo'$this->data); 



RE: auto load header menu data to each page - kilishan - 07-20-2020

That's a great way to handle it. Additionally, you might look into View Cells. They're probably something that should be used with care, but combined with caching (that's built into them) it could work well for this instance, also.


RE: auto load header menu data to each page - marcogmonteiro - 07-20-2020

(07-20-2020, 06:50 AM)kilishan Wrote: That's a great way to handle it. Additionally, you might look into View Cells. They're probably something that should be used with care, but combined with caching (that's built into them) it could work well for this instance, also.

Yea that looks exactly like something view cells should be used for.