Using the MVC Framework style, the following router loads controllers once the keyword from "GET" url variable is found in the controllers folder and load its view(html) from the views folder.
I have "login.php"
,"signup.php"
,"index.php"
,"logout.php"
,"ajax.php"
,"get_started.php"
files in the controllers folder.
I have the following in the main directory:
"index.php"(file)
,".htaccess"(file)
,"init.php"(file)
,"app"(folder)
,"assets"(folder)
How do I make an example: "home.php"
in the controllers folder the default page such that if there is no keyword "home" as url variable it will still work.
By doing this, the url address should then look like:http://localhost/joacmedia/
and not http://localhost/joacmedia/home
after the user is logged in.
How possible can I achieve this with .htaccess or app router?
MY APP ROUTER:
<?php
//DEFINE A CLASS OF APP
//CREATING THE ROUTER...
//LOAD A PAGE OR CONTROLLER ONCE THE "URL" GET VARIABLE IS FOUND AS CONTROLLER
class App
{
//DEFINE DEFAULT PROPERTIES FOR CONTROLLER AND METHOD
public $controller = "login", $method = "index", $params = [];
public function __construct()
{
$url = $this->splitUrl();
//CHECK IF A CONTROLLER EXISTS
if (file_exists("app/controllers/". strtolower($url[0]) .".php"))
{
$this->controller = strtolower($url[0]);
unset($url[0]);
}
else
{
//NO CONTROLLER FOUND
//SHOW 404 PAGE
include "app/views/404.php";
//STOP THE SCRIPT IF A CONTROLLER IS NOT FOUND
exit;
}
//LOAD CONTROLLER AND METHOD
require ("app/controllers/". $this->controller .".php");
//CHECK IF A CLASS EXISTS
/*FOR DEBUG PURPOSES*/
if (class_exists($this->controller))
{
//INSTANTIATE THIS CLASS
$this->controller = new $this->controller;
//LOOK FOR THE METHOD OF THIS CLASS INSTANTIATED ABOVE
if (isset($url[1]))
{
if (method_exists($this->controller, $url[1]))
{
//OVERWITE DEFAULT METHOD
$this->method = $url[1];
unset($url[1]);
}
}
//RUN THE CLASS AND METHOD
$url = $this->params = array_values($url);
call_user_func_array([$this->controller,$this->method], $this->params);
}
else
{
//INSTANTIATE DEBUGER
$DEBUG = new DEBUGER();
$DEBUG->Logger($this->controller." : CLASS could not be found.");
}
}
//THIS METHOD SPLIT URL RETURNING THE CONTROLLER CLASS AND METHOD
private function splitUrl()
{
$url = isset($_GET["url"]) ? strtolower($_GET["url"]) : 'login';
return explode("/", filter_var(trim($url,"/"), FILTER_SANITIZE_URL ));
}
}
HTACCESS FILE sitting in the main directory:
Options -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
My Answer:
To change the default route to "home.php" in the controllers folder, you can modify the App class constructor in your router as follows:
php
public function __construct()
{
$url = $this->splitUrl();
// Check if user is logged in
$loggedIn = true; // Assuming user is logged in
// Check if a controller exists
if ($loggedIn && empty($url[0])) {
$this->controller = "home";
} elseif (file_exists("app/controllers/" . strtolower($url[0]) . ".php")) {
$this->controller = strtolower($url[0]);
unset($url[0]);
} else {
// No controller found
// Show 404 page
include "app/views/404.php";
// Stop the script if a controller is not found
exit;
}
// Load controller and method
require("app/controllers/" . $this->controller . ".php");
// Check if a class exists
if (class_exists($this->controller)) {
// Instantiate the class
$this->controller = new $this->controller;
// Look for the method of the class instantiated above
if (isset($url[1])) {
if (method_exists($this->controller, $url[1])) {
// Overwrite default method
$this->method = $url[1];
unset($url[1]);
}
}
// Run the class and method
$url = $this->params = array_values($url);
call_user_func_array([$this->controller, $this->method], $this->params);
} else {
// Instantiate debugger
$DEBUG = new DEBUGER();
$DEBUG->Logger($this->controller . " : CLASS could not be found.");
}
}
With this modification, if the user is logged in and there is no keyword in the URL, the default controller will be set to "home". This will allow the URL to look like http://localhost/joacmedia/ instead of http://localhost/joacmedia/home after the user is logged in.
Rate this post
5 of 5 based on 1842 votesComments