PHP Routing - error includes css / js files
I am learning to do routing in php. I can redirect the page without any error, but I am stuck including files css
and js
. The environment I'm using for the tutorial and example is on the laracast video. I am also using xampp
apache for server on windows.
I wanted to do my own practice and below image is the file structure I made,
in my file .htaccess
, I have:
RewriteEngine On
RewriteBase /season-temp
RewriteRule ^.*$ index.php [END]
In my views folder, I have the entire view page file including header
and footer
to include css
and js
.
header.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>Logo Nav - Start Bootstrap Template</title>
<!-- Bootstrap Core CSS -->
<link href="/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom CSS -->
<link href="/css/logo-nav.css" rel="stylesheet">
</head>
<body>
<?php require('navigations.php'); ?>
footer.php
<!-- jQuery -->
<script src="/js/jquery.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="/js/bootstrap.min.js"></script>
</body>
</html>
From the codes above, it will give an error:
GET http://localhost/css/bootstrap.min.css
GET http://localhost/css/logo-nav.css
GET http://localhost/js/jquery.js
GET http://localhost/js/bootstrap.min.js
I tried to paste <base href="/">
in a chapter section and link to each src / href like 'season-temp/<folder_name>/<file_name>'
for example; 'season-temp/css/bootstrap.min.css'
but still can't get css and js files.
How do I enable these files?
Update - wrong output
First, we apologize for the incorrect update below. The css and js did work, but the link doesn't work. If I .htaccess
revoke the database in @ Triby's answer, the link works fine, but the css and js don't. Below are the pages for mine navigations.php
androutes.php
navigations.php
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>
<a href="/season-temp/about">About</a>
</li>
<li>
<a href="/season-temp/services">Services</a>
</li>
<li>
<a href="/season-temp/contact">Contact</a>
</li>
</ul>
</div>
routes.php
<?php
$router->get('season-temp','controllers/index.php');
$router->get('season-temp/about','controllers/about.php');
$router->get('season-temp/services','controllers/services.php');
$router->get('season-temp/contact','controllers/contact.php');
If I update the link as @ceejayoz mentioned for the css and js files, I got the error:
<br />
<b>Fatal error</b>: Uncaught exception 'Exception' with message
'No route defined for this URI.'
in C:\xampp\htdocs\season-temp\core\router.php:30
Stack trace:
#0 C:\xampp\htdocs\season-temp\index.php(9):
Router->direct('season-temp/js/...', 'GET')
#1 {main}
thrown in <b>C:\xampp\htdocs\season-temp\core\router.php</b>
on line <b>30</b><br />
source to share
You are redirecting everything to index.php, trying to redirect only non-existent files or directories:
RewriteEngine On
RewriteBase /season-temp
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [L]
L Should avoid handling any of the rules below when the rule matches
source to share