开发者

PHP File Location Different for Ajax Calls Vs. Inital Load

开发者 https://www.devze.com 2023-01-16 12:12 出处:网络
I have a file I use called fileCabinet.php to load in all my classes etc. It works great when my site is loaded from index.php via:

I have a file I use called fileCabinet.php to load in all my classes etc. It works great when my site is loaded from index.php via:

require_once('../includes/fileCabinet.php');

My issue is when I make Ajax calls, I want to use the same classes and would love to use the same file to load them, but the location of the ajax handling script is different from the location of the index.php so I end up with something that looks like this:

if($admin){
    $path = '../';
}else if($ajax){
    $path = '../../';
}

@require_once($path . 'admin/includes/generalFunctions.php');
@require_once($path . 'admin/corModules/PageAdmin.php');
@require_once($path . '开发者_运维问答admin/corModules/SEOManager.php');

I have two different paths depending on where I'm calling from (admin or ajax). I'm wondering if there is a die-hard approach to locating your files no matter where the call for it comes from.

Thoughts?


Inside fileCabinate.php include relative to that file only:

require_once(__DIR__.'admin/includes/generalFunctions.php');
require_once(__DIR__.'admin/corModules/PageAdmin.php');
require_once(__DIR__.'admin/corModules/SEOManager.php');

The __DIR__ magic constant is the directory of the current file, so in this case it's assumed the three sub files are in the admin sub-folder.

/ 
/ fileCabinate.php
/ admin / includes /
                   / generalFunctions.php
/ admin / corModules /
                     / PageAdmin.php
                     / SEOManager.php

Per op's point - prior to 5.3, you needed to use:

require_once(dirname(__FILE__).'admin/includes/generalFunctions.php');

... oh how quickly we forget how it worked in the good ol' days!


Are the files still all located in the same place? Or do you have 2 sets of these files around?

If they are all in just 1 location, use some constants to setup these locations.

define('ROOT_PATH', '/path/to/root/folder');

In addition you should setup a few constants that might be useful: web root, root path, image directory, whatever you are going to use over and over. Also, you could use the $_SERVER variable to setup these constants so that you don't need to manually define these every time you do a new project and put this stuff in a config file.

Then you can do

require_once(ROOT_PATH . '/admin/blah/file.php');

To continue down this path you should use require instead of require_once (better performance) and you should look into the SPL libraries and using autoloading. Just a good path to go down to simplify things and improve performance.


Place all files that you need to include on a regular basis in a single directory, such as includes. Add that directory to the include path. Ideally, you would do this from an Apache configuration file, such as an .htaccess file.

php_value include_path ".:/foo/bar/includes"

Make sure you write the complete include path (it may contain other directories, such as PEAR). If you cannot do this, change the include path from PHP in every file:

set_include_path(get_include_path().PATH_SEPARATOR.'/foo/bar/includes');

If you did either of those, the files are now in the include path, which means you can write simply require 'foo.php' and it will load includes/foo.php no matter where you are.

0

精彩评论

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