vtomic85
vtomic85

Reputation: 611

Unable to include/require PHP file

I'm making a simple application for managing the employees' tasks, called WorkerManager. At first, I had the following structure:

workermanager
|-index.php
|
|-user
|   |-dao
|   |  |-UserDAO.php
|   |-model
|   |-view
|-task
|   |-dao
|   |-model
|   |-view
|-department
|   |-dao
|   |-model
|   |-view
...

So, UserDAO.php was located in workermanager/user/dao/ and I was including it using require_once './user/dao/UserDAO.php'.

Later on, I decided to change the structure of the project and made something like this:

workermanager
|-index.php
|
|-dao
|   |-user
|   |   |-UserDAO.php
|   |-task
|   |-department
|-model
|   |-user
|   |-task
|   |-department
|-view
|   |-user
|   |-task
|   |-department
...

I did this by creating folders, dragging & dropping inside the NetBeans Projects pane. Since then, I'm unable to include UserDAO.php (or any other file that changed the location). The function file_exists('./dao/user/UserDAO.php') returns false. When I echo the file location using $_SERVER['DOCUMENT_ROOT'].'/workermanager/dao/user/UserDAO.php', it gives the correct path, but when I try to include or require it this way, it doesn't work. I have tried to erase this project and to make a new one with existing sources, but it still doesn't work.

Any ideas what can I do?

Next time when I decide to change the tree structure, should I do it outside the NetBeans? Is there any way to make the NetBeans "scan" the project structure and to "learn" new paths?

I'm using NetBeans 8.1, Apache 2.4.18, Ubuntu 16.04

Thanks.

Edit: Slightly off-topic... In a project like this, if I have one "model", one "DAO" and one or more "view" files for each entity, what structure do you prefer? Are there any "best practices" regarding the project tree shape?

Edit 2: I've just made a new project with the following structure:

shoppinglist
|-index.php
|...
|-list
|   |-ShoppingList.php
|   |-ShoppingListController.php
|   |-create-new-list.php
|   |-edit-list.php
|...
...

In index.php I have require_once './list/ShoppingListController.php'; and it works perfectly. But in list/edit-list.php I have exactly the same line and it doesn't work. I have also tried to require/include only 'ShoppingListController.php', as well as $_SERVER['DOCUMENT_ROOT'].'/shoppinglist/list/ShoppingListController.php', but still nothing. Can anyone tell me what is going on???

Upvotes: 1

Views: 567

Answers (2)

vtomic85
vtomic85

Reputation: 611

I've finally found the solution!

In every include or require I have started the path with './'. I thought it was the root of my site, but it's the current folder. So, it was fine to use './list/ShoppingListController.php' in index.php, but it shouldn't be used in edit-list.php, which is located in the same folder as ShoppingListController.php.

The solution was to use dirname(__FILE__) at the beginning of each include or require and then to concatenate the rest of the path.

For example, in index.php I have:

require_once dirname(__FILE__).'/list/ShoppingListController.php';

...while in edit-list.php I have:

require dirname(__FILE__).'/ShoppingListController.php';

In ShoppingListController.php I need to include several files, so I have:

require_once dirname(__FILE__).'/../db/DBConnection.php';
require_once dirname(__FILE__).'/ShoppingList.php';
require_once dirname(__FILE__).'/../article/Article.php';

That solved my problem and now everything works perfectly :)

Upvotes: 0

Karol Kalinowski
Karol Kalinowski

Reputation: 46

It is possible that Apache cached your index.php file, and still uses the old paths. You should check that.

Netbeans is just an IDE that changes your files. There is no way Netbeans is problem here, you could as well use Notepad to change these paths, that would be the same from the project perspective.

Upvotes: 1

Related Questions