Есть проблема подключения модели в контроллерe.Я создал основной метод для подключения файлов моделеи который вызывается при необходимости.
Проблмa находится в методе actionServiceinfo()
в который подключается requireModel()
.Помогите с проблемой .Заранне спасибо
Вот Код
index.php
error_reporting(E_ALL);
ini_set("display_errors", 1);
require_once('Core/Bootstrap.php');
require_once('Core/Controller.php');
require_once('Core/Model.php');
require_once('Core/View.php');
require_once('Core/Database.php');
require_once('config/Database.php');
require_once('config/Secure.php');
$bootstrap = new Bootstrap();
Core/Bootstrap.php
private $controller = NULL;
private $action = NULL;
private $params = array();
private function parseUrl(){
if (isset($_GET['url'])) {
$url = Secure::secureUrl($_GET['url']);
return $url;
}else{
require_once('controller/HomeController.php');
$object = new HomeController;
$object->actionIndex();
return false;
}
}
public function __construct(){
$explode = explode('/', $this->parseUrl());
if (isset($explode[0])) {
if (file_exists("controller/".ucfirst($explode[0])."Controller.php")) {
$this->controller = ucfirst($explode[0])."Controller";
require_once("controller/".$this->controller.".php");
}else{
require_once('controller/ErrorController.php');
$object = new ErrorController();
$object->actionIndex();
return false;
}
}
$object = new $this->controller;
if (isset($explode[2])) {
if (method_exists($object, "action".ucfirst($explode[1]))) {
$this->action = "action".ucfirst($explode[1]);
$this->param = $explode[2];
call_user_func([$object,$this->action], $this->param);
}else{
require_once('controller/ErrorController.php');
$object = new ErrorController();
$object->actionIndex();
return false;
}
}else{
if (isset($explode[1])) {
if (method_exists($object, "action".ucfirst($explode[1]))) {
$this->action = "action".ucfirst($explode[1]);
call_user_func([$object,$this->action]);
}else{
require_once('controller/ErrorController.php');
$object = new ErrorController();
$object->actionIndex();
return false;
}
}else{
$object->actionIndex();
}
}
}
}
Core/Controller.php
class Controller{
public function __construct(){
$this->view = new View();
}
public function requireModel($name){
$pathe = "model/" . $name . "Model.php";
if (file_exists($pathe)) {
require_once($pathe);
$this->model = new $name."Model";
}
}
}
Core/Model.php
class Model{
public function __construct(){
$this->db = new Database();
}
}
Core/Database.php
class Database extends PDO {
public function __construct(){
parent::__construct(DB_TYPE . ":host=" . DB_HOST . ";dbname=" . DB_NAME,DB_USER, DB_PASS);
}
}
Controller/ServiceController.php
class ServiceController extends Controller{
public function __construct(){
parent::__construct();
}
public function actionIndex(){
$this->view->render('Service/index');
}
public function actionServiceinfo($id = NULL){
$this->requireModel('Service');
$serviceModel = new ServiceModel();
$serviceInfo = $serviceModel->userInfo($id);
$this->view->render('Service/info',$serviceInfo);
print_r($serviceInfo);
}
}
Model/ServiceModel.php
class ServieModel extends Model{
public function __construct(){
parent::__construct();
}
public function userInfo($id){
$studentPrepare = $this->db->prepare("SELECT * FROM `services` WHERE service_id = :service_id");
$studentPrepare->execute([':service_id' => $id]);
return $studentPrepare->fetch();
}
}
Есть проблема подключения модели в контроллерe.
.. и она заключается в том, что модели не должны подключаться в контроллерах :)