parse url Algorith

In stock

parse url Algorith

Step 1:
Set Default Values for $folder, $controller, $method, and $params.
$folder = ‘user’; $controller = ‘HomeController’; $method = ‘index’; $params = [];
Step 2:
Check if $_GET[‘url’] is set:
-> If set, assign $url = $_GET[‘url’].
-> If not set, assign $url = ”.
Step 3:
Trim Slashes:
-> Use trim($url, ‘/’) to remove leading and trailing slashes.
Step 4:
Check if URL is Empty:
If $url is empty after trimming:
Set $url = [] (empty array).
Return default values [‘user’, ‘HomeController’, ‘index’, []].
If $url is not empty, proceed to sanitize and split it.
Step 5:
Validate and Sanitize URL:
If filter_var($url, FILTER_VALIDATE_URL) is false, return ‘Invalid URL’ ❌.
If preg_match(‘/^[a-zA-Z0-9\/_-]+$/’, $url) is false, return ‘Malformed URL’ ⚠️.
Use filter_var($url, FILTER_SANITIZE_URL) to remove illegal characters.
Use explode(‘/’, $url) to split the URL into an array of segments.
Step 6:
Handle Empty URL/Base URL (e.g., http://example.com/):
If $url[0] is empty or not set, return default values [‘user’, ‘HomeController’, ‘index’, []].
If $url[0] is not empty, proceed with further processing (determining folder, controller, method, and parameters).

Step 7:
Check for ‘admin’ Folder:
If the first segment URL, $url[0] is ‘admin’, set $folder = ‘admin’.
If the second segment $url[1] exists and is not empty, capitalize it and append with ‘Controller’:
$controller = ucfirst($url[1]) . ‘Controller’;
Else, set $controller = ‘DashBoardController’.
If it’s empty, the default controller is ‘DashBoardController’.
The third segment URL, $url[2] is used to determine the method.
If $url[2] is not empty, set $method = $url[2].
If $url[2] is empty, the default method is ‘index’. Set $method = ‘index’.

Step 8:
Handle ‘user’ Folder (Default Case):
If the first segment, $url[0] is not ‘admin’, set $folder = ‘user’.
If second segment $url[1] is not empty, capitalize it and append with ‘Controller’:
$controller = ucfirst($url[1]) . ‘Controller’;
Else if $url[1] is empty, the default controller is ‘HomeController’.
Set $controller = ‘HomeController’.
If the third segment ($url[2]) is not empty, it is used as the method.
Set $method = $url[2].
Else if $url[2] is empty, the default method is ‘index’.
Set $method = ‘index’.

Step 9:
Extract Parameters:
If the fourth segment $url[3] is not empty, assign $url[3] to $params array.
Set $params = array_slice($url, 3).
Else, set $params = [].
if (isset($url[3])) {
$params = array_slice($url, 3);
} else {
$params = [];
}

Step 10:
Return Parsed Values:
Return [$folder, $controller, $method, $params].

Step-by-Step Mnemonic Breakdown

🏁 Step 1: Find the Default Route (Find)
📌 Concept: Start with default values (safe starting point).
📝 Mnemonic: “If lost, return home.”

$folder = ‘user’;
$controller = ‘HomeController’;
$method = ‘index’;
$params = [];
🔍 Step 2: Trim the URL (Trim)
📌 Concept: Remove unnecessary parts from the URL.
📝 Mnemonic: “Clean the edges before using it.”

$url = isset($_GET[‘url’]) ? $_GET[‘url’] : ”;
$url = trim($url, ‘/’);
🛡 Step 3: Sanitize Input (Sanitize)
📌 Concept: Prevent malicious input.
📝 Mnemonic: “Sanitize before use.”

$url = filter_var($url, FILTER_SANITIZE_URL);
✅ Step 4: Validate URL Format (Validate)
📌 Concept: Ensure it’s a valid, structured URL.
📝 Mnemonic: “Check ID before entry.”

if (!preg_match(‘/^[a-zA-Z0-9\/_-]+$/’, $url)) {
return ‘Malformed URL’;
}
🌍 Step 5: Handle Empty URLs (Handle)
📌 Concept: If no URL is provided, use the defaults.
📝 Mnemonic: “If the address is empty, go home.”

if (empty($url)) {
return [‘user’, ‘HomeController’, ‘index’, []];
}
🏢 Step 6: Check for Admin Panel (Admin)
📌 Concept: If the URL starts with “admin”, load the admin dashboard.
📝 Mnemonic: “Admin has a separate entrance.”

if ($url[0] == ‘admin’) {
$folder = ‘admin’;
$controller = isset($url[1]) ? ucfirst($url[1]) . ‘Controller’ : ‘DashBoardController’;
}
👤 Step 7: Handle User Panel (Default Case) (User)
📌 Concept: If not admin, it’s a user request.
📝 Mnemonic: “Users go through the main gate.”

if ($folder === ‘user’) {
$controller = isset($url[1]) ? ucfirst($url[1]) . ‘Controller’ : ‘HomeController’;
}
⚙ Step 8: Extract Method from URL (Extract)
📌 Concept: Assign a method based on the URL.
📝 Mnemonic: “Controller decides what to do next.”

$method = isset($url[2]) ? $url[2] : ‘index’;
🧳 Step 9: Retrieve Additional Parameters (Routes)
📌 Concept: Capture extra URL parts as parameters.
📝 Mnemonic: “Carry extra luggage only if needed.”

$params = array_slice($url, 3);
🚀 Step 10: Return Parsed Components (Return)
📌 Concept: Return structured output.
📝 Mnemonic: “Pack everything and send it off.”

return [$folder, $controller, $method, $params];

Find → Clean → Secure → Check → Go → Admin/User → Do → Carry → Send
Step Action Mnemonic
1 Set Defaults “If lost, go home.”
2 Trim Slashes “Cut off extra edges.”
3 Sanitize Input “Keep it clean.”
4 Validate Format “Check ID before entry.”
5 Handle Empty URL “If empty, go home.”
6 Check for Admin “Admin has a VIP gate.”
7 Handle User Panel “Users enter through the main gate.”
8 Extract Method “What action to perform?”
9 Retrieve Parameters “Grab extra items if needed.”
10 Return Components “Pack and deliver.”

Main Menu

parse url Algorith

parse url Algorith