Codeigniter & Joomla Integration – Part 2
Last time we covered how to get a website running on Joomla & Codeigniter.
The next question is, how to access Joomla’s API within any additional controllers you want to create within the Codeigniter framework.
The easiest way is to extend the defualt CI_Controller class and add some core functionality. Create a file called MY_Controller.php and place it in /application/core/ folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | class MY_Controller extends CI_Controller { var $joomla_app; var $user; var $user_groups; function __construct() { parent::__construct(); $this->joomla_app = &JFactory::getApplication('site'); $this->joomla_app->initialise(); $this->user = &JFactory::getUser(); if ($this->user->id > 0) { $this->user_groups = $this->user->getAuthorisedGroups(); } } } /* End of file MY_Controller.php */ /* Location: ./application/core/MY_Controller.php */ |
Let me explain, what we are doing in the above code.
First, we define a few variables in the CI_Controller class to store the Joomla application object, user object, and the authorised user groups as an array.
Next, in the construct, we are initializing the Joomla’s Site application, get the logged in user and there authorised groups if the user is logged in.
Any new controller that you write after this should extend this Class.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <!--?php if (!defined('<span class="hiddenSpellError" pre=""-->BASEPATH')) exit('No direct script access allowed'); class Dashboard extends MY_Controller { public function __construct() { parent::__construct(); } public function index() { /*Check if the user is logged in*/ if ($this->user->id > 0) { /*Write you magic to display the dashboard*/ } else { /*Do whatever you need to do when the user is guest*/ } } } /* End of file dashboard.php */ /* Location: ./application/controllers/dashboard.php */ |
Hope this helps.
Fatal error: Call to undefined method JSite::execute() in C:\xampp\htdocs\p\jom\j_c\1\application\controllers\welcome.php on line 29
Hello Ramana this is my error which I got after following all your steps
help it
Can you share your welcome.php file via pastebin. Also, what version of Joomla & Codeigniter are you using?
Thanks,
Ramana
This trick works fine for static Page
But not working for using for $this->load->database();
Error: Failed to start application: Call to undefined function mysql_pconnect()
and also function not exist when using base_url() or current_url() ( error not exist function )
how to use joomla menu in codeigniter controller & view?
Codeigniter Development & Joomla Development, both have their own features and developers work on both of the framework. Thank a lot for sharing this informative information. Keep Post like this always.