SoFunction
Updated on 2025-03-10

ThinkPHP general controller implementation method example

This article describes the implementation method of thinkPHP general controller. Share it for your reference, as follows:

<?php
namespace Table of contents\Controller;
class TypeController extends Controller
{
  public function add()
  {
    if(IS_POST)
    {
      $model = D('Type');
      if($model->create())
      {
        if($model->add())
        {
          $this->success('Added successfully!  ', U('lst'));
          exit;
        }
        else
        {
          $sql = $model->getLastSql();
          $this-&gt;error('Insert the database failed!  .<hr />SQL:'.$sql);
        }
      }
      else
      {
        $error = $model-&gt;getError();
        $this-&gt;error($error);
      }
    }
    $this-&gt;display();
  }
  public function lst()
  {
    $model = D('Type');
    $data = $model-&gt;search();
    $this-&gt;assign($data);
    $this-&gt;display();
  }
  public function save($id)
  {
    $model = D('Type');
    if(IS_POST)
    {
      if($model-&gt;create())
      {
        if($model-&gt;save() !== FALSE)
        {
          $this-&gt;success('The modification was successful!  ', U('lst'));
          exit;
        }
        else
        {
          $sql = $model-&gt;getLastSql();
          $this-&gt;error('Failed to modify the database!  .<hr />SQL:'.$sql);
        }
      }
      else
      {
        $error = $model-&gt;getError();
        $this-&gt;error($error);
      }
    }
    $data = $model-&gt;find($id);
    $this-&gt;assign('data', $data);
    $this-&gt;display();
  }
  public function del($id)
  {
    $model = D('Type');
    $model-&gt;delete($id);
    $this-&gt;success('The operation was successful!  ', U('lst'));
  }
  public function bdel()
  {
    $delid = I('');
    if($delid)
    {
      $delid = implode(',', $delid);
      $model = D('Type');
      $model-&gt;delete($delid);
    }
    else
      $this-&gt;error('Please select the record you want to delete!');
    $this-&gt;success('The operation was successful!  ', U('lst'));
  }
}

For more information about thinkPHP related content, please check out the topic of this site:ThinkPHP Introduction Tutorial》、《Summary of the operation skills of thinkPHP templates》、《Summary of common methods of ThinkPHP》、《Codeigniter Introductory Tutorial》、《Advanced tutorial on CI (CodeIgniter) framework》、《Zend FrameWork Framework Introduction Tutorial"and"PHP template technical summary》。

I hope that the description in this article will be helpful to everyone's PHP programming based on the ThinkPHP framework.