Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Ion Auth as a separate module in the HMVC structure

I am interested in using ion auth for a project of mine which is running on the HMVC pattern. The application is written in Codeigniter.

The problem I face is once the ion auth is placed in the /app/modules/auth folder, when I try to access the module I get the below error:

HTTP Error 500 (Internal Server Error):
An unexpected condition was encountered while the server was attempting to fulfill the request.

Please help me out here, I am sure that I am having some sort of a configuration/path problem but just can't figure out where.

I have simply downloaded the ion_auth files from github and placed the extracted files as it is in the module folder I removed all the lines where it loads the libraries such as database, session since I have used the config to auto load them. But I left the loading of the ion_auth library.

In the module folder modules/auth I have a similar application structure with the module specific config, libraries, etc folders.

Let me know where I must have done wrong, I will continue to search and fix this problem and post if I have any luck.

like image 328
Gayan Hewa Avatar asked Jun 15 '11 04:06

Gayan Hewa


4 Answers

Try this:

  1. Get: codeigniter.zip (CI2.0)
  2. Extract, make sure it is running, set config/config.php
  3. Get Modular Extension : HMVC
  4. Install - Copy MY_Loader & MY_Router to /core, MX to third party folder Do not copy MY_Controller - this is for Modular Separation and not Extensions
  5. Get Ion_auth
  6. Install the SQL for the Ion_auth
  7. Put Ion_auth into a modules folder /application/modules/users
  8. Add route in config/routes.php : $route['auth/(.*)'] = 'users/auth/$1';

  9. Autoload ion_auth - $autoload['libraries'] = array('database','session','users/ion_auth');

  10. Edit following paths in modules/users/library/ion_auth.php :

    $this->ci->load->config('users/ion_auth', TRUE);
    $this->ci->load->library('email');
    $this->ci->load->library('session');
    $this->ci->lang->load('users/ion_auth');
    $this->ci->load->model('users/ion_auth_model');
    
like image 158
ciuser Avatar answered Nov 17 '22 07:11

ciuser


I got CI 2.1 + Modular Extensions 5.4 + Ion Auth 2 all working.

Since, I didn't really see any exact info on this and the stuff I did see, had a bunch of things like routing and stuff that I couldn't get working the way they were done, I decided to share the what I did to accomplish this.

At first I was struggling with it, but then I had to sit back and think about what was going on.

After that, it was actually pretty straight forward, only a couple of gotchas…

The Steps I took to get ION AUTH working with CodeIgniter + MX HMVC

  1. Install CodeIgnter (I actually used an existing project I was working on, so it wasn't a fresh clean install. I removed "index.php" and I had HMVC already installed the recommended way. This is about Ion Auth anyway.)

  2. Get the latest version of Ion Auth.

  3. Instead of installing Ion Auth in application/third_party, Unzip it, and rename the resulting directory to auth. Put it in application/modules which results in application/modules/auth.

  4. Run Ion Auth's sql to set up the tables.

  5. In application/config/autoload.php update the line to:

    $autoload['libraries'] = array('database','session');
    
  6. In modules/auth/libraries/Ion_auth.php update the lines in __construct to:

    $this->ci->load->config('auth/ion_auth', TRUE);
    $this->ci->load->library('email');
    $this->ci->load->library('session');
    $this->ci->lang->load('auth/ion_auth');
    $this->ci->load->model('auth/ion_auth_model')
    
  7. In modules/auth/models/ion_auth_model.php update the lines in __construct to:

    $this->load->config('auth/ion_auth', TRUE);
    $this->load->helper('cookie');
    $this->load->helper('date');
    $this->load->library('session');
    $this->lang->load('auth/ion_auth');
    
  8. Change the auth controller (modules/auth/controllers/auth.php) to extend MX_Controller instead of the default CI_Controller.

  9. Now, in auth.php, make sure you change all $this->data to $data - (Make sure to read about this below!!).

  10. Move the files and directories in modules/auth/views/auth to modules/auth/views resulting in modules/auth/views with no lower level auth dir - (Make sure to read about this below!!).

  11. Add a routes.php file into modules/auth/config and add the following line:

    $route['auth/(:any)'] = "auth/$1";
    
  12. Now, go to http://yoursite/auth and everything should be good to go!

Gotchas

First off.. DO NOT AUTOLOAD THE LIBRARIES OR MODELS in the application/config/autoload.php file. Do them in the modules explicitly with $this->load->library("whatever"), etc…

That one stumped me for quite a while.

I forgot to mention that in my current install, I have already removed index.php from the URL and I have a .htaccess file in the the base of my installation. If things don't work, it's probably something with the RewriteBase here. This is the .htaccess I use:

## Set up mod_rewrite
<IfModule mod_rewrite.c>
Options +MultiViews +FollowSymLinks
DirectoryIndex index.php index.html

# Enable Rewrite Engine
# ------------------------------
RewriteEngine On

# UPDATE THIS TO POINT TO where you installed this FROM YOUR DOC ROOT.
# If this is in the DOC ROOT, leave it as it is
#---------------------
RewriteBase /

# In case your hosting service doesn't add or remove 'www.' for you, you can
# do it here by uncommenting and updating the 'Rewrite*'s below.
#
# Add or remove 'www.'  Whichever you prefer.  
# This one removes the 'www.' which seems to be the favorable choice these days. 
# ------------------------------
#RewriteCond %{HTTP_HOST} ^www.<sitename>.com
#RewriteRule (.*) http://<sitename>.com/$1 [R=301,L]

# Redirect index.php Requests
# ------------------------------
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{THE_REQUEST} !/system/.*
RewriteRule (.*?)index\.php/*(.*) $1$2 [R=301,L]

# Standard ExpressionEngine Rewrite
# ------------------------------
RewriteRule modules/(.+)/controllers/(.+)\.php$ /index.php?/$1/$2 [L,R=301]
RewriteRule controllers/(.+)\.php$ /index.php?/$1 [L,R=301]

RewriteCond $1 !\.(css|js|gif|jpe?g|png) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

=================================

When I updated the modules/auth/controllers/auth.php to extend MX_Controller instead of CI_Controller, I was getting a series of errors after. The first of these errors was:

    A PHP Error was encountered

    Severity: Notice

    Message: Undefined property: CI::$data

    Filename: MX/Controller.php

To resolve this error, I changed all $this->data to $data in the auth.php controller`.

After fixing this problem, when I would go to auth, I would get an error like this:

Unable to load the requested file: auth/login.php

Apparently, it can't find the view files in it's own views dir. Ahh. Not exactly true though after thinking about it. The reason is because it's trying to find module/file_to_view and the file_to_view should be in views! Not in auth/views/auth!! So, we need to move everyting up from the auth dir into the views dir!

After that, everything works fine! I can cross load models, libraries and controllers in other modules and I can do Modules::run() in views and everything else!

I hope this helps someone else. Good Luck!

like image 10
Dash Avatar answered Nov 17 '22 07:11

Dash


I don't see any reason for it to not work . check out pyrocms

They are using ionauth with hmvc.

if you don't get it working, just upload the files in normal ci directories and check if it works without any problems.

like image 1
Vamsi Krishna B Avatar answered Nov 17 '22 06:11

Vamsi Krishna B


This is what I did following ciuser's guideline but with some changes:

  1. Do a clean install of Codeigniter. Set up config.php, database.php etc.
  2. Install Modular Extension:
    Move third_party/MX to CI/application/third_party.
    Move core/MY_Loader.php and core/MY_Router.php to CI/application/core.
  3. Install Ion Auth:
    Move the following Ion Auth folders to CI/application/modules/auth folder: config, controllers, language, libraries, models.
    Move the files under Ion Auth/views folder to CI/application/modules/auth/views. (Without one extra layer of auth as in Ion Auth.)
    Run Ion Auth sql in database.
  4. Check it at yourbaseurl/index.php/auth.
like image 1
coderpulse Avatar answered Nov 17 '22 08:11

coderpulse