0

I've been working on some projects in which we have configuration files, and other functions that are used on multiple pages to be included through php as an external file. I've been looking into a way to do something similar to that, but instead of having to include it in every single file, have it included on every file in a directory.

Is this possible at all? I'm trying to use it for some very simple things such as queries, or inclusions, or error reporting, so i dont have to have that include line on each page.

(side note: I know its lazy, and no, its not a needed thing... at this point, its more of curiosity than application)

Thanks!

7
  • 1
    Have you done some research or tried something to accomplish your goal? Commented May 5, 2015 at 17:49
  • Welcome to Stack Overflow! This question is a little short on information. Can you share what you have tried, and what problems you have run into? Commented May 5, 2015 at 17:49
  • 4
    "I know its lazy" - Hey, "you" said it, we didn't. Commented May 5, 2015 at 17:51
  • 3
    There's auto_prepend_file. But that's more of a workaround really. Commented May 5, 2015 at 17:51
  • 1
    auto_prepend_file may be the closest thing to the idea :) Commented May 5, 2015 at 18:09

2 Answers 2

1

What I usually do is have a file called includer.php. That file contains something like:

<?php

include_once("pageBuilder.php");
include_once("helperFunctions.php");

?>

Then in individual files I have this at the top:

<?php

include_once("includer.php");
include_once("fileOnlyThisPageNeeds.php");

?>

This can be improved in a number of ways, but that's the general idea.

That way you can include any file in includer.php and it will be included in all the pages you need.

EDIT:

I don't recommend forcing all pages to include a file - If you decide you don't want one page to have the file included later on, you'll have to go back and use some other method.

It's very little work to add that one line. It will save you time later.

Sign up to request clarification or add additional context in comments.

Comments

1

The only way I can think of doing this would be to route everything to one entry point using htaccess, e.g:

RewriteEngine on
RewriteRule ^.+$ /index.php [L]

In your index.php file you can then include the relevant files you mention.

And then, depending on how your application is structured, you can then just take the request URI and parse it as normal.

If you take requests to php files, it's as simple as then including the relevant file. If your application is bootstrapped or has another master application file, then include that, etc.

I'll leave that as an exercise for the reader ;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.