如何将函数从PHP过程转换为PHP MVC


How to convert a function from PHP Procedural to PHP MVC?

我的View Page上有这个函数,它截断一些数据以显示在我的表中。

function truncate($mytext) {  
    //Number of characters to show  
    $chars = 100;  
    $mytext = substr($mytext,0,$chars);  
    $mytext = substr($mytext,0,strrpos($mytext,' '));    
    return $mytext;  
    }  

我为动态文本设置了一个局部变量:

$mydescription = $value['PROBLEM_DESCRIPTION']

在同一页,我有这个:

echo '<td><p>' .truncate($mydescription). '; ?></p><</td>

它工作完美,所以我的问题是如何在MVC架构上应用这个使用Codeigniter?如果有人有一个想法让我知道,谢谢!!

您至少应该将这个函数定义为您的模型的一个方法(您从这里获得这个描述)。

class TextModel extends CI_Model {
   ...
   public function getShortDescription(){
       $chars = 100;  
       $mytext = substr($this->mytext,0,$chars);  
       $mytext = substr($mytext,0,strrpos($mytext,' '));    
       return $mytext;
   }
}

在你的控制器中沿着以下行:

class Blog extends Controller {
    function index()
    {
            $this->load->model('TextModel');
        $data['short_description'] = $this->TextModel->getShortDescription();
        $this->load->view('blogview', $data);
    }
}

最后,在你看来:

echo '<td><p>'.$short_description.'</p></td>';

我不熟悉CodeIgniter,但我猜任何数据操作应该在模型中完成,因此你将保持你的控制器'瘦',不会违反MVC范式。

既然你已经有一个工作的代码,所有你需要决定的是在MVC模式的CI应该转移到哪里。我建议把它放在帮手里面。CI中的helper顾名思义就是一个简单的php文件,其中包含一系列帮助您完成某些任务的函数。假设您将此文件放入名为'utility.php'的文件中,然后可以在需要时使用

包含此文件。
$this->load->helper('utility');

,然后调用truncate函数

现在,如果你有一堆相关的任务,你想要在一个基于类的结构中整齐地组织,你可以创建你自己的CI库。假设您创建了一个名为'truncate'的库,那么与帮助程序类似,这些库被加载为

$this->load->library('truncate');
就我个人而言,对于一堆实用程序,我会把它们都放在一个助手中,而不是扩展核心CI类或创建一个自定义库。

可以在控制器中设置:

class Something extends CI_Controller{
    public function index(){
        $data['mytext'] = $this->_truncate('some text to truncate');
        //truncated text will be available in the view as $mytext
        $this->load->view('some_view', $data);
    }
    private function _truncate($text = NULL){
        if($text){
            $chars = 100;  
            $mytext = substr($text,0,$chars);  
            $mytext = substr($text,0,strrpos($text,' ')); 
            return $mytext;
        }  
    }
}

编辑:

你正在调用数据库的东西在你的视图,这完全不是Codeigniter MVC。

在MVC中可能是这样的:

h2控制器

class Something extends CI_Controller{
    public function index(){
        $test_text = $this->my_model->get_text();
        $data['test_text'] = $this->_truncate($test_text);
        //truncated text will be available in the view as $test_text
        $this->load->view('some_view', $data);
    }
    private function _truncate($text = NULL){
        if($text){
            $chars = 100;  
            $mytext = substr($mytext,0,$chars);  
            $mytext = substr($mytext,0,strrpos($mytext,' ')); 
            return $mytext;
        }  
    }
}

class My_Model extends CI_Model{
    public function get_text(){
        //$this->db->get_where....or other db functions
        return "testing text... this is only a test";
    }
}
<<h2>视图/h2>
<html>
<body>
<b><?php echo $test_text; ?></b>
</body>

当前的答案解释说,您可以将方法添加到模型和控制器中。从功能上讲,这没有问题,正如您所说,无论您将其放置在何处,该函数都可以完美地工作。

当你需要在另一个控制器中调用相同的方法时会发生什么?或者换一种不同的模式?

很有可能你会得到一些重复的代码。

作为一种可能的解决方案,您可以创建一个允许您保留所有字符串的类库操纵在一个地方。只需在你的类中添加更多的方法…

// example/libary/path/TextFormatter.php
class TextFormatter
{
  protected $_text = '';
  public function __construct($text)
  {
    $this->_text = $text;
    return $this;
  }
  public function truncate($maxChars = 100)
  {
    $truncated = substr($this->_text, 0, $maxChars);  
    $truncated = substr($truncated, 0, strrpos($truncated,' '));    
    return $truncated;
  }
  public function stripHtml()
  {
    ///.....
  }
  public function doSomethingElseWithText()
  {
  }
}

然后在控制器中…

// Controller.php
class SomeController extends Controller
{
  public function index()
  {
    $formatter = new TextFormatter('Example text string');  
    $data['formattedText'] = $formatter->truncate();
  }

}

唯一的警告是TextFormatter类需要自动加载/可从任何地方使用它。

控制器:创建一个控制器truncate.php.

<?php
 Class Truncate extends CI_Controller {
 function __construct() {
  parent::__construct();
 }
 function truncate_text($mytext) {  
     //Number of characters to show
     $chars = 100;
     if(empty($mytext))
         echo '$mytext is empty.';
     $mytext = substr($mytext,0,$chars);  
     $data['mytext'] = substr($mytext,0,strrpos($mytext,' '));    
     $this->load->view('truncate_view',$data);
 }


视图:创建一个视图文件truncate_view.php

<?php
    echo '<td><p>'.$mytext.'</p></td>';
   //Write your other view code if any are present.
?>