0

I've tried to include the css from a global var but styles are not included.

Global vars:

<?php 

require_once '../app/init.php';
define('ROOT_DIR', dirname(__FILE__));
define('CSS_DIR', ROOT_DIR . '\css');
define('CSS_BOOTSTRAP', CSS_DIR . '\bootstrap.min.css');
define('CSS_STYLES', CSS_DIR . '\styles.css');       

$app = new App;

HTML:

<html lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta charset="utf-8">
    <title>Bootstrap Login Form</title>
    <meta name="generator" content="Bootply" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
            <link href='<?php echo CSS_BOOTSTRAP; ?>' rel="stylesheet">
    <!--[if lt IE 9]>
        <script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link href='<?php echo CSS_STYLES; ?>' rel="stylesheet">


</head>
<body>

Code from browser:

<html lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <meta charset="utf-8">
    <title>Bootstrap Login Form</title>
    <meta name="generator" content="Bootply" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
            <link rel="stylesheet" type="text/css" href="C:\xampp\htdocs\grade_your_room\public\css\bootstrap.min.css" >
    <!--[if lt IE 9]>
        <script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link rel="stylesheet" type="text/css" href="C:\xampp\htdocs\grade_your_room\public\css\styles.css" >


</head>
<body>

If i try to acces the path C:\xampp\htdocs\grade_your_room\public\css\styles.css the styles can be showed.

1
  • 1
    link's href attribute must have a relative/absolute link as a value, not a local path. It therefore should be: <link href="/css/styles.css" > if the domain you are is, indeed, targeting your C:\xampp\htdocs\grade_your_room\public folder (check Apache's vhost configuration). Commented Jul 28, 2015 at 10:02

1 Answer 1

2

This is normal dirname (https://secure.php.net/manual/en/function.dirname.php) returns parent directory's path in local!

You should set ROOT_DIR to your domain name (localhost for your development).

<?php
require_once '../app/init.php';
define('ROOT_DIR', 'localhost');
define('CSS_DIR', ROOT_DIR . '/css');
define('CSS_BOOTSTRAP', CSS_DIR . '/bootstrap.min.css');
define('CSS_STYLES', CSS_DIR . '/styles.css'); 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, one last question. It's a good idea use global var's to link the css and js or not?
For me yes, I have done the same when I was building my own framework.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.