The thinkPHP framework implements a simple example of an java like filter

  • 2021-11-02 00:10:42
  • OfStack

This article illustrates a simple way to implement java-like filters in the thinkPHP framework. Share it for your reference, as follows:

When writing java web code, you can define filters to filter the controller, and you can realize permission verification and so on

Similar requirements can be implemented in thinkphp by inheriting the methods of the parent class

Parent class code


<?php
/**
 * Created by PhpStorm.
 * User: xieyicheng
 * Date: 2014/12/11
 * Time: 14:43
 */
namespace Admin\Controller;
use Think\Controller;
class CommonController extends Controller
{
  public function _initialize()
  {
    if(!isset($_SESSION['username'])) {
      redirect( U('Admin/Login/index'), 0);// No login, jump to the login page 
    }
  }
}

Subclass code


<?php
/**
 * Created by PhpStorm.
 * User: xieyicheng
 * Date: 2014/12/4
 * Time: 13:06
 */
namespace Admin\Controller;
use Home\Model;
use Admin\Model\Shop;
class FileController extends CommonController
{
  public function index()
  {
    $this->display();
  }
}

In this way, the class will perform the operation of verifying login before calling the controller's method

Readers who are interested in thinkPHP can check the topics of this site: "ThinkPHP Introduction Tutorial", "thinkPHP Template Operation Skills Summary", "ThinkPHP Common Methods Summary", "codeigniter Introduction Tutorial", "CI (CodeIgniter) Framework Advanced Tutorial", "Zend FrameWork Framework Introduction Tutorial" and "PHP Template Technology Summary".

I hope this article is helpful to the PHP programming based on ThinkPHP framework.


Related articles: