Joomla 3 - How to get value from configuration file? - joomla

I'm building a custom component and I just want to get a value from the global config in my controller. I can't find any information about how to do this.
Something like...
$config = JFactory::getConfig();
$this->_db = $config->get('db');

The documentation on how to do it is slightly outdated:
http://docs.joomla.org/JFactory/getConfig
But if you check the code they actually drop the ampersand function:
https://github.com/joomla/joomla-cms/blob/staging/components/com_users/models/registration.php
$config = JFactory::getConfig();
$fromname = $config->get('fromname');
Also if you are trying to connect to the database you really can just use the DB object from JFactory.
$db = JFactory::getDbo();
Learn more about properly connecting to the database here:
http://docs.joomla.org/Accessing_the_database_using_JDatabase

Since Joomla 3.2:
JFactory::getApplication()->get($varname, $default);
See the reference

Related

Codeigniter 4 Current/Active Controller

In Codeigniter 3 is possible to get current active class and method with this code:
$active_controller = $this->router->fetch_class();
$active_function = $this->router->fetch_method();
Are there such functions in Codeigniter 4?
In CodeIgniter 4
$router = service('router');
$controller = $router->controllerName();
$router = service('router');
$method = $router->methodName();
Its worth saying those classes were never officially part of CI3 (https://codeigniter.com/user_guide/installation/upgrade_300.html?highlight=fetch_class). Bearing in mind CI4 is a lot more flexible and that routes are defined more variably I would look at the routing side of things and extract it from there (https://codeigniter4.github.io/userguide/incoming/incomingrequest.html#the-request-url).
You can use PHP constant or functions that provide you the same:
Get Current Function name:
__FUNCTION__
Get Current Class name:
__CLASS__
OR
get_class()
Codeigniter 4: All the above is working well otherwise use the same code that #mathan answered:
$router = service('router');
echo $router->controllerName();

Using X-Editable - Backend part

So I'm just trying to use xeditable (http://vitalets.github.io/x-editable/docs.html#gettingstarted) to make changes to my database via AJAX.
Since I'm new to this concept and I'm (forcefully) working with PHP for the first time, I need some help.
I setup the frontend part, and a script called (say) script.php is handling the data for me (I need to write the new value in my database).
I can't really understand what to do in the script. Can someone guide me towards it? The docs above don't really do it for me.
Looking in a project I worked on a few months back (sorry about the mysql_ stuff – not my choice!)
Something like:
<?
include your/database/connection_stuff.php;
// Can't remember if x-editable passes the table in as well or not
$table = mysql_real_escape_string($_GET['table']);
// If not,
$table = 'name_of_table';
$value = mysql_real_escape_string($_POST['value']);
$name = mysql_real_escape_string($_POST['name']);
$pk = mysql_real_escape_string($_POST['pk']);
$result = mysql_query("UPDATE `$table` SET `$name` = '$value' WHERE id = '$pk'");
?>
Will do the trick.

Sanitize input in joomla

I have code like this in my Joomla plugin:
$some_id = $_GET["someid"];
$db = JFactory::getDBO();
$db->setQuery("SELECT * FROM #__table WHERE id = '$some_id'");
$result = $db->loadRow();
Does Joomla sanitize this automatically, or i need to do something (and what) to sanitize this query ? Using Joomla 2.5.
There is no need to sanitize database queries when using Joomla. The information you are pulling down is the information that has put put there or already there, and thus you don't want to change. I would also recommend using Joomla 2.5 coding standards to make database queries, like so:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName('*'))
->from($db->quoteName('#__table'))
->where($db->quoteName('id') . ' = ' . $db->quote($some_id));
$db->setQuery($query);
$rows = $db->loadRow(); //or loadResult()
The only time I have ever needed to sanitize (so to speak) something was when handling files, in which case I used JFile::makeSafe();.
Please take a look at you will get your answer
Secure coding guidelines
http://docs.joomla.org/Secure_coding_guidelines
Take a look at JInput and this corresponding documentation
Filter example:
$jinput = JFactory::getApplication()->input;
$some_id = $jinput->get('someid', '', 'string');

CodeIgniter: current_url shows question mark

Say my page is on:
http://localhost/app1/profile/index/123/
The result of current_url() is this:
http://localhost/app1/?profile/index/123
There is a ? that shouldn't be there. The question mark seems to be caused by the following config setting:
$config['enable_query_strings'] = TRUE;
I need query strings enabled in my application. Any ideas what I need to do?
EDIT 1:
Also, in the case when the URL does have a query string, I need current_url to also return that. I'm hoping Phil Sturgeon's solution here CodeIgniter current_url doesn't show query strings will help me.
I'm using CI 2.1.0.
As mentioned, $config['enable_query_strings'] is sort of a "legacy" setting in Codeigniter from back when there was no support $_GET (really, none).
http://codeigniter.com/user_guide/general/urls.html
Enabling Query Strings
In some cases you might prefer to use query strings URLs:
index.php?c=products&m=view&id=345
c === Your controller name
m === Your method name
The rest is the method arguments. It's a very misleading description, and there's no mention of the other setting or query strings at all in the rest of the URL docs. I've never heard of anyone actually using this. CI comes with $config['allow_get_array']= TRUE; by default, which is what you want.
You can modify the current_url() function for query string support, just create application/helpers/MY_url_helper.php and use this:
function current_url($query_string = FALSE)
{
$CI =& get_instance();
$current_url = $CI->config->site_url($CI->uri->uri_string());
// BEGIN MODIFICATION
if ($query_string === TRUE)
{
// Use your preferred method of fetching the query string
$current_url .= '?'.http_build_query($_GET);
}
// END MODIFICATION
return $current_url;
}
Then call it like current_url(TRUE) to include the query string.
Don't use: $config['enable_query_strings'] = TRUE;
Use this instead: $config['allow_get_array']= TRUE;
enable_query_strings is not what you think and is not used much.
To build your own query strings, use one of these two:
$query_string = http_build_query($this->input->get());
$query_string = $this->input->server('QUERY_STRING');
along with this:
$lastseg_with_query = $lastseg.'?'.$query_string;
Please consult this SO Q&A for more information: URI Segment with Question Mark

execute php app/console doctrine:schema:update from controller symfony 2

I want to execute this command
php app/console doctrine:schema:update
from the controles without use exec php function,
Any comment will be use full to me.
Thanks!!!
If you are looking here, on the bottom there is an example of how to execute a command from within symfony code.
Please also mind the note on the end saying that it might not be a good idea to use a command within your code.
As said, the following code should be used with care. I wouldn't use it for the reasons statet in the symfony doc, but it works.
When using the following code within your controller, you are able to execute a command:
$kernel = $this->get('kernel');
$application = new \Symfony\Bundle\FrameworkBundle\Console\Application($kernel);
$application->setAutoExit(false);
$options = array('command' => 'list');
$application->run(new \Symfony\Component\Console\Input\ArrayInput($options));
If you need the output, you have to either use an existing class implementing OutputInterface or create your own depending on your needs.
Thanks to every one,
I used this code
$kernel = $this->get('kernel');
$application = new \Symfony\Bundle\FrameworkBundle\Console\Application($kernel);
$application->setAutoExit(false);
//Create de Schema
$options = array('command' => 'doctrine:schema:update',"--force" => true);
$application->run(new \Symfony\Component\Console\Input\ArrayInput($options));
//Loading Fixtures
$options = array('command' => 'doctrine:fixtures:load',"--append" => true);
$application->run(new \Symfony\Component\Console\Input\ArrayInput($options));
and in this link, there are some information maybe will be usefull for others
Thanks!!!

Resources