Welcome Guest, Not a member yet? Register   Sign In
Joomla and Codeigniter
#2

[eluser]danielgrin[/eluser]
I based my solution on code found here http://forum.joomla.org/viewtopic.php?f=304&t=580193.

At first I tried to load the Joomla framework within a CI library, but this caused a series of errors, and seemed like more trouble than it was worth.

My current working solution is to use CURL to access a custom script. I'm sure this could be refined further but I needed a quick solution.


JOOMLA SIDE (loads Joomla framework and returns logged in user object as JSON)
file: get_joomla_user.php (could be in the site root or anywhere as long as paths are correct)

Code:
<?php
define ( '_JEXEC', 1 );
define ( 'JPATH_BASE', $_SERVER ['DOCUMENT_ROOT'] . '/JOOMLA_ROOT/' );

define ( 'DS', DIRECTORY_SEPARATOR );
require_once (JPATH_BASE . DS . 'includes' . DS . 'defines.php');
require_once (JPATH_BASE . DS . 'includes' . DS . 'framework.php');
$mainframe = & JFactory::getApplication ( 'site' );
$mainframe->initialise ();

//I am using Jomsocal, so these are required to retrieve Jomsocial user object
require_once ( JPATH_ROOT.DS.'components'.DS.'com_community'.DS.'defines.community.php');
require_once (COMMUNITY_COM_PATH.DS.'libraries'.DS.'apps.php' );


//$user = JFactory::getUser (); //Joomla user object
$user = CFactory::getUser (); //Jomsocial user object
if ($user->id == 0) //user is not logged in
{
echo 'false';
} else {
echo json_encode($user); //return the user object as JSON object
}

CI SIDE - fetch user object via file_get_contents
file: CI_APP_FOLDER/libraries/Joomla_user.php

Code:
<?php
if (! defined ( 'BASEPATH' ))
    exit ( 'No direct script access allowed' );

class Joomla_user {
    var $CI; //create CI instance so we can access libraries
    var $get_user_url = 'http://URL_TO_get_joomla_user.php';
    
    function __construct() {
        $this->CI = & get_instance ();
    }    
    function get_user() {
        //Need to set cookie context so that Joomla can access it's session/user cookie
        $opts = array ('http' => array ('header' => 'Cookie: ' . $_SERVER ['HTTP_COOKIE'] . "\r\n" ) );
        $context = stream_context_create ( $opts );
        $juser = json_decode ( file_get_contents ( $this->get_user_url, false, $context ) );        
        if(!$juser){                            
            //Not logged in - echo error -or-
            //redirect to joomla login page
            $this->CI->load->helper('url');
            redirect('JOOMLA_LOGIN_PAGE_URL', 'refresh');
        }else{
            //Logged in
            return $juser;
        }
    }

}

I hope this helps.

Daniel


Messages In This Thread
Joomla and Codeigniter - by El Forum - 03-16-2011, 11:23 AM
Joomla and Codeigniter - by El Forum - 04-03-2011, 10:17 PM
Joomla and Codeigniter - by El Forum - 04-09-2011, 10:05 AM
Joomla and Codeigniter - by El Forum - 04-09-2011, 10:08 AM
Joomla and Codeigniter - by El Forum - 04-09-2011, 12:40 PM
Joomla and Codeigniter - by El Forum - 04-09-2011, 11:20 PM
Joomla and Codeigniter - by El Forum - 07-29-2011, 01:27 AM



Theme © iAndrew 2016 - Forum software by © MyBB