我在我的应用程序的Codeigniter中创建外部类层次结构。 如下所示。
这些类位于libraries文件夹中。 整个项目文件结构如下所示。
Codeigniter文件夹结构
Project
|
+-- controllers
| |
| +-- App.php
|
+-- models
| |
| +-- Student_model.php
|
+-- libraries
| |
| +-- Person.php
| +-- Car.php
| +-- Student.php
|
在这里
使用的模型 - 数据处理
用于应用程序导航逻辑的控制器(连接模型和视图)。
用于表示数据的视图(演示文稿)
将所有业务逻辑放入外部类层次结构
代码如下所示
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class App extends CI_Controller
{
public function __construct()
{
parent::__construct();
//loading model
$this->load->model('Student_model');
//load custom class
$this->load->library('Student');
}
public function studying()
{
$Student = new Student();
//processing done inside student class
$response = $Student->processStudying();
echo $response;
//send data to view later
}
//process done in external class
public function showdata()
{
//get data from model
$result = $this->Student_model->get_student_data();
$Student2 = new Student();
//processing done inside student class method
$data = $Student2->studentDataProcessing($result);
echo '<pre>',print_r($data,true),'</pre>';
//send data to view
//$this->load->view('student_view', $data);
}
public function writing()
{
$Student3 = new Student();
//processing done inside student class method(inheri from person)
$response = $Student3->writingProcess();
echo $response;
//send data to view later
}
//use CI libary inside a class
public function displaytable()
{
$this->load->library('table');
$Student4 = new Student();
//processing done inside student class method
$tableData= $Student4->CI_GenerateTable($this->table);
echo $tableData;
//send data to view later
}
}
?>
<?php
//using custom libary
require_once('Person.php');
require_once('Car.php');
class Student Extends Person
{
public function processStudying()
{
return 'process Studying';
}
//'create another nobject inside method
public function studentDataProcessing($resultData)
{
$names;
foreach($resultData as $Data)
{
$names[] = $Data->fname;
}
$car = new Car();
$driveText = $car->drive($names);
return $driveText;
}
public function CI_GenerateTable($r)
{
$data = array(
array('Name', 'Color', 'Size'),
array('Fred', 'Blue', 'Small'),
array('Mary', 'Red', 'Large'),
array('John', 'Green', 'Medium')
);
return $r->generate($data);
}
}
class Car
{
public function drive($studentNames)
{
$tempArr;
foreach($studentNames as $element)
{
$tempArr[$element] = $element;
}
return $tempArr;
}
}
class Person
{
public function writingProcess()
{
return 'writing';
}
}
<?php
class Student_model extends CI_Model
{
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function get_student_data()
{
$this->db->select('fname');
$this->db->from('student');
$query = $this->db->get();
$result = $query->result();
return $result;
}
}
我的问题是
根据MVC设计模式,这个架构是否正确?
我的意思是在外部类中完成的所有处理都是正确的,而不是在控制器或模型中完成吗?
答案 0 :(得分:0)
是的但是我们使用了不同的模式设计我使用了类似的
Project
|__ Controllers
| |__ controllers.php
|__ Models
| |__ classes.php
|__ Dao
| |__ sql.php // sql statements
|__ Views
| |__ views.html