My server with Joomla and API:
web/pruebasmario/joomla
web/public/api
I need use Jfactory..etc, functions plugin, componentes Joomla 2.5 in my Api.
I created a file accesjoomla.php in web/public/api:
<?php
include('../../pruebasmario/extjom.php');
$user = JFactory::getUser();
echo "Usuario " . $user->username . " con id: " . $user->id . " conectado a Joomla";
I created a file extjom.php in web/pruebasmario/joomla:
/* Initialize Joomla framework */
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__) );
define( 'DS', DIRECTORY_SEPARATOR );
/* Required Files */
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
/* To use Joomla's Database Class */
require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'factory.php' );
I look this: include Jfactory class in an external php file, Joomla
Access Joomla 2.5 from external script to get article by id
Joomla 2.5 Get User Data from External Script
BUT NO workk!!!!! :-((
Help me plez!
thanks!
EDIT: I managed to take a step.
Adding:
require_once (JPATH_BASE. DS. 'libraries'. DS. 'joomla'. DS. 'error'. DS. 'error.php');
I have no errors. But nothing appears. all white.
The return of the api is not returned.
What is happening?
Related
I try to launch the functions of a joomla controller from an external script. The beginning of the script works very well but the use of the controller does not.
Thank you for your help,
<?php
define('_JEXEC', 1);
// this file is in a subfolder 'cron' under the main joomla folder
define('JPATH_BASE', realpath(dirname(__FILE__) . '/..'));
require_once JPATH_BASE . '/includes/defines.php';
require_once JPATH_BASE . '/includes/framework.php';
use Joomla\CMS\Factory;
// instantiate application
$app = Factory::getApplication('site');
// database connection
$db = Factory::getDbo();
jimport('joomla.application.component.controller');
JLoader::import('Article', JPATH_ROOT . '/components/com_content/controllers');
$controller = JControllerLegacy::getInstance('Article');
var_dump($controller);
I don't get anything to instantiate a controller from any other place instead of the controller's component.
So I visit the BaseController class where the getInstance() method lays. You can hack this by setting a task through application input.
<?php
define('_JEXEC', 1);
// This file is in a subfolder 'cron' under the main joomla folder
define('JPATH_BASE', realpath(dirname(__FILE__) . '/..'));
require_once JPATH_BASE . '/includes/defines.php';
require_once JPATH_BASE . '/includes/framework.php';
use Joomla\CMS\Factory;
// Instantiate application
$app = Factory::getApplication('site');
// Get the input instance
$input = $app->input;
// Database connection
$db = Factory::getDbo();
/**
* You have to provide a task for getting the controller instance.
* The `article` is the controller filename as well as the controller class's suffix
* See the controller class name is `ContentControllerArticle`
*
*/
$input->set('task', 'article.');
/**
* The first argument of the getInstance() function is the type.
* The type is the component name here.
* And you have to pass the base_path of the component through the $config argument.
*/
$controller = JControllerLegacy::getInstance('Content', ['base_path' => JPATH_ROOT . '/components/com_content']);
echo "<pre>";
print_r($controller);
echo "</pre>";
in Joomla you can pass php variables from the template e.g. in less files. Now i try to do the same with scssphp (scss compiler from leafo.net)
So i think first i have to load the Joomla Framework in the style.php and define the variables $options = $this->params->get('option'); also there, but i´m not sure.
Is that possible at all? All my tests fail ...
Here an example:
require_once "scssphp/scss.inc.php";
use Leafo\ScssPhp\Server;
$directory = "scss";
Server::serveFrom($directory);
// Get Joomla! framework
define( '_JEXEC', 1 );
define( '_VALID_MOS', 1 );
define( 'JPATH_BASE', realpath(dirname(__FILE__)));
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();
$option = $this->params->get('option');
If i now use #option in style.scss i get this error:
Parse error: Undefined variable $option: line: 14
I have the following PHP script file.php in my Joomla site:
$user = JFactory::getUser();
$usr_id = $user->get('id');
If I run it directly, using in HTML:
include_once "file.php";
It will get the user id, no problem.
However, if run it through an Ajax request:
$.ajax({
url: "other.php",
...
Where other.php is:
include_once "file.php";
I get the error:
Fatal error: Class 'JFactory' not found in file.php on line 3
Why? Any help!?
You need to import the Joomla library in order to use the JFactory class. To import the library, add the following to the top of your file:
define( '_JEXEC', 1 );
define( 'JPATH_BASE', dirname(__FILE__) ).'/../..' );
require_once ( JPATH_BASE .'/includes/defines.php' );
require_once ( JPATH_BASE .'/includes/framework.php' );
require_once ( JPATH_BASE .'/libraries/joomla/factory.php' );
$mainframe = JFactory::getApplication('site');
$mainframe->initialise();
I am trying to make a login system for an android application that works in with my 2.5 Joomla website. I am trying to do this by making a Joomla plugin which the android application sends post data to a php file which that then authenticates the user to see if the credentials are correct or not for the login. I have been trying to get this working all afternoon but it seems all my attempts of importing JFactory are failing.
I have the below code which bugs out at the first mention of JFactory. My attempt of importing it is not working either.
<?php
//needed to be commented out otherwise the android application cannot send data to the file
//defined('_JEXEC') or die;
define('_JREQUEST_NO_CLEAN', 1);
define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);
if (file_exists(dirname(__FILE__) . '/defines.php')) {
include_once dirname(__FILE__) . '/defines.php';
}
if (!defined('_JDEFINES')) {
define('JPATH_BASE', dirname(__FILE__));
require_once JPATH_BASE.'/includes/defines.php';
}
require_once JPATH_BASE.'/includes/framework.php';
require_once JPATH_BASE.'/includes/helper.php';
require_once JPATH_BASE.'/includes/toolbar.php';
require JPATH_BASE.'/library/joomla/factory.php';
$email = $_POST['email'];
$password = $_POST['password'];
file_put_contents("Log.txt", "got data: ".$email." and ".$password);
$credentials->email = $email;
$credentials->password = $password;
$responce;
//error occurs here
$db = JFactory::getDbo();
...
?>
I have looked at these other questions about importing JFactory but none of them are working for me:
- JFactory,JDatabase class not found in /var/www/joomla2.5/database.php
- Class 'JFactory' not found
- include Jfactory class in an external php file, Joomla
So my simplified question is how do I import JFactory or at least work around it in this situation? Anyone feeling smarter than me who would be so kind to answer the question :)
General Information:
running php 5.1.13
Joomla 2.5
Testing on wamp server (localhost)
I suggest to go the official Joomla way and bootstrap an application. What I did works pretty well and is the recommended Joomla way (I haven't tested the code below but it should give you a starting point as it was a copy paste from multiple sources in my code).
define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);
error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', 1);
define('JPATH_BASE', dirname(dirname(dirname(__FILE__))));
require_once JPATH_BASE.DS.'includes'.DS.'defines.php';
require JPATH_LIBRARIES.DS.'import.php';
require JPATH_LIBRARIES.DS.'cms.php';
JLoader::import('joomla.user.authentication');
JLoader::import('joomla.application.component.helper');
class MyJoomlaServer extends JApplicationWeb {
public function doExecute() {
$config = JFactory::getConfig();
$email = $_POST['email'];
$password = $_POST['password'];
$user = ...;//get the user with the email
file_put_contents("Log.txt", "got data: ".$email." and ".$password);
$authenticate = JAuthentication::getInstance();
$response = $authenticate->authenticate(array('username' => $user->username, 'password' => $password));
return $response->status === JAuthentication::STATUS_SUCCESS;
}
public function isAdmin() {
return false;
}
}
$app = JApplicationWeb::getInstance('MyJoomlaServer');
JFactory::$application = $app;
$app->execute();
Something along the lines works for me. More platform examples can be found here https://github.com/joomla/joomla-platform-examples/tree/master/web.
try this,
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__) );//this is when we are in the 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();
$db = JFactory::getDbo();
It will work, you missed JFactory::getApplication('site');
Hope it helps..
I have three drop down in my module.I want to call ajax on drop down onChange() event in my joomla 2.5 module.
Country,State,City are drop down. user select country from first drop down when all state result display in second drop down using ajax.
How to do above functionality using AJAX in joomla 2.5 module.
Please help me.
There is no official way to do ajax from a module.
If you where to develop a component you could call your component with
index.php?option=com_yourcomponent&task=getjsondata&view=yourview&format=json
This way your the file views/yourview/view.json.php would be called and there you can get your data send it out echo json_encode( array("success"=>true) ); jexit()
please note the use of jexit() insted o exit()
If you really need to do ajax from a module here's a hack to import joomla in your files
<?php
if (! class_exists('JFactory') ) {
define( 'DS', DIRECTORY_SEPARATOR );
$rootFolder = explode(DS,dirname(__FILE__));
$currentfolderlevel = 2;
array_splice($rootFolder,-$currentfolderlevel);
$base_folder = implode(DS,$rootFolder);
if(is_dir($base_folder.DS.'libraries'.DS.'joomla'))
{
define( '_JEXEC', 1 );
define('JPATH_BASE',implode(DS,$rootFolder));
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
// Joomla is loaded! horray!
$email = JRequest::getVar('email');
$tags = implode(",",JRequest::getVar('tags',array()));
$db = JFactory::getDbo();
$db->setQuery("INSERT INTO `#__newsletter_users` (email,tags) VALUES('$email','$tags')");
$db->query();
}
}
?>
create a file like the one above (save_email.php for me) in your module folder and call it directly. Here is a piece of my modules template:
$.get('<?php echo JUri::base()."modules/mod_newsletter/save_email.php?email=" ?>'+email, function(data) {
console.log(data);
modal.hide();
});