Heraclitus
Heraclitus

Reputation: 80

What's the best way to reference files from a controller?

Currently because of multiple includes & requires I need to use the following code when referencing a file in my views:

require_once('../app/views/pages/home.php');

But I don't think this is a good idea, is there some way to make referencing files cleaner and reducing the risk of maintenance problems later on?

Upvotes: 0

Views: 49

Answers (1)

dbuser
dbuser

Reputation: 11

Assuming this is on a web server, I usually use something based on $_SERVER['DOCUMENT_ROOT'], so you'd have something like:

require_once($_SERVER['DOCUMENT_ROOT'] . '/app/views/pages/home.php');

If it's not on a web server or something that supplies that DOCUMENT_ROOT, if you have a config file that is automatically read by every script in your project, you could define a constant with the absolute path to your code root, and reference that similarly. (If that's the case and you need more specifics, I can provide an example.)

Upvotes: 1

Related Questions