Reputation: 45
im trying to build my mvc framework and im encountering some problems regarding url.
i have setup my .htaccess file and i can retrieve the url and explode it to an array.
My problem is when i start clicking links on my page, my framework keeps adding them to the url and i end up with a long url that my framework is unable to use to find the right controller.
EX:
at the root of my site the url is:
localhost/root
when i click a link for the first time, the url change to:
localhost/root/controller/model/params
if i click on another link, my url will be:
localhost/root/controller/model/params/controller/model/params <-- here is where i get the problem because the url is not properly formated for the framework to use it.
I dont know if the problem is in the .htacces or in my framework. What i would like to be able to do is regardless of where i am in my webpage i want the url to be always localhost/root
my .htaccess:
Options -MultiViews
RewriteEngine On
Options -Indexes
RewriteBase /root
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
and my main php file is:
class main {
protected $controller ="_default";
protected $method ="_getDefaultView";
protected $params;
public function __construct(){
$url = $this->parseUrl();
if(file_exists('app/controllers/'.$url[0].'.php')){
$this->controller = $url[0];
unset($url[0]);
}
require_once('app/controllers/'.$this->controller.'.php');
$this->controller = new $this->controller;
if(method_exists($this->controller, $url[1])){
$this->method = $url[1];
unset($url[1]);
}
$this->params = $url ? array_values($url) : [];
call_user_func([$this->controller, $this->method], $this->params);
}
public function parseUrl(){
if (isset($_GET['url'])){
return $newUrl = explode('/', filter_var(rtrim ($_GET['url'], '/'), FILTER_SANITIZE_URL));
}
}
}
help is appreciated. :-)
Upvotes: 0
Views: 119
Reputation: 45
My question was answered by @maniteja where he suggest to contruct the links like this
<a href="/controller/model/params">url</a>
all i did was to add /root.
credits are yours @maniteja
Upvotes: 0