1
我想知道是否可以在未將new_admin begin添加到鏈接的情況下訪問我的'admin'控制器內的'new_admin'功能。Codeigniter - 在同一控制器上的索引函數中加載控制器功能
我的控制器:
<?php
class Admin extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('session');
$this->load->model('Admin_model', '', TRUE);
$this->load->view('admin/admin_nav');
}
public function index()
{
if($this->session->userdata('logged_in'))
{
$this->load->view('admin/admin_dashboard');
}
else
{
redirect('login');
}
}
public function new_admin()
{
$data = [];
if($this->input->server('REQUEST_METHOD') == 'POST')
{
$email = $this->input->post('email');
$this->form_validation->set_rules('email', 'E-mail', 'required|valid_email');
if($this->form_validation->run())
{
$result = $this->Admin_model->check_if_admin($email);
if(!$result)
{
$new_admin = new Admin_model();
$new_admin->email = $email;
$new_admin->password = password_hash('admin', PASSWORD_DEFAULT, ['cost' => 12]);
$this->Admin_model->add_new_admin($new_admin);
$data['success'] = 'success';
}
else
{
$data["error_email"] = 'error';
}
}
}
$this->load->view("admin/admin_dashboard", $data);
}
}
我的觀點:
<div>
<div>
<h1>Dashboard</h1>
</div>
<div>
<div>Add a new admin</div>
<div>
<form action="<?php echo site_url('admin/new_admin') ?>" method="post">
<label for="email">Email new admin</label>
<input type="text" name="email" id="email" value="<?php echo set_value('email'); ?>">
<?php echo form_error('email', '<div>', '</div>'); ?>
<?php if (isset($succes)): ?>
<p><?php echo $succes; ?></p>
<?php endif ?>
<?php if (isset($error_email)): ?>
<p><?php echo $error_email; ?></p>
<?php endif ?>
<button type="submit">Add new admin</button>
</form>
</div>
</div>
</div>
這段代碼的作品,但我想它在沒有URL變化工作。 這可能嗎?
現在,我第一次訪問的網址是:'example.com/admin'
然後在表單提交後,它變成了'example.com/admin/new_admin'。
在此先感謝。