开发者

how to have my functions.php available sitewide

php file to be available sitewide such that without require or include i simply can call functions defined in my functions.php.开发者_开发知识库

i have two main directories one is public and admin both have similar functions defined in functions.php so instead of defining separte path for public and admin i just want to have my site setup in such a way that when user go to the admin folder the functions.php functions are available there as well and in all the subdirectories . similar to wordpress .


you can use the auto_prepend_file directive in your php ini or in your htaccess like that:

php_value auto_prepend_file /path/to/your/file.php

Then your file.php containing your common functions will be automaticly include in all your script


You could gather your functions into a class as a set of static methods and use either standard autoloading or autoloading boosted by the spl.


If you have many entry points for your application then one way to do this without touching the files is to create rewrite rules for you app in your webserver. It will map each php request to one file, passing the path of the original query as parameter. This new file will include all the things needed and the include the file that was passed as a parameter.


I don't think there is any way of calling code from a file without including it, but in order to make the job of including the file a lot easier take a look at this page

http://css-tricks.com/1754-php-include-from-root/

Something like the code included there altered to match your page structure would do the job:

<?php
 $path = $_SERVER['DOCUMENT_ROOT'];
 $path .= "/common/header.php";
 include_once($path);
?>

where path is the same as the path on your server


You can do this

<?php
 $root = $_SERVER['DOCUMENT_ROOT'];
 include_once($root."/config.php");
?>

CONFIG.PHP

<?php
include_once('functions.php');
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜