Search code examples
phpcodeigniterhttp-status-code-404codeigniter-2

CodeIgniter HMVC Arhitecture 404 Error


I have CodeIgniter 2.1.3 extended with following HMVC here plugin i followed it to the end and got it to work with no problems with the basic example. However now i would like to extend my application with BitAuth authentication libary and i would like to implement it as a module.

So i have done all the steps according to documentation for the plugin:
I have created a folder auth with controllers and view that comes with BithAuth libary and called the module controller and method yet this gives me a 404 error.

I noticed one diffirence between the test module and my bithauth module for instance when i call

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

this on the test module and then visit localhost/home/index the page shows home page view and renders the view of module. However when i call this on my bitauth module and i visit localhost i get redirected to localhost/bithauth_controller/bitauth_method and that throws a 404 error.

My folder structure:

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

My home controller that maps to home url: localhost

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

and its view file:

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

Now in the auth folder i have bithauth controller:

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()));
    }


}

And the view in auth/views: a simple form

<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>

I noticed that redirect was happening due to if() statement in index() method i solved that now however i dont get the view of my module displayed and i dont get any errors.


Solution

  • i dont get the view of my module displayed and i dont get any errors.

    try this:

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

    but better to use this way (Codeigniter Output class) :

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

    i hope this helps