【问题标题】:Calling a function gives me 404 page in codeigniter在codeigniter中调用一个函数给了我404页
【发布时间】:2014-05-23 04:25:21
【问题描述】:

我正在从视图中调用我的控制器的注销功能。 提醒一下,我已经制作了项目的模块化结构,当我转到 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";

【问题讨论】:

  • 你的项目中有htaccess吗??
  • 是的,我有。我也应该分享一下吗?
  • 你删除 index.php 吗??
  • 是的,我正在删除其中的 index.php
  • 这是因为没有对应logout的view页面。

标签: php codeigniter


【解决方案1】:

您的应用目录有 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

【讨论】:

  • 它有效,但现在当我转到http://my-local-project.com/admin 时,它会将我带出管理员并显示欢迎页面。如何解决这个问题?
  • @baig772 试试这个$route['admin(/:any)'] = "admin/admin$1";
  • 但是如果我输入像myproject.com/admin/index 这样的手动网址,它工作正常
【解决方案2】:

您必须重定向到页面或必须在您尝试从 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
}

【讨论】:

    【解决方案3】:

    只需销毁会话并重定向...

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

    【讨论】:

    • 请在 Bora 的回答中查看我的 cmets
    【解决方案4】:

    在路线中实现:

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

    并将注销调用为&lt;a href="&lt;?php echo base_url()?&gt;logout"&gt;Sign Out&lt;/a&gt;

    通过这种方式,您将调用管理模块中存在的注销功能-->管理类-->注销功能

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-24
      • 2014-07-23
      • 2019-04-04
      • 2011-05-02
      • 2022-07-21
      相关资源
      最近更新 更多