Suraj Kumar
Suraj Kumar

Reputation: 11

Unable to loan external CSS and JS file in php

I am working on a project build on php. I am building it from the scratch using core php. So I boot a server using this

    php -S localhost:3000

The problem is I implemented the routing in the application, No my application is unable to load any of the external files. This is the route file :

<?php
// Grabs the URI and breaks it apart in case we have querystring stuff
$request_uri = explode('?', $_SERVER['REQUEST_URI'], 2);

// Route it up!
switch ($request_uri[0]) {
    // Home page
    case '/':
        require '../views/Home/Home.php';
        break;
    // About page
    case '/login':
        require '../views/Login/Login.php';
        break;
    // Everything else
    default:
        header('HTTP/1.0 404 Not Found');
        require '../views/404/404.php';
        break;
}
?>

Here is another file where I want to add some external JS or CSS file :

<?php require_once "../views/Header/Header.php"?>
<script type="text/javascript" src="Login.js"></script>
<link rel="stylesheet"

Now its saying 404 file not found.

File Structure is like this :

---src(root)
   ---public
      ---index.php(route file)
   ---views
      ---Login
         ---Login.php
         ---Login.js
         ---Login.css

File Structure

Upvotes: 0

Views: 57

Answers (1)

Thanh Trung
Thanh Trung

Reputation: 3804

Putting php and JS and CSS together is a bad bad bad structure. You JS and CSS are files need to be accessible from public. So put in a folder like public/js/Login.js and public/css/Login.css

Then change the path of the src src="js/Login.js" assuming index.php is the entry file.

Upvotes: 1

Related Questions