Roots Discourse

Helpers.php vs Controllers/App.php

If I am not mistaken, the point is that App.php is related and meant for views that use app.blade.php layout. So in other words, the functions here should provide data for the layout.

helpers.php on the other hand is supposed to be for generally accessible functions, that are perhaps not related to view data as much, right?

Or is there a better rule/convention to use here?

Sometimes I get mixed up and not sure where to put the functions, since the majority of my views use one layout, except for the frontpage which has a separate one.

For example I have get_organisations(), published_organisations_exist(), get_banner_img_src() etc. defined in App controller.

On the other hand, in helpers.php I have stuff like: ha_get_mail_footer(), ha_registerWhoops(), ha_send_email(), ha_get_register_email_text() etc.

Can someone share their opinion / examples / thoughts so I can feel more organized with what to put where in the future?


This question came up because I wanted to access certain functions defined in App.php from admin.php, and they were not accessible (tried even use function on top…). And sometimes the other way around, wanted functions from, say, helpers.php to be available in a controller -> App.php (use function on top worked here, for instance: use function App\ha_get_acf_fields_key_name_map;). What I am doing wrong? :slight_smile:

The App namespace represents the “top” level of your site (i.e. your “App”). This is distinct from the App controller (which should have the namespace App/Controllers, meaning the fully qualified name of the App controller would be App/Controllers/App), which passes data to every blade.

In general you should not put functions into controllers that you want access to elsewhere: controllers are only for passing data to views, not for collecting/organizing functions/methods.

To directly answer your final question, your function call didn’t work because the namespace in controllers/App.php is not App so what you called will not resolve to your function.

1 Like

Indeed it is as I thought, I believe I was on the right track. Using controller for just view related functions and am now trying to separate logic properly based on given functionality.

Thanks for taking the time to reply.