Ahoj,
nevíte někdo kde by mohla být chyba, stabilně mam tuto hlášku a nemohu přijít na problém:
Fatal error: Uncaught Error: Call to a member function query() on null in C:\xampp\htdocs\cms\app\model\UserManager.php:19 Stack trace: #0 C:\xampp\htdocs\cms\app\controllers\user\user.php(18): CMS\Model\UserManager->create('test', 'test@test.cz', 'test') #1 C:\xampp\htdocs\cms\admin\register.php(49): CMS\Controllers\User\User->createAction('test', 'test@test.cz', 'test') #2 {main} thrown in C:\xampp\htdocs\cms\app\model\UserManager.php on line 19
Register.php
<?php
require_once $_SERVER['DOCUMENT_ROOT'].'/cms/vendor/autoload.php';
session_start();
use CMS\Controllers\Database\Connection;
use CMS\Controllers\User\User;
$database = new Connection;
$user = new User($database);
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scal=1">
<meta name="description" content="">
<meta name="author" content="">
<title></title>
<link rel="stylesheet" href="../admin/css/admin.css">
<script src="../admin/js/tinymce/tinymce.js"></script>
<script src="../admin/js/tinymce/init-tinymce.js"></script>
</head>
<body>
<div id="main" class="layout-column-main">
<div class="admin-bar clearfix">
<a class="logo-wrapper" href="" title="Anywhere System"><span class="logo-system"></span></a>
<ul class="admin-bar-ul">
<li class="admin-bar-list-li">
<a href="login.php" class="admin-bar-list-inside">Přihlášení</a>
</li>
<li class="admin-bar-list-li">
<a href="register.php" class="admin-bar-list-inside">Registrace</a>
</li>
<li class="admin-bar-list-li">
<a class="admin-bar-list-inside" href="../index.php">Hlávní stránka</a>
</li>
</ul>
</div>
<div class="flex-column">
<?php include_once 'templates/content.php'; ?>
<?php
if(isset($_POST['register'])){
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$confirm_password = $_POST['confirm_password'];
if ($password == $confirm_password){
$register = $user->createAction($username, $email, $password);
if($register) {
echo "Registrace proběhla úspěšně";
}
}
}
?>
<form method="post" action="" class="form_align">
<input class="input-login-reg" type="text" name="username" placeholder=" Uživatelské jméno" required />
<input class="input-login-reg" type="email" name="email" placeholder=" Zadejte váš email" required />
<input class="input-login-reg" type="password" name="password" placeholder=" Zadejte vaše heslo" required />
<input class="input-login-reg" type="password" name="confirm_password" placeholder=" Potvrďte vaše heslo" required />
<button class="btn-login-reg" type="submit" name="register">Zaregistrovat se</button>
</form>
<?php include_once 'templates/footer.php'; ?>
Connection.php
<?php
namespace CMS\Controllers\Database;
/*
* Database connection class
*/
class Connection {
/*
*
*/
private $pdo;
public function __construct() {
$this->pdo = $this->connect();
}
/*
* connection function with database
*/
public function connect() {
try {
$config = parse_ini_file($_SERVER['DOCUMENT_ROOT'].'/cms/app/config/config.ini');
$this->pdo = new \PDO($config['dbtype'].":host=".$config['dbhost'].";dbname=".$config['dbname'], $config['dbuser'], $config['dbpass']);
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
// echo "Připojení k databázi proběhlo úspěšně";
} catch (\Exception $e) {
echo "Připojení k databazi se nezdařilo: ".$e->getMessage();
}
return $this->pdo;
}
public function disconnect() {
$this->pdo = null;
}
public function query($sql,...$params) {
$query = $this->pdo->prepare($sql);
return $query->execute(array($params));
}
public function fetch($sql,$params) {
return $this->pdo->query($sql,$params)->fetch();
}
public function fetchAll($sql,$params) {
return $this->pdo->query($sql,$params)->fetchAll();
}
}
UserManager.php
<?php
namespace CMS\Model;
use CMS\Controllers\Database\Connection;
class UserManager {
private $database;
public function __construct(Connection $database) {
$this->database = $database;
}
public function create($username,$email,$password) {
$hashed_password = password_hash($password, PASSWORD_BCRYPT);
$sql = "INSERT INTO users(username, email, password) VALUES (?,?,?)";
$params = array($username,$email,$hashed_password);
$query = $this->database->query($sql,$params);
return $query;
}
}
User.php
<?php
namespace CMS\Controllers\User;
use CMS\Controllers\Database\Connection;
use CMS\Model\UserManager;
class User extends UserManager{
private $database;
public function __construct(Connection $database) {
$this->database = $database;
}
public function createAction($username,$email,$password){
return $this->create($username, $email, $password);
}
}
Díky