Php 调用函数会在codeigniter中显示404页
我正在从视图调用控制器的注销函数。 为了提醒大家,我已经制定了项目的模块化结构,当我进入Php 调用函数会在codeigniter中显示404页,php,codeigniter,Php,Codeigniter,我正在从视图调用控制器的注销函数。 为了提醒大家,我已经制定了项目的模块化结构,当我进入http://my-local-project.com/admin,它加载我的管理员控制器的索引功能。但是当我进入http://my-local-project.com/admin/logout,显示404页 我的目录结构是 应用/ 控制器/ 管理员/ admin.php 控制器: <?php /* * To change this template, cho
http://my-local-project.com/admin
,它加载我的管理员控制器的索引功能。但是当我进入http://my-local-project.com/admin/logout
,显示404页
我的目录结构是
- 应用/
- 控制器/
- 管理员/
- admin.php
- 管理员/
- 控制器/
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of TestController
*
* @author Ibm
*/
class Admin extends CI_Controller {
function __construct() {
parent::__construct(); //call to parent constructor
$this->data = "";
$this->header = $this->load->view('admin/header', $this->data, TRUE);
$this->template = $this->load->view('admin/template', $this->data, TRUE);
$this->footer = $this->load->view('admin/footer', $this->data, TRUE);
$this->load->helper('url');
// $this->loginModel = $this->load->model('admin/loginModel');
session_start();
}
public function index() {
echo "all is well";
}
public function logout() {
$userSessionData = array(
'user_id' => '',
'username' => '',
'email' => ''
);
$this->session->unset_userdata($userSessionData);
$this->session->sess_destroy();
session_destroy();
redirect(base_url('admin/login'));
exit;
}
}
?>
您必须重定向到一个页面,或者在试图从url调用的函数中加载一个视图页面。所以试着这样做
<a href="<?php echo site_url()?>admin/logout">Sign Out</a>
public function logout()
{
$userSessionData = array(
'user_id' => '',
'username' => '',
'email' => ''
);
$this->session->unset_userdata($userSessionData);// unset your sessions
$this->session->sess_destroy();
redirect('admin/index'); // redirect to admin index page
}
你的应用程序目录有2个
admin
段。1.文件夹(/admin/),2。文件(admin.php)
URL应该类似于http://my-local-project.com/admin/admin/logout
如果你不想这样,你必须设置路线:
$route['admin'] = "admin/admin/index";
$route['admin/(:any)'] = "admin/admin/$1";
或使用CodeIgniter模块扩展-HMVC:
只需销毁会话并重定向
$this->session->sess_destroy();
redirect('controller/method');
在我看来:
$route['logout'] = "admin/admin/logout";
$route['admin'] = "admin/admin";
并将注销呼叫为
这样,您将调用管理模块-->管理类-->注销函数中的注销函数
在注销时,像你那样销毁会话,并使用重定向(“admin”)将你重定向到控制器索引(因为它是在路由中定义的)你的项目中有htaccess吗?是的,我有。我也应该分享吗?R u removind index.php在其中?是的,我正在删除index.php在其中这是因为没有任何与注销对应的视图页面。它工作了,但现在当我转到
http://my-local-project.com/admin
它将我带出管理员界面,并显示欢迎页面。如何解决这个问题?@baig772尝试一下这个$route['admin(/:any)]=“admin/admin$1”
但是如果我键入手动URL,比如myproject.com/admin/index
它工作正常,请查看我对Bora答案的评论
$route['logout'] = "admin/admin/logout";
$route['admin'] = "admin/admin";