Home  >  Article  >  Backend Development  >  How to implement PHP's universal controller

How to implement PHP's universal controller

php中世界最好的语言
php中世界最好的语言Original
2017-12-20 16:51:432947browse

This article teaches you how to use PHP to implement a universal controller. Friends who are interested can communicate with each other. The following code can be used directly

<?php
namespace 目录\Controller;
class TypeController extends Controller
{
  public function add()
  {
    if(IS_POST)
    {
      $model = D(&#39;Type&#39;);
      if($model->create())
      {
        if($model->add())
        {
          $this->success(&#39;添加成功!&#39;, U(&#39;lst&#39;));
          exit;
        }
        else
        {
          $sql = $model->getLastSql();
          $this->error(&#39;插入数据库失败!.<hr />SQL:&#39;.$sql);
        }
      }
      else
      {
        $error = $model->getError();
        $this->error($error);
      }
    }
    $this->display();
  }
  public function lst()
  {
    $model = D(&#39;Type&#39;);
    $data = $model->search();
    $this->assign($data);
    $this->display();
  }
  public function save($id)
  {
    $model = D(&#39;Type&#39;);
    if(IS_POST)
    {
      if($model->create())
      {
        if($model->save() !== FALSE)
        {
          $this->success(&#39;修改成功!&#39;, U(&#39;lst&#39;));
          exit;
        }
        else
        {
          $sql = $model->getLastSql();
          $this->error(&#39;修改数据库失败!.<hr />SQL:&#39;.$sql);
        }
      }
      else
      {
        $error = $model->getError();
        $this->error($error);
      }
    }
    $data = $model->find($id);
    $this->assign(&#39;data&#39;, $data);
    $this->display();
  }
  public function del($id)
  {
    $model = D(&#39;Type&#39;);
    $model->delete($id);
    $this->success(&#39;操作成功!&#39;, U(&#39;lst&#39;));
  }
  public function bdel()
  {
    $delid = I(&#39;post.delid&#39;);
    if($delid)
    {
      $delid = implode(&#39;,&#39;, $delid);
      $model = D(&#39;Type&#39;);
      $model->delete($delid);
    }
    else
      $this->error(&#39;请选择要删除的记录!&#39;);
    $this->success(&#39;操作成功!&#39;, U(&#39;lst&#39;));
  }
}


I believe after reading this You have mastered the case method. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Related reading:

Summary of usage of PHP header

ThinkPHP How to send emails through corporate mailboxes through PHPMailer

How to use try catch in laravel5?

The above is the detailed content of How to implement PHP's universal controller. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn