I wrote a class to generate URL as I need. (Using http or https and switching off the rewrite rules) That is my first OOP code. It's the right approach, any suggestion?
inc/class.urls.inc.php
class Url {
public function __construct() {
$this->rewrite = SITE_REWRITE;
$this->domain = SITE_URL;
}
public function home() {
return $this->domain;
}
public function page($page) {
if ($this->rewrite == 1) {
return $this->domain . '/' . $page . '.html'; // https://www.example.com/contact.html
} else {
return $this->domain . '/' . $page . '.php'; // https://www.example.com/cantact.php
}
}
public function manufacter($slug) {
if ($this->rewrite == 1) {
return $this->domain . '/' . $slug; // https://www.example.com/apple
} else {
return $this->domain . '/manufacter.php?s=' . $slug; // https://www.example.com/manufacter.php?s=apple
}
}
public function product($slug, $manufacter) {
if ($this->rewrite == 1) {
return $this->domain . '/' . $manufacter . '/' . $slug; // https://www.example.com/apple/iPhone
} else {
return $this->domain . '/product.php?s=' . $slug; // https://www.example.com/product.php?s=iPhone
}
}
public function category($slug) {
if ($this->rewrite == 1) {
return $this->domain . '/' . 'categories/' . $slug; // https://www.example.com/categories/phone
} else {
return $this->domain . '/category.php?s=' . $slug; // https://www.example.com/category.php?s=phone
}
}
}
Page
<?php
DEFINE('SITE_NAME','Site Name');
DEFINE('SITE_DOMAINS','www.exemple.com');
DEFINE('SITE_HTTP','https://');
DEFINE('SITE_URL',SITE_HTTP.SITE_DOMAINS);
DEFINE('SITE_REWRITE',1);
include_once 'inc/class.urls.inc.php';
$url = new Url();
echo '<b>Home:</b> '.$url->home() ."<br>";
echo '<b>Page:</b> '.$url->page('contacts') ."<br>";
echo '<b>Category:</b> '.$url->category('ipa') ."<br>";
echo '<b>Product:</b> '.$url->product('punk-ipa','brew-dog') ."<br>";
echo '<b>Manufacter:</b> '.$url->manufacter('brew-dog') ."<br>";
?>
Result
Home: https://www.exemple.com
Page: https://www.exemple.com/contacts.html
Category: https://www.exemple.com/categories/ipa
Product: https://www.exemple.com/brew-dog/punk-ipa
Manufacter: https://www.exemple.com/brew-dog