This is my first routing system on PHP. As you can see it's very simple. Any advice or tip about it that can make it better? I want to make it better so I could use it in real websites.
First I include db connect, then get the URL, if the URL have special index like post id or post name, then include post.php or if $sp_link[4] == profile then load the profile.php.
<?php
require_once 'inc/header.php';
include_once 'inc/nav.php';
$actual_link = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
// echo $actual_link;
$sp_link = explode("/", $actual_link);
print_r($sp_link);
$sql ='SELECT * from posts WHERE post_id=? AND post_name=?';
$stmt=$pdo->prepare($sql);
$stmt->execute([@$sp_link[5], @$sp_link[6]]);
$post=$stmt->fetch();
if ($sp_link[4] == 'blog' && @$sp_link[5] == '' ) {
// @include 'blog.php';
//$page_title = $post->post_title;
}
else if ($sp_link[4] == 'profile' && @$sp_link[5] == '' ) {
@include_once 'profile.php';
}
else if ($sp_link[4] == 'login' && @$sp_link[5] == '' ) {
@include 'login.php';
$page_title = 'ورود';
}
else if ($sp_link[4] == 'signup' && @$sp_link[5] == '' ) {
@include 'signup.php';
$page_title = 'ورود';
}
else if ($sp_link[4] == 'blog' && @$sp_link[5] == $post->post_id && @$sp_link[6] == $post->post_name) {
@include 'post.php';
$page_title = $post->post_title;
}
else if (($sp_link[4] == '' || $sp_link[4] == 'index' || $sp_link[4] == 'index.php') && @$sp_link[5] == '') {
$page_title = 'سکان آکادمی | آموزش برنامه نویسی';
include 'home.php';
}
else if (($sp_link[4] == 'blog' && $sp_link[5] == '12342') && @$sp_link[6] == '') {
include_once 'post.php';
} else {
include_once '404.php';
}