Sterling Duchess
Sterling Duchess

Reputation: 2080

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.

Upvotes: 0

Views: 2258

Answers (2)

Sayed
Sayed

Reputation: 631

So if any one of you is here, looking for a solution when using a REQUEST_METHOD (GET/POST/DELETE/PUT) on your route, and facing the 404 error, here's the solution: Apparently, CodeIgniter's HMVC plugin forgot to consider handling the different types of routing requests, unlike it's native version. Actually, when you define a route for a specific type of request, it fails to load the associated module. So, I went ahead and tweaked the module class. If you've maintained your code structure as specified by others above, you need to go to application/third_party/MX/Modules.php and add following to line 234:

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

Upvotes: 0

Bence Ber&#233;nyi
Bence Ber&#233;nyi

Reputation: 179

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

Upvotes: 2

Related Questions