如何为特定控制器加载钩子 [英] How to load hook for particular controller

查看:189
本文介绍了如何为特定控制器加载钩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的codeigniter。我想为管理面板控制器加载钩子。

  $ hook ['post_controller_constructor'] [] = array(
'class'=>'AdminData'
'function'=>'myfunction',
'filename'=>'loginhelp.php',
'filepath'=>'hooks',
'params '=> array()
);


解决方案

post_controller_constructor 在加载 $ class 后调用钩子。载入的类基于路由参数。



system / core / Codeigniter.php

  / ** 
*< code>
* http://example.com/adminData/method
*< / code>
*
* $ CI = new adminData(); => application / controllers / adminData.php
** /
$ CI = new $ class();

$ EXT-> call_hook('post_controller_constructor');

所以如果你想调用 adminData controller,你可以这样做。



这种方法不是很理想,因为它不是很像OOP,但CI是从设计点



application / controllers / adminData.php b
> class AdminData extends CI_Controller
{
public function __construct(){}

//这不能直接在浏览器中调用
public function _filter()
{
/ **
*将你的逻辑放在这里
*< code>
* $ this-> model-> logic()
*< / code>
** /
exit('我刚刚被调用!');
}
}

application / hooks / loginhelp.php

  class AdminData 
{
protected $ ci;

public function __construct()
{
global $ CI;
$ this-> ci = $ CI;
}

public function myfunction()
{
//如果类不是== AdminData,只是bail
if(get_class - > ci)!='AdminData')return;

if(!is_callable(array($ this-> ci,'_filter')))return;

//调用$ AdminData-> _filter()
return call_user_func(array($ this-> ci,'_filter'));
}
}


I am new in codeigniter. I want to load hooks for admin panel controller.

$hook['post_controller_constructor'][] = array(
    'class'    => 'AdminData',
    'function' => 'myfunction',
    'filename' => 'loginhelp.php',
    'filepath' => 'hooks',
    'params'   => array()
);

解决方案

The post_controller_constructor hook gets called after a $class is loaded. The class that gets loaded is based on the route parameters.

system/core/Codeigniter.php

/**
 *<code>
 *  http://example.com/adminData/method
 *</code>
 *
 * $CI = new adminData(); => application/controllers/adminData.php
**/
$CI = new $class();

$EXT->call_hook('post_controller_constructor'); 

So if you wanted to call a method on the adminData controller, you could do something like this.

This method is not ideal, as its not very OOP like, however the way CI is built from a design point of view, you have to do a few workarounds like the example below

application/controllers/adminData.php

class AdminData extends CI_Controller
{
    public function __construct(){}

    // This cannot be called directly in the browser
    public function _filter()
    {
        /**
         * Put your logic in here
         *<code>
         * $this->model->logic()
         *</code>
        **/
        exit('I have just be called!');
    }
}

application/hooks/loginhelp.php

class AdminData
{
    protected $ci;

    public function __construct()
    {
        global $CI;
        $this->ci = $CI;
    }

    public function myfunction()
    {
        // If the class is not == AdminData, just bail
        if(get_class($this->ci) != 'AdminData') return;

        if(!is_callable(array($this->ci, '_filter'))) return;

        //calls $AdminData->_filter()
        return call_user_func(array($this->ci, '_filter'));
    }
}

这篇关于如何为特定控制器加载钩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆