In your second example, you’d be able to access video in your Blade with the following:
Methods in the App namespace are available to every Blade, unless they’re overwritten by another controller–i.e. if you had a controllers/FrontPage.php controller file that also defined homepageFields(), the $homepage_fields variable in views/front-page.blade.php would contain the value returned by the controllers/FrontPage.php version of homepageFields() instead of the one defined in controllers/App.php.
Personally, I don’t like to group unrelated data together in the variables I pass from my controllers to my Blades, because then my templates become more opinionated about how they handle data. That being the case, I’d separate out subheading and video instead of returning them as an array, but that’s a personal preference.
No problem. All pretty new to me too but once you’re past the initial learning curve, it all clicks very nicely. Having super clean views/templates is particularly satisfying. Maintainability is also greatly improved thanks to the separation of logic and templating. I find that once all the logic is taken care of, moving stuff around/amending your templates is really easy.
It is awesome, that’s what I love about React.js, it’s funny, where I work, we run an MVC environment but it’s such a mess, with php logic and everything in-between mashed into the templates haha so it’s good to understand the proper way of doing it
Using object syntax like this is very clever and I like it a lot! I realize it’s mostly aesthetic preference on my part, but for some reason $container->field always looks so much nicer to me than $container['field'].
I’m just getting started here, but this has really given me new hope for Wordpress. I always got frustrated that my template files would get so messy when you started adding logic for ACF fields and so on.
I have created this Related Posts snippet. It utilizes an ACF Bidirectional Post Relationship plugin. The code is as follows:
Totally untested and assumes that your returning your related post as the post ID not an object (by looking at what you posted up). It involves two loops (one in the controller and one in the view) but it keeps the view very tidy in my opinion.
It would be fairly easy to build in an acf helper class into Controller that speeds up the process of looping through fields and then outputting to an object notation. All you would need to use in the view then would be @foreach (for repeater fields). Basic fields could be flattened using the key as the variable name.
If you’re using acf in a standard way, it could all be automated to be honest. I’m going to look into building a prototype module over the next few days should time allow.
Been working on something like this. All values returned to the view are returned in object notation should that be required.
In a Controller Class;
class Single extends Controller
// all returned values from Acf, even if multidimensional arrays are in object notation
public function acf()
// gets all fields
// use $acf->repeater in view
// use $acf->text_field in view
public function text()
// gets 1 field and returns flattened
// use $text in view
public function combo()
return Acf::get(['repeater_field', 'text_field']);
// gets 2 fields, and returns in object notation using advanced custom fields key values
// use $combo->text_field in view
// use $combo->repeater_field in view