Linesofcode
Linesofcode

Reputation: 5923

PHP require link from root folder

I've searching around and I actually found some topics related, but the suggestions there did not worked for me.

My project folder structure is the following:

projectSample (name of folder)

 » backend (folder)
  -> index.php
  -> orders.php
  » Admin (folder)
   -> index.php

 » libs (folder)
  -> config.php
  -> orders.php
  -> global.php

 » views (folder)
  » backend (folder)
   -> header.php
   » content (folder)
    -> index.php
    -> orders.php
    -> unique_order.php
    -> add_order.php
    » admin (folder)
     -> index.php

 » css (folder)
 » js (folder)

I have a function inside the file global.php called getLanguageFile(), which requires the file appropriate.

public static function getLanguageFile(){
    $default = 'pt';

    if(isset($_COOKIE['language'])){
        switch($_COOKIE['language']){
            case 'fr': break;
            case 'en_US': $default = 'en'; break;
            case 'es': break;
        }
    }
    $path = rootPath . "/languages/" . $default . ".php";

    return require_once($path);
}

I call this function in all files of Backend folder. The variable rootPath has the following value in the config.php file.

define("rootPath", '/projectSample');

This does not work and I receive in every page the following error:

Warning: require_once(/projectSample/languages/pt.php): failed to open stream: No such file or directory in C:\xampp\htdocs\projectSample\libs\global.php on line 370

But looking at the path, it is actually correct (/projectSample/languages/pt.php) actually exists. I've tried to adapt other functions to my needs such as:

define("rootPath", dirname(dirname(__FILE__))); 

This one seems to work fine for the require_onces function, but not for css/js files. If I use the same variable for css/js files to include I receive and error on Chrome console:

Not allowed to load local resource: file:///C:/xampp/htdocs/projectSample/css/bootstrap/bootstrap.min.css

So, using another variable just for css/js seems the best approach but I can't put it worked for all files.

define("clientPath", dirname(dirname($_SERVER['PHP_SELF'])));

This variable works fine for » Backend folder (and for all each files), however, it does not work well on the folder Admin.

<link rel='stylesheet' href='<?php echo clientPath. "/css/bootstrap/bootstrap.min.css"; ?>'/>

» backend (folder) > works
 -> index.php > works
 -> orders.php -> works
 » admin (folder) > does not work
  -> index.php > does not work

The reason why it does not work in that folders its because the final link is:

http://localhost/projectSample/backend/css/bootstrap/bootstrap.min.css

And it should be:

http://localhost/projectSample/css/bootstrap/bootstrap.min.css

Ok, I could create another variable (like the below), but is that the best approach?

define("clientPath_admin", dirname(dirname(dirname($_SERVER['PHP_SELF']))));

Sorry for the long topic, and I'm not using basehref.

Upvotes: 0

Views: 668

Answers (1)

Vigneshwaran
Vigneshwaran

Reputation: 397

One possible way is,

Declare, this is the frontend path

 define("BASEPATH", "http://localhost/projectSample/");
 define("BASEPATH_TO_ADMIN", "http://localhost/projectSample/backend/");

In Html part

<link rel='stylesheet' href='<?php echo BASEPATH. "css/bootstrap/bootstrap.min.css"; ?>'/>

Upvotes: 2

Related Questions