Problems with file include, pointing directory relative to page that was first opened

1

I'm having trouble when my project has a structure with a large directory depth. When I give include in the file the url is relative to the page that was opened. And then some files includes are not recognized.

For example, I open a page with depth 3 of the directory, dou include in a file with depth 2, and within that file the includes that it has not recognize the files since the url is in about the first page I opened.

Not to mention that many files need to do this ../../../arquivo.php to recognize files from other directories.

EXAMPLE STRUCTURE

.
├── controller
│   └── entityController.class.php
│
│
├── model
│   └── entity.class.php
│   
|
└── ws
    ├── dir
    │   └── page1.php
    └── dir2
        └── page2.php

SITUATION

1st page1 is opened in it has an include a entityController.class.php at two levels above to be able to access

include '../../controller/entityController.class.php';

2nd entityController has an include a entity that needs to return to a level above to be able to access

include '../model/entity.class.php';

When the problem occurs, this include does not work because it occurs in relation to the level of the page1.php file that was the first to be called, so it will look for the folder model within the ws directory, that is to make it right it would have to do so

include '../../model/entity.class.php';

But it would solve in this case, and it would be a problem because the controller is called by other files of different levels.

    
asked by anonymous 24.01.2015 / 00:19

1 answer

1
include or require use the absolute path instead of relative path, this is very easy using the "magic" constants. Try this:

pager1

include __DIR__.'/../../controller/entityController.class.php'

entityController

include __DIR__.'/../model/entity.class.php';

Note that the constant __DIR__ does not have the last bar, so it needs to be placed.

The constant __DIR__ returns the directory name of the current file (the file that is using it) the same as dirname( __FILE__ ) .

    
24.01.2015 / 12:15