CodeIgniter HMVC Arhitecture 404 Error


CodeIgniter HMVC Arhitecture 404 Error

我在这里用以下 HMVC 插件扩展了 CodeIgniter 2.1.3,我遵循它到最后,让它在基本示例中没有问题地工作。但是现在我想使用 BitAuth 身份验证库扩展我的应用程序,我想将其实现为一个模块。

因此,我已经根据插件的文档完成了所有步骤:
我已经创建了一个带有 BithAuth 库附带的控制器和视图的文件夹身份验证,并调用了模块控制器和方法,但这给了我一个 404 错误。

我注意到测试模块和我的 bithauth 模块之间存在差异,例如当我调用

<?php Modules::run('module/controller/method'); ?>

这在测试模块上,然后访问本地主机/主页/索引页面显示主页视图并呈现模块的视图。但是,当我在我的bitauth模块上调用它并访问localhost我被重定向到localhost/bithauth_controller/bitauth_method时,会抛出404错误。

我的文件夹结构:

->application  
-->controllers  
-->views  
-->models  
--->modules/auth/controllers  
--->modules/auth/vews/examples  
--->modules/auth/models    

映射到主网址的主控制器:本地主机

class Home extends MX_Controller {
    public function index()
    {
        $this->load->view('home');
    }
}

及其视图文件:

<html>
    <head>
        <title>Shop: index</title>
    </head>
    <body>
        <?php Modules::run('auth/Example/index'); ?>
    </body>
</html>

现在在身份验证文件夹中,我有 bithauth 控制器:

class Example extends MX_Controller
{
    /**
     * Example::__construct()
     *
     */
    public function __construct()
    {
        parent::__construct();
        $this->load->library('bitauth');
        $this->load->helper('form');
        $this->load->helper('url');
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
    }
    public function index()
    {
        if( ! $this->bitauth->logged_in())
        {
            $this->session->set_userdata('redir', current_url());
            redirect('example/login');
        }
        $this->load->view('example/users', array('bitauth' => $this->bitauth, 'users' => $this->bitauth->get_users()));
    }

}

和身份验证/视图中的视图:一个简单的表单

<body>
    <?php
        echo '<table border="0" cellspacing="0" cellpadding="0" id="table">';
        echo '<caption>BitAuth Example: Users</caption>';
        echo '<tr><th width="1">ID</th><th>Username</th><th>Full Name</th><th>Actions</th></tr>';
        if( ! empty($users))
        {
            foreach($users as $_user)
            {
                $actions = '';
                if($bitauth->has_role('admin'))
                {
                    $actions = anchor('example/edit_user/'.$_user->user_id, 'Edit User');
                    if( ! $_user->active)
                    {
                        $actions .= '<br/>'.anchor('example/activate/'.$_user->activation_code, 'Activate User');
                    }
                }
                echo '<tr>'.
                    '<td>'.$_user->user_id.'</td>'.
                    '<td>'.$_user->username.'</td>'.
                    '<td>'.$_user->fullname.'</td>'.
                    '<td>'.$actions.'</td>'.
                '</tr>';
            }
        }
        echo '</table>';
        echo '<div id="bottom">';
        echo anchor('example/logout', 'Logout', 'style="float: right;"');
        echo anchor('example/groups', 'View Groups');
        if($bitauth->is_admin())
        {
            echo '<br/>'.anchor('example/add_user', 'Add User');
        }
        echo '</div>';
    ?>
</body>

注意到重定向是由于我解决的方法中的if()语句index()但是我没有显示我的模块视图,也没有收到任何错误。

我没有显示我的模块视图,也没有收到任何错误。

试试这个:

<?php echo Modules::run('module/controller/method'); ?>

但最好使用这种方式(代码点火器输出类):

<?php $this->output->append_output( Modules::run('module/controller/method') ); ?>

我希望这有所帮助

因此,如果你们中的任何一个人在这里,在路由上使用REQUEST_METHOD(GET/POST/DELETE/PUT)并遇到404错误时寻找解决方案,这是解决方案:显然,CodeIgniter 的 HMVC 插件忘记考虑处理不同类型的路由请求,这与它的本机版本不同。实际上,当您为特定类型的请求定义路由时,它无法加载关联的模块。因此,我继续调整模块类。如果您按照上面其他人指定的代码结构维护了代码结构,则需要转到 application/third_party/MX/Modules.php 并将以下内容添加到第 234 行:

            $method = $_SERVER['REQUEST_METHOD'];
            $val = is_array($val) ?(empty($val[$method]) ?'' :$val[$method]) :$val;