开发者

php - multiple include from multiple directory error

Here is my directory listing :

>htdocs
-->MyWeb
---->admin
------>cms.php
---->images
---->controller
------>post.php
------>general.php
---->model
------>posts.php
------>sessions.php
------>connections.php
---->index.php

and here are the includes for each page starting from top :

cms.php:
    require_once('../model/sessions.php');
    require_once('../controller/post.php');
post.php:
    require_once('general.php');
    require_once('../model/posts.php');
general.php : none 
p开发者_开发知识库osts.php:
    require_once('sessions.php');
    require_once('connections.php');
sessions.php:
    require_once('connections.php');
connections.php : none
index.php:
    require_once('controller/post.php');

condition : tested everything fine with cms.php, i got access to all the function using forms provided by the controller, and the controller got the data from model. now, the administration things are done, time to get to the index.php to show the data, i put inside index.php, when i open it, here's what happened :

I got errors which tell me that the "require_once()" function cannot locate the specified file. I did this with cms.php and its all right. The errors are not from index.php, but from the file included within it (post.php).

lets take a look again in index.php, it has included the controller, post.php using "require_once('controller/post.php') ;", but the post controller itself, includes another file, which is the post model "require_once('../model/posts.php')". The problem occurs when index.php view the required file of the controller using top folder perspective (MyWeb), then reads the file included in post controller as exactly "require_once('../model/posts.php')" = up one folder of index.php, folder model, which mean :

htdocs>model>posts.php which is of course, didn't exist. I was really confused at the first time, then i create some cheats for this :

1. I put a variable $check = true ; before the require_once('controller/post.php') ; in index.php
2. then for each page that requires another file, i put these :
    //for example, in sessions.php
    if(isset($check)) {
        //old link which is like this : require_once('connections.php') ; has become :
        require_once('model/connections.php') ;
        //so in index.php perspective, the file can be reached
    }
    else {
        //old links, only executed when this page was not viewed from index.php
        require_once('connections.php') ;
    }
3. it works now, cms.php read the includes from old links, while index.php will read the included from new links.

Now, even though it works, my codes look ugly and inconsistent if in the future, i might add more files, more includes. question : do anyone has better solution for this ? how to access working directories of the application, such in linux using : ~/controller/post.php or ~/model/posts.php. so the address will be consistent. can't use getcwd(), i dont want to show the absolute path of my files. does anyone can give me better solution for specifying directories such in my case ?

note : pardon my english


how to access working directories of the application, such in linux using : ~/controller/post.php or ~/model/posts.php.

$_SERVER['DOCUMENT_ROOT']


DOCUMENT_ROOT works, but is useless if you need to be able to move your application around in subdirectories like

domain.com/apps/myapp
domain.com/apps/myapp/v1

in that case, my favourite approach is to define the root directory in a configuration file that gets called in every script:

// in /index.php
define("BASEDIR", dirname(__FILE__));

you can then use the BASEDIR constant wherever you need to substitute the full path to the application.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜