これらの両方の URL が必要です。
/管理者/ユーザー/追加
と
/管理者/ユーザー/3/編集
edit($user_id = 0)
ユーザーコントローラーの機能を指すようにします。$user_id
2 番目の URL の数字 3 をパラメーターに渡す必要があります。
これをスムーズに行うにはどうすればよいですか?
これらの両方の URL が必要です。
/管理者/ユーザー/追加
と
/管理者/ユーザー/3/編集
edit($user_id = 0)
ユーザーコントローラーの機能を指すようにします。$user_id
2 番目の URL の数字 3 をパラメーターに渡す必要があります。
これをスムーズに行うにはどうすればよいですか?
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";
あなたはこれができますか
public function users ($type, $id = null)
{
if ($type === 'edit')
{
// do edit stuff
}
else
{
// ad add stuff
}
}
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);
}