Codeigniter:如何在不加载库的情况下检查库方法是否存在?

发布于 2024-10-09 22:06:12 字数 1038 浏览 7 评论 0原文

我想将控制器方法中的某些功能分离到库中,以使它们模块化。根据 URI,我尝试路由到库方法(如果存在)。但我必须加载库来检查 php method_exists 函数的方法。我发现检查它的唯一其他方法是将所有库方法放入配置数组中并检查那里的方法名称。例如:

<?php
/**
 * Application_config.php
 */

$config['extensions'] = array(
 'News' => array(
  'library' => 'articles_library',
  'methods' => array(
   'articles',
   'article',
   'edit_article',
   'add_article',
   'delete_article'
  )
 )
);

是否

<?php
/**
 * admin_controller.php
 *
 * all admin routes go to index
 */
class C3_base_controller extends Controller {
 public function index() {
  $lib_chk = 0;
  $ext = $this->config->item('extensions');
  foreach($ext as $item) {
   foreach ($item['methods'] as $meth) {
    if ($this->uri->segment(2) == $meth) {
     $lib = $item['library'];
     echo $this->$lib->$meth();
     $lib_chk = 1;
    }
   }
  }
  if ($lib_chk == 0) {
   // rest of controller...
  }
 }
}

有更好的方法来检查每个库中是否存在库方法而不加载库?必须复制应用程序配置中的每个方法只是要求它。

I want to separate certain functions from controller methods into libraries to make them modular. Based on the URI I'm trying to route to a library method if it exists. But I'd have to load the library to check for the method with the php method_exists function. The only other way I've found to check for it is to put all the library methods in a config array and check for the method name there. Such as:

<?php
/**
 * Application_config.php
 */

$config['extensions'] = array(
 'News' => array(
  'library' => 'articles_library',
  'methods' => array(
   'articles',
   'article',
   'edit_article',
   'add_article',
   'delete_article'
  )
 )
);

and

<?php
/**
 * admin_controller.php
 *
 * all admin routes go to index
 */
class C3_base_controller extends Controller {
 public function index() {
  $lib_chk = 0;
  $ext = $this->config->item('extensions');
  foreach($ext as $item) {
   foreach ($item['methods'] as $meth) {
    if ($this->uri->segment(2) == $meth) {
     $lib = $item['library'];
     echo $this->$lib->$meth();
     $lib_chk = 1;
    }
   }
  }
  if ($lib_chk == 0) {
   // rest of controller...
  }
 }
}

Is there a better way to check for the existence of a library method in each library without loading the libraries? Having to duplicate every method in the application config is just asking for it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

罗罗贝儿 2024-10-16 22:06:12

您可以使用

method_exists

例如,如果您正在检查数据库对象是否存在

if(method_exists($this->CI->db, 'set')){
// code ...
}

You can use

method_exists

For example, if you are checking for the database object if it's exists

if(method_exists($this->CI->db, 'set')){
// code ...
}
初见 2024-10-16 22:06:12

您是否正在追求可重用代码的模块化,或者您是否正在创建一个具有可能存在或不存在的功能的应用程序……如插件中那样?

我很难想象您的基础应用程序具有硬编码的 url,导致可能不存在的库。您可以测试库而不是方法吗?检查目录并将文件名加载到数组中,然后查看 url 或 uri 所需的库是否在该数组中可能更简单。

但是,如果您需要坚持现有方法名称的数组,您可以通过在“函数(空格)”之后出现并后跟括号来对方法名称进行正则表达式吗?

Are you pursuing modularity for reusable code or are you creating an application that has features that may or may not be present...as in plugins?

It's hard for me to imagine your base application having hard-coaded url's that lead to a library that may not exist. Can you test for the library rather than the method? That may be simpler to check a directory and load file names into an array, then see if the library required by a url or uri is in that array.

But if you need to stick to your array of existing method names, could you just regex for the method names by that occur after "function (space)" and followed by parenthesis?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文