问题
I have been trying to include a file in my template which includes some functions I was intending to use for validation of the access level a member has in order to tailor content for different types of users in Joomla 2.5. The trouble is even though I have used the standard PHP include statement, none of the functions appear to be usable in the template. Instead calling the functions causes any pages using the template to crash. I could hard code the functions at the top of the template which is working, but I also have plans to use some the functions elsewhere in my web application, so it makes sense to store them in an include file. Does anyone have some insight into why the functions do not work from an include, but do when added to the top of the template? The following is the top few lines of my template with the include statement:
<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
JHtml::_('behavior.framework', true);
include ("/includes/checkAccess.php");
Please note the functions all work fine when hard-coded into the template, so it is definitely a problem with the include. Also, the include path above appears to be correct because if the include line above is added, the template still works fine unless a call is made to one of the functions it contains.
回答1:
I have figured it out. The include path I had above was not correct even though it wasn't throwing an error (unless I called a function from the include). The following is correct and succeeds in pulling the functions through into the template:
include ("./includes/checkAccess.php");
回答2:
This certainly works if the folder "includes" is in the same directory
include(dirname(__FILE__)."/includes/checkAccess.php");
回答3:
You could include the functions inside a Joomla file which is already active on every page instead of the templates which usually read certain things and exclude others.
Is there a functions file Joomla already uses? You could include it at the bottom of that. Also make sure none of your variables or globals can conflict with Joomla, make sure they're all very much unique.
回答4:
For access checks, I would use the build-in ACL (Access Control List).
Read more about ACL.
来源:https://stackoverflow.com/questions/16206708/how-do-i-include-a-file-of-php-functions-in-a-joomla-template