繁体   English   中英

调用函数会在Codeigniter中给我404页面

[英]Calling a function gives me 404 page in codeigniter

我正在从视图中调用控制器的注销功能。 请注意,我已经完成了项目的模块化结构,当我转到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;
    }
            }

    ?>

在这里我想这样调用这个函数

 <a href="<?php echo site_url()?>admin/logout">Sign Out</a>

编辑我的routes.php是

$route['default_controller'] = "welcome";
$route['admin(/:any)'] = "admin/admin$1";

您必须重定向到页面或必须在尝试从url调用的函数中加载视图页面。 所以尝试这样

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)

网址应类似于http://my-local-project.com/admin/admin/logout

如果您不想这样,则必须设置路线:

$route['admin']        = "admin/admin/index";
$route['admin/(:any)'] = "admin/admin/$1";

或使用CodeIgniter模块化扩展-HMVC:

https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc

只是破坏会话并重定向...

$this->session->sess_destroy();
redirect('controller/method');

在路线上做到这一点:

$route['logout'] = "admin/admin/logout";

$route['admin'] = "admin/admin";

并以<a href="<?php echo base_url()?>logout">Sign Out</a>身份调用注销

这样,您将调用admin模块-> admin类-> logout函数中存在的logout函数。

在注销时像您一样销毁会话,并使用redirect('admin')将您重定向到控制器索引(因为它是在路由中定义的)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM