Router and Auth classes
This commit is contained in:
commit
238ed149ec
|
@ -0,0 +1,69 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use PDO;
|
||||||
|
|
||||||
|
class Auth{
|
||||||
|
|
||||||
|
private $pdo;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor method to initialize the database connection and start the session
|
||||||
|
*/
|
||||||
|
|
||||||
|
public function __construct($host, $dbname, $username, $password){
|
||||||
|
$dsn = "mysql:host=$host;dbname=$dbname;charset=utf8mb4";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Instantiate the PDO object with the DSN, username, and password, and set attributes.
|
||||||
|
* Then start a new session
|
||||||
|
*/
|
||||||
|
|
||||||
|
$this->pdo = new PDO($dsn, $username. $password, [
|
||||||
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||||
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
||||||
|
]);
|
||||||
|
|
||||||
|
session_start();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method to autenticate the user based on the provided routes.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public function login($username, $password){
|
||||||
|
$sql = "SELECT * FROM users WHERE username = :username or email = :email LIMIT 1";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prepare the SQL query for execution.
|
||||||
|
* Then txecute the query with the provided username and password parameters and
|
||||||
|
* fetch the user record from the database.
|
||||||
|
*/
|
||||||
|
|
||||||
|
$query = $this->pdo->prepare($query);
|
||||||
|
$query->execute([
|
||||||
|
":username" => $username,
|
||||||
|
":password" => $password
|
||||||
|
]);
|
||||||
|
|
||||||
|
$user = $query->fetch();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if the provided password is verified against the hashed password in the database.
|
||||||
|
* If correct, store user information in a session.
|
||||||
|
*
|
||||||
|
* Return true indicating a successful login
|
||||||
|
*/
|
||||||
|
|
||||||
|
if($user && password_verify($password, $user['password'])){
|
||||||
|
$_SESSION['user_id'] = $user['id'];
|
||||||
|
$_SESSION['username'] = $user['username'];
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
|
@ -0,0 +1,54 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
class Router{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor method
|
||||||
|
* Executed when an instance of the Router class is created
|
||||||
|
*/
|
||||||
|
|
||||||
|
public function __construct(){
|
||||||
|
/* Parse the URL from the request and extract the path part (e.g., "/home" from "http://example.com/home") */
|
||||||
|
$uri = parse_url($_SERVER['REQUEST_URI'])['path'];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method to route the request to the appropriate controller
|
||||||
|
* based on the provided routes
|
||||||
|
*/
|
||||||
|
|
||||||
|
public function routeToController($routes){
|
||||||
|
// Check if the requested URI exists in the routes array
|
||||||
|
if(array_key_exists($this->uri, $routes)){
|
||||||
|
// If it exists, require (include and execute) the file corresponding to the route
|
||||||
|
require $routes[$uri];
|
||||||
|
}else{
|
||||||
|
// If the route doesn't exist, call the abort method to handle the error
|
||||||
|
$this->abort();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method to handle HTTP errors
|
||||||
|
* Default is 404 - Not Found
|
||||||
|
*/
|
||||||
|
|
||||||
|
public function abort($code = 404){
|
||||||
|
// Set the HTTP response code to the specified value
|
||||||
|
http_response_code($code);
|
||||||
|
// Include and execute the corresponding error view (e.g., "views/404.php")
|
||||||
|
require "views/{$code}.php";
|
||||||
|
// Terminate the script execution
|
||||||
|
die();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// $routes = [
|
||||||
|
// '/' => 'controllers/index.php',
|
||||||
|
// '/about' => 'controllers/about.php',
|
||||||
|
// '/contact' => 'controllers/contact.php',
|
||||||
|
// ];
|
||||||
|
|
||||||
|
// $router = (new Router)->routeToController($routes);
|
||||||
|
|
||||||
|
?>
|
Loading…
Reference in New Issue