gpt4 book ai didi

php - Codeigniter,是否可以有一个可变的公共(public)函数名称?

转载 作者:行者123 更新时间:2023-11-29 21:12:33 25 4
gpt4 key购买 nike

我将继续在 CodeIgniter 框架中开发我的网站,我对我所做的选择感到非常高兴,因为它非常简单并且代码很干净。下面我将解释我当前的问题。

在我写的一个 Controller 中:

public function abcd() {
//CONTENT of the controller public function
}

是否可以动态更改“abcd()”名称,例如从 mySQL 获取值?为了更清楚起见,我想使用一个语句,允许我始终使用 Controller 的相同内容,但用于不同的公共(public)功能。

最佳答案

您可以在config/routes.php中使用ROUTE更改 Controller 函数名称

$route['dynamic_name'] = "controller_name/abcd";

无论你在哪里更改路由名称,url 中的 Controller 都会更改

之前

http://domain.name/index.php/welcome/abcd

之后

http://domain.name/index.php/welcome/dynamic_name

还可以为same_url添加多个路由

如果您想从数据库获取值,请使用 config/routes.php 中的以下代码

require_once( BASEPATH .'database/DB'. EXT );
$db =& DB();
$query = $db->get('menus');
$names = $query->result();

foreach ($names as $key => $name) {
$route[$name] = "controller_name/abcd";
}

关于php - Codeigniter,是否可以有一个可变的公共(public)函数名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36244968/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com