我如何创建一个codeigniter 3控制器的功能像一个普通的php类



我想知道我如何做一个像这样的控制器,以便从视图中调用它们,我想用控制器而不是库来做。如果可能的话,请帮忙。

<?php
require_once('database.php');
require_once('employees.php');
require_once('photograph.php');
class employees {

protected static $table_name = "employees"; 
protected static $db_fields = array('idemployee', 'employeenumber', 'name', 'lastname', 'sex', 'phone', 'email');

public $idemployee;
public $employeenumber; 
public $name; 
public $lastname;
public $sex; 
public $phone;
public $email;


public function full_name() {
if(isset($this->name) && isset($this->lastname)) {
return $this->name . " " . $this->lastname;
} else {
return "";
}
}

public function employee_detail_card() {
$foto = $this->photo();
return 
"<div class='card' style='width:100%;'>
<img class='card-img-top' src='../../public/images_up/$foto' alt='Card image' width='50%'>
<div class='card-header'>"
. $this->botonesFotoPerfil() .
"</div>
<div class='card-header card bg-info text-white'>
Numero ibo: " . $this->employeenumber . "
</div>
<div class='card-body'>
<table class='table table-striped'> 
<tr>
<tr>
<td><strong>Telefono: </strong>" . $this->phone . "</td>
</tr>
<tr>
<td><strong>E-mail: </strong>" . $this->email . "</td>
</tr>
</table>
</div>";
}

public function list_employees() {
return "<tr>" . 
"<td>" . $this->employeenumber . "</td>" .
"<td>" . $this->full_name() . "</td>" .
"<td align='right'>" . $this->phone . "</td> " .
"<td align='right'>" . $this->email . "</td> ";
}
}
?>

我确实有一个结果,但与一个库,但老实说,我明白这是更好的做法,在控制器上做它。

我建议使用加载器类,首先创建自定义类,将其放在application/library下,然后使用加载器类加载它$this->load->library('yourclassname');在纪录片中非常详细,你可以在这里找到

https://codeigniter.com/userguide3/general/creating_libraries.html

但是如果你出于某种原因想要避免它,没有什么可以阻止你做一个简单的include/require

最新更新