Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对两个'使用相同的函数;添加';和';编辑';在Codeigniter中?_Codeigniter_Codeigniter Url_Codeigniter Routing - Fatal编程技术网

对两个'使用相同的函数;添加';和';编辑';在Codeigniter中?

对两个'使用相同的函数;添加';和';编辑';在Codeigniter中?,codeigniter,codeigniter-url,codeigniter-routing,Codeigniter,Codeigniter Url,Codeigniter Routing,我想要这两个URL: /admin/users/add 及 /admin/users/3/edit 指向“我的用户”控制器中的edit($user\u id=0)功能。第二个url中的数字3必须传递给$user\u id参数 我怎样才能顺利地完成这项工作?你能做到吗 public function users ($type, $id = null) { if ($type === 'edit') { // do edit stuff }

我想要这两个URL:

/admin/users/add

/admin/users/3/edit

指向“我的用户”控制器中的
edit($user\u id=0)
功能。第二个url中的数字3必须传递给
$user\u id
参数

我怎样才能顺利地完成这项工作?

你能做到吗

public function users ($type, $id = null)
{
     if ($type === 'edit')
     {
           // do edit stuff
     }
     else
     {
           // ad add stuff
     }
 }
在application/config/routes.php中:

$route['admin/users/add'] = "users/edit";
$route['admin/users/(:num)/edit'] = "users/edit/$1";
如果您希望此操作也适用于其他控制器,可以执行以下操作:

$route['admin/(:any)/add'] = "$1/edit";
$route['admin/(:any)/(:num)/edit'] = "$1/edit/$2";
或者同样,使用正则表达式:

$route['admin/([a-z]+)/add'] = "$1/edit";
$route['admin/([a-z]+)/(\d+)/edit'] = "$1/edit/$2";

作为分离逻辑的替代方案

我通常有两个控制器,它们都对同一个视图说话

admin/user/add
admin/user/edit/3
两者都指向这一观点

admin/user_form.php
然后在发布表单时访问save_user()方法


但正如米沙所说,通过设置路由,你几乎可以将任何url指向任何方法。

Sulotion:

function _remap($method)
{
$param_offset = 2;

// No method, point to...
if (!method_exists($this, $method))
{
    if (is_numeric($method) || $method == 'add')
    {
        // Show single
        $param_offset = 1;
        $method = 'show';
    }
    else
    {
        // Index
        $param_offset = 1;
        $method = 'index';
    }
}

// Since all we get is $method, load up everything else in the URI
$params = array_slice($this->uri->rsegment_array(), $param_offset);

// Call the determined method with all params
call_user_func_array(array($this, $method), $params);
}

你想更新数据库吗?你想要一个通用的功能吗?是的,但我不喜欢你必须为每个控制器设置新的路由。如果您可以在控制器内设置路由,我会使用它。@PeterWesterlund,您不必为每个控制器设置路由。使用正则表达式,可以设置适用于所有控制器的单个路由。此外,您在问题中没有提到该解决方案也适用于其他控制器。如果这很重要,下次在你的问题中提出来。@Misha:我不喜欢CI的路线。似乎您必须为所有控制器类创建新路由。如果我使用路由,它应该适用于所有类。否则,我将直接在类中进行路由设置。您不必为所有控制器设置路由。您可以使用正则表达式将路由动态映射到所需的控制器。这样,您可以对具有相同模式的所有控制器使用一条路由。至少在我看来,两行代码比您上面所做的更“流畅”。请在您的问题中提到,您希望这不仅适用于用户,也适用于其他控制器。这是重要的信息,人们在回答时可以将其考虑在内。我花时间回答了你的问题,但你甚至没有考虑我的答案,因为“你不喜欢CI路由”,而且你不知道一条路由可以用于多个控制器。如果你把这些重要的信息包括进来,我会告诉你如何处理路由。@Misha:首先,我很抱歉有任何误解!我是CodeIgniter的新手,我可能因为语言的原因而误解了您答案中的某些内容(我不是英语国家的人)。当我说我不喜欢这些路线时,我的真正意思是它是一个单独的文件。如果我能直接从类中确定路线,那绝对是最好的。再一次,很抱歉我没有解释这一点。那么您将为所有控制器添加一个
\u remap
函数?你不认为把所有的路线都放在一个中心位置比把不清楚的代码分散在你的控制器上要好吗?为了充分利用框架,您应该学习如何使用它(通过使用路由)。至少我是这么认为的。祝你好运