I know I come up with the worst questions. As I'm not great at coding. I am very confused about how Routing Works. I see a lot of MVC Tutorials, talking about first parameter being a controller and second a valid method and the rest being params, but it does not make sense to me, how would that work with most pages being dynamic? ideally I would prefer to avoid the MVC pattern and have each document responsible for itself.
But to keep it simple, Say I want the following clean dynamic URL:
/Products/fruits/oranges/bloodoranges
Not like this:
/Products/product/fruits or /Products/product?=fruits
Products can have: fruits, nuts, beans etc, and perhaps n number of products in the future.
Fruits can have: oranges, bananas, kiwi etc...
If the user clicks nuts, it should be
/Products/nuts
The next page is a sub category of one of these, and the next a further sub category.
What would be the best logic?
If I keep methods for fruits, nuts, etc within Products, then that seems inefficient to me
I have already redirected everything to index.php.
Basically I want each URL to map to a dynamic document which I can make up with other components like headers, footers etc, Rather than everything as Models, views, controllers
Can this work?
So far I have done the basics:
#HTAccess:
RewriteEngine on
Options -indexes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,NC,L]
//Request Class Constructor
function __construct()
{
foreach ($_SERVER as $key => $value)
{$this->httpRequest[filter_var($key,
FILTER_SANITIZE_URL)] = filter_var($value, FILTER_SANITIZE_URL);
}
if (isset($_GET['url'])) {
$this->params = explode('/', rtrim($_GET['url'], '/'));
$this->module = $this->params[0];
echo "Module: ". $this->module . "<br>";
unset($this->params[0]);
var_dump($this->params);
}
}
//Route
public function route()
{
if
(file_exists(MODULES.'/'.$this->module . '/'.$this->module.'.php')). {
echo "File found";
}else{
echo "File not found";
}
}
I was thinking of having a class with methods in an array like so:
class Module {
private $route = ['product', 'category', 'subcategory'];
//Map each URL to a method
public function product($data){
$this->createWiew(View $view, $data)
}
}
I know it should be really simple, I am just not getting it.