I have followed a youtube playlist to build a simple MVC framework based on PHP, here is a link to the playlist I have followed.
and here is an image of my application structure enter image description here
I have connected the application to a MySql database, everything is working fine, but now I am trying to do friendly URLs.
Currently, my URLs look like the following:
mywebsite.com/home/listings/
this is fine as this link is for a static page, but I want the same for dynamic URLs, for example:
Current URL: mywebsite.com/home/listings/apart?id=10
to URL: mywebsite.com/home/listings/apart/id/10
I have tried a lot of methods in htaccss but the data is not pulled from the database once the URL is re-written and I get only an empty page.
current .htaccess code (the one that is located in the public folder):
Options -MultiViews
RewriteEngine On
RewriteBase /public
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
I have tried a lot of methods and rules but non have worked.
I am not sure if the details I provided are clear enough, I am little confused as I combined a lot of tutorials together to create this app.
App.php code:
<?php
class App{
protected $controller = 'home';
protected $method = 'index';
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(isset($url[1])){
if(method_exists($this->controller, $url[1])){
$this->method = $url[1];
unset($url[1]);
}
}
$this->params = $url ? array_values($url) : [];
call_user_func_array([$this->controller, $this->method], $this->params);
}
public function parseUrl(){
if(isset($_GET['url'])){
return $url = explode('/', filter_var(rtrim($_GET['url'], '/'), FILTER_SANITIZE_URL));
echo $url;
}
}
}
?>
controller.php code:
<?php
class Controller{
public function model($model){
require_once '../app/models/' . $model . '.php';
return new $model();
}
public function view($view, $data = []){
require_once '../app/views/' . $view . '.php';
}
}
?>
controllers code:
<?php
class home extends Controller{
public function index(){
$this->view('home/index');
}
public function listings(){
$this->view('home/listings');
}
public function property(){
$this->view('home/property');
}
}
?>
The project is online, I can give full access if someone interested in helping me solve this issue.
core/Controller.phpPresuming that's got the bit which parses$_GET['url'], btw no one want to sit through a youtube series looking for your issue, please post it here.$urlyou should put that inside$this->paramslike$this->params['params'], then you will be able to access the proceeding URL path from your controllers method. Can't tell which but it will be in the methodName($somthing)` in your controller file. Post a controllers mmetods code if you need further help$this->paramsso post the code of one of your controllers methods