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....