Problem with requires and URL

1

Good afternoon,

I have a problem with the topic of internal routes on a platform in php.

This is the error message:

Fatal error: require_once(): Failed opening required 'http://localhost:8080/CBS/CBSPlatform/html/links.php' (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\CBS\CBSPlatform\dashboard.php on line 34

This is what I have in my mind and in the code, which is surely wrong:

When I do the require_once to put every part of the page ...

<?php require_once($CRDomain . "html/header.php"); ?>

... I put the $ CRDomain that saves the path of the page, to access each file instead of by assets / css / css.css access with link . I do this for "supposedly" when it comes to the case that for example, the html pages to administer something are in administration / instead of in the main index folder. do not have to add ../ in each file that is entered in that page.

Returning to the error above, in the file links.php, which supposedly does not find, I have all the example css:

<link href="<?php echo $CRDomain; ?>assets/css/mainstyle.css" rel="stylesheet" type="text/css" />

With the CRDomain also to access as I said before.

The fact is that I do not understand what I'm doing wrong, if someone could review the code to tell me that I'm doing wrong with the routes of the files I would appreciate it very much.

I do not know if I've explained myself correctly, if not tell me and I try to clarify it more in some way.

Greetings

    
asked by Charlie Clewer 09.08.2017 в 21:42
source

2 answers

1

Hello @Charlie Clewer I do not know if I understood your question, please clarify me in case it is convenient. Do you need to define some constants to then invoke anywhere you need your file structure? In that case. I do the following:

In a file Example: global.php I have defined

php tag

define ("VIEWS", "views / css");

-Where views is a folder and css a sub-folder that contains several folders or files that you will require in other files -

This file is required in the main index or where you use these defined constants.

Example: index.php

start php tag

require_once "global.php";

start html tag

php tag

require_once (VIEWS. "/css.css");

require_once (VIEWS. "/style.css");

close php

continuous html code

Clarifying. It would be, have a file that are defined routes, then invoke that file and require your defined parameters. If I have any doubt, I am attentive. Greetings. I hope to be of help .-

    
answered by 11.08.2017 / 07:39
source
1

The error occurs because in PHP, include, require and require_once refer to the absolute path in the server, not to a URL, let's say then that its use is not correctly applied here, for what is required they should exist, in any case, 2 variables:

$CRDomain = 'http://localhost:8080/CBS/CBSPlatform/';
$AbsRoute = 'C:\xampp\htdocs\CBS\CBSPlatform\';

This way you could call:

<?php require_once($AbsRoute . "html/header.php"); ?>

And use:

<link href="<?php echo $CRDomain; ?>assets/css/mainstyle.css" rel="stylesheet" type="text/css" />

For the assets accessible from the URL, this would be a better application.

$_SERVER['DOCUMENT_ROOT']

It works because it is a global variable that returns the document's root directory of the server in which the current script is running, as defined in the server's configuration file, but all the variables are defined and included. , it will also work.

    
answered by 11.08.2017 в 09:10