我正在尝试使用PHP开发一个小型MVC网站以使用密码。 (出于学习目的,没有任何类似Laravel之类的框架)。
为此,我至少编写了4个类和一个索引文件:
Controller->要扩展的基类,它包含控制器的所有基本逻辑;
索引->用于处理普通用户登录的控制器类;
Admin->用于处理管理员用户登录的控制器类;
Bootstrap->一个类,用于解释传递的URL并用作路由器;
这些类如下所示:
libs / Controller.php
<?php
class Controller {
protected $view;
protected $security_level;
function __construct() {
$this->security_level = 0;
}
public function model($model) {
$path = 'models/' . ucfirst($model). '.php';
if (file_exists($path)) {
require_once ($path);
return new $model();
}
}
public function getSecurityLevel(){
return $this->securiy_level;
}
public function view($view, $data = []){
require_once('views/commom/header.php');
require_once('views/' . $view . '.php');
require_once('views/commom/footer.php');
}
}
controllers / Index.php
<?php
class Index extends Controller {
public function __construct() {
parent::__construct();
$this->model('UserModel');
//$this->securiy_level = 0;
}
public function index($error = 0) {
$this->view('index/index', ['error' => $error]);
}
public function admin($error = 0) {
$this->view('index/index_adm', ['error' => $error]);
}
public function login(){
$auth = new Authentication();
$permission = $auth->authenticate("user");
if($permission){
header("location: /account/index");
}
else{
$this->index(1);
}
}
public function login_adm(){
$auth = new Authentication();
$permission = $auth->authenticate("admin");
if($permission){
header("location: /admin/index");
}
else{
$this->admin(1);
}
}
public function signin(){
echo "method sign in invoked <br />";
}
public function logout(){
$this->view('index/logout');
}
public function lostMyPassword(){
echo "method lost invoked <br />";
}
public function details() {
$this->view->view('index/index');
}
}
控制器/管理员
<?php
//I Think that this is VERY wrong, but okay
@session_start();
class Admin extends Controller {
private $encrypt_unit;
public function __construct() {
parent::__construct();
$this->model('UserModel');
$this->encrypt_unit = new Encrypter();
$this->securiy_level = 1;
}
public function index($msg = "", $err = false){
$users = $this->recover();
$this->view('admin/index', ["msg" => $msg, "err" => $err, "users" => $users]);
}
public function create(){
$user_var = new UserModel();
$user_var->setLogin($_POST["login"]);
$user_var->setEmail($_POST["email"]);
$user_var->setPassword($this->encrypt_unit->encrypt($_POST["password"]));
$user_var->setIsAdmin($_POST["isAdmin"]);
$user_dao = new UserDAO();
$flag = $user_dao->insertUser($user_var);
if($flag)
$this->index("User created successfully", false);
else
$this->index("Can't created user, please try again", true);
}
public function recover(){
$user_dao = new UserDAO();
$all_users = $user_dao->getUsers();
$users = array();
foreach ($all_users as $value) {
array_push($users, [
"id" => $value->getId(),
"login" => $value->getLogin(),
"email" => $value->getEmail(),
"password" => $this->encrypt_unit->decrypt($value->getPassword()),
"isAdmin" => $value->getIsAdmin()
]);
}
return $users;
}
public function update(){
$user_var = new UserModel();
$user_var->setId($_POST["id"]);
$user_var->setLogin($_POST["login"]);
$user_var->setEmail($_POST["email"]);
$user_var->setPassword($this->encrypt_unit->encrypt($_POST["password"]));
$user_dao = new UserDAO();
$flag = $user_dao->updateUser($user_var);
if($flag)
$this->index("User updated successfully", false);
else
$this->index("Can't updated user, please try again", true);
}
public function update_credential($credential_level){
$user_var = new UserModel();
$user_var->setId($_POST["id"]);
$user_var->setIsAdmin($credential_level);
$user_dao = new UserDAO();
$flag = $user_dao->updateUserCredential($user_var);
if($flag)
$this->index("User updated successfully", false);
else
$this->index("Can't updated user, please try again", true);
}
public function delete(){
$user_var = new UserModel();
$user_var->setId($_POST["id"]);
$user_dao = new UserDAO();
$flag = $user_dao->deleteUser($user_var);
if($flag)
$this->index("User deleted successfully", false);
else
$this->index("Can't deleted user, please try again", true);
}
public function search(){
echo "method search invoked <br />";
}
}
库/引导程序:
<?php
class Bootstrap {
// protected $controller;
// protected $method;
// protected $params;
function __construct() {
//$this->method = 'index';
$this->redirect();
}
public function parseUrl(){
return isset($_GET['url']) ? explode('/',filter_var(rtrim($_GET['url'], '/'), FILTER_SANITIZE_URL)) : null;
}
function redirect(){
$controller;
$method;
$params;
$url = $this->parseUrl();
if(empty($url[0])){
$controller_name = 'Index';
}
else{
$controller_name = ucfirst($url[0]);
}
$filename_controller = 'controllers/' . $controller_name . '.php';
if (file_exists($filename_controller)) {
require_once($filename_controller);
// Do this to use the rest of array to select method, and than parameters
unset($url[0]);
}
else{
$this->error("Controller $controller_name not founded");
return false;
}
$controller = new $controller_name;
//default method
$method = 'index';
if(isset($url[1])){
if (method_exists($controller, $url[1])) {
$method = $url[1];
// Do this to use the rest of array to select parameters
unset($url[1]);
}
else{
$this->error("The controller $controller_name doesn't have any public method called $url[1]");
}
}
//This 'array_values($url)' command is possible because we have unseted the first and second position of this aray before
$params = $url ? array_values($url) : [];
// Securiy comparassion?
var_dump($controller);
var_dump(get_class_methods($controller));
var_dump($controller->getSecurityLevel());
var_dump($controller->{"getSecurityLevel"}());
// if(property_exists($controller, "securiy_level")){
// $authEntity = new Authentication();
// $authEntity->auth($controller->getSecurityLevel());
// }
//(new $url[0])->{$url[1]}($url[2]);
call_user_func_array([$controller, $method], $params);
}
function error($msg="") {
//echo "error invoked: <br /> $msg <br />";
require_once('controllers/Error.php');
$errorHandler = new ErrorController();
$errorHandler->index();
return false;
}
}
/index.php:
<?php
// Use an autoloader!
require_once('libs/Bootstrap.php');
require_once('libs/Controller.php');
require_once('libs/Model.php');
require_once('libs/View.php');
// Library
require_once('libs/Database.php');
require_once('libs/ConnectionDB.php');
require_once('libs/Session.php');
require_once('libs/Authentication.php');
require_once('libs/Encrypter.php');
require_once('config/paths.php');
require_once('config/database.php');
require_once('config/passwords.php');
// DAOS
require_once('daos/UserDAO.php');
require_once('daos/AccountDAO.php');
$app = new Bootstrap();
我的主要问题是在Bootstrap类上,尤其是当我尝试运行时:
var_dump($controller->getSecurityLevel()); //or
var_dump($controller->{"getSecurityLevel"}());
这是我的实际出口,当我尝试访问页面“ http://localhost/index/”时是:
object(Index)#2(2){[“ view”:protected] => NULL [“ security_level”:protected] => int(0)}
array(12){[0] =>字符串(11)“ __construct” [1] =>字符串(5)“ index” [2] =>字符串(5)“管理员” [3] =>字符串(5)“登录” [4] =>字符串(9) “ login_adm” [5] =>字符串(6)“ signin” [6] =>字符串(6)“ logout” [7] => 字符串(14)“ lostMyPassword” [8] =>字符串(7)“详细信息” [9] =>字符串(5) “ model” [10] =>字符串(16)“ getSecurityLevel” [11] =>字符串(4)“ view”}
**注意:未定义的属性:第21行的/var/www/html/libs/Controller.php中的index :: $ securiy_level ** NULL
**注意:未定义的属性:第21行的/var/www/html/libs/Controller.php中的index :: $ securiy_level ** NULL
我无法理解的是PHP如何向我显示$controller
变量具有我要访问的属性,我具有执行访问的方法,但是我无法访问。当PHP显示“ Index :: $ securiy_level”时,这意味着PHP尝试执行的“静态”绑定。
我了解PHP的范围(至少有一点)。 我打算在Controller类中使用变量“ $ security_level”来为所有controllers-child提供默认值。如果程序员不想显式声明$ security_level的其他值,则不必担心,只需使用父亲的财产即可。
我的配置是:PHP 7,Ubuntu 16,apache2;
在高级方面,我将感谢您。
如果我的问题不清楚,也请对其进行评论以澄清任何内容。
*糟糕:当我尝试访问“ http://localhost/admin”时,请注意以下输出:
object(Admin)#2(4){[“” encrypt_unit“:” Admin“:private] => object(Encrypter)#3(2){[“” encrypt_key“:” Encrypter“:private] => 字符串(20)“ RmUzYm1hcUxnY3ZYcA ==” [“ encrypt_algorithm”:“加密器”:专用] =>字符串(11)“ aes-256-cbc”} [“ view”:protected] => NULL [“ security_level”:protected] => int(0) [“ securiy_level”] => int(1)}
array(11){[0] =>字符串(11)“ __construct” [1] =>字符串(5)“ index” [2] =>字符串(6)“创建” [3] =>字符串(7)“恢复” [4] =>字符串(6) “更新” [5] =>字符串(17)“ update_credential” [6] =>字符串(6)“删除” [7] =>字符串(6)“搜索” [8] =>字符串(5)“模型” [9] =>字符串(16) “ getSecurityLevel” [10] =>字符串(4)“ view”} int(1)int(1)
答案 0 :(得分:2)
您在Controller中有错字,return $this->securiy_level;
应该是return $this->security_level;
;)