Welcome Guest, Not a member yet? Register   Sign In
auto load header menu data to each page
#1

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.
Reply
#2

(This post was last modified: 07-20-2020, 06:29 AM by marcogmonteiro.)

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); 
Website: marcomonteiro.net  | Blog: blog.marcomonteiro.net | Twitter: @marcogmonteiro | TILThings: tilthings.com
Reply
#3

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); 
Reply
#4

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.
Reply
#5

(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.
Website: marcomonteiro.net  | Blog: blog.marcomonteiro.net | Twitter: @marcogmonteiro | TILThings: tilthings.com
Reply




Theme © iAndrew 2016 - Forum software by © MyBB