I´m writing my own PHP MVC framework. So, the entry point for bootstrapping is at myapp/public/index.php
.
There I check the URL passed, as:
$url = $_GET["url"];
And then I explode the code finding the necessary controller, action and parameters to navigate:
$parts = explode("/", trim($url, "/"));
if (sizeof($parts) > 0)
{
$controller = $parts[0];
if (sizeof($parts) >= 2)
{
$action = $parts[1];
$parameters = array_slice($parts, 2);
}
}
From here the code loads and runs the controller and view.
At the controller, I need (after successfull login) redirect to my app main page (called main/index
). Here is the redirection code:
... controller code...
$this->redirect("main", "index");
...
And
public static function redirect($controller, $method = "index", $args = array())
{
$location = "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'] . $controller . "/" . $method . "/" . implode("/",$args);
header("Location: " . $location);
exit;
}
Well, this calls again my public/index.php
, but from now I loose all of my relative path referentes in the included javascript and css code, like:
<link rel="stylesheet" rel="stylesheet" media="all" href="css/bootstrap.min.css">
<script type="text/javascript" charset="UTF-8" src="js/jquery-2.1.4.min.js"></script>
Insted of looking at myapp/public
it starts looking at myapp/pubic/main/index
folder, where it natually does not belong to.
Also, myapp/public/main/index
is not even a valid folder (the controller and action is code is under application/controllers
folder).
I´m very confused in the way this mechanism of redirecting is working and I can´t find out the solution..
My .htaccess
, located in myapp\public
:
Options -indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^([a-zA-Z0-9\/\-_]+)\.?([a-zA-Z]+)?$ index.php?url=$1&extension=$2 [QSA,L]
Thanks for helping....
This is the problem with relative links in your css/images etc. Relative paths are resolved by adding the relative path in the parent folder of current page's path.
To resolve, you can add this in the
<head>
section of your page's HTML:so that every relative URL is resolved from that base URL and not from the current page's URL.
It seems to me that it's working the way it should. Relative paths will always be relative to the current location. I think absolute paths would be your best bet. You could use a constant for your base directory like BASE_DIR and make all of your content paths relative to that.