I am creating a core file in codeigniter for user and want to run some database queries there as well. core file is for users so when ever i open website users data hould be available automatically and no need to create a library and call it again and again.
I have tried autoloader and Codeigniter.php file but its not working.Do anyone have idea how to achive this. so that i can use 'user' function easily like. $this->user->login() , $this->user->logout() ... same as some builtin functions pre-loaded like $this->input->get() or $this->load->SOMETHING()... etc etc.
You can try hooks for db in core file. enable hooks on config.php
$config['enable_hooks'] = TRUE;
and use this for more see this https://codeigniter.com/userguide3/general/hooks.html
$CI =& get_instance();
$this->CI->db->select('*')
Related
I have been fighting with this problem for several days now & I would appreciate input from (PHP / Wordpress) developers that are more experienced than me.
I'm making a debugging plugin for Wordpress, but one of the errors that I need to change comes from this file:
/site/public/wp-includes/wp-db.php
I need to change the structure and add more information to the $error_str in lines 1363 and 1366 so the output better suits our log reader and contains more useful information.
To do that I think I need to change the print_error() in line 1344
The problems are:
That the file is a core file in Wordpress, so I can't edit it directly.
It also have a global $wpdb variable that is used to call: $wpdb->query() & $wpdb->get_row() (also core functions in same file & class) that both in turn calls the print_error() function.
Other plugins are using these functions & I can't mess up their functionality but I should change their output too.
I think the most likely solution is to inherit & override the print_error() (and maybe also query() & get_row() so I can re-direct them to my new print_error()), but other plugins are using the $wpdb & those functions, I can (should) change the error_log() output for those plugins too, but I can't edit those plugins either & I can't mess them up with a faulty solution.
Questions:
How can I replace the print_error()?
What is the best way to do this?
How do I deal with the global $wpdb variable?
Do any of you have a better solution? I would very much appreciate it.
Link to the print_error() documentation (with source code):
https://developer.wordpress.org/reference/classes/wpdb/print_error/
Link directly to the source code: https://core.trac.wordpress.org/browser/tags/4.9.8/src/wp-includes/wp-db.php#L1344
Not entirely sure this is an excellent idea, but you could extend the wpdb class and replace the global $wpdb object.
E.g.
global $wpdb;
$wpdb = new YourWpdbClass(); // you can override methods here to your heart's content
wp_set_wpdb_vars();
Officially, the way to replace the global $wpdb is by creating a db.php file inside wp-content. So if you plugin needs to alter db functionality it would need to include that file and possibly custom installation instructions
I'm not very experienced with php projects structure, I found this awesome and simple tutorial: https://arjunphp.com/creating-restful-api-slim-framework/ how to create simple slim rest app.
This is actually PHP SLIM's official project structure, my question is what is best and proper way to add and use RedBean php ORM, I dont want on every route to include something like this
use \RedBeanPHP\R as R;
R::setup( 'mysql:host=localhost;dbname=mydatabase', 'myusername', 'mypassword)
and then
$book = R::load( 'book', $id );
And then use ReadBean for my db stuff. Im wondering how to include RedBeans into project and then just use it where i need it. This is my project structure https://github.com/iarjunphp/creating-restful-api-slim-framework3.
Note: i added red beans via composer like its described here https://github.com/gabordemooij/redbean
You can put the code for setting up your libraries in any file that is going to be included on each request, so assuming you're using slim/slim-skeleton, src/dependencies.php is probably the place you want to add these two lines:
use \RedBeanPHP\R as R;
R::setup( 'mysql:host=localhost;dbname=njux_db', 'root', '');
Then you can use ReadBeans in your route callbacks but you also need to add the use \RedBeanPHP\R as R; statement to your src/routes.php as well (or any file that is going to use this class)
If you use a MVC framework (which I recommend) like codeigniter it's pretty easy.
You only have to copy your rb.php to the application/third_party folder.
Then create a file called application/libraries/rb.php containing a code like this one.
<?php
class Rb {
function __construct() {
include(APPPATH.'/config/database.php');
include(APPPATH.'/third_party/rb.php');
$host = $db[$active_group]['hostname'];
$user = $db[$active_group]['username'];
$pass = $db[$active_group]['password'];
$db = $db[$active_group]['database'];
R::setup("mysql:host=$host;dbname=$db", $user, $pass);
}
}
?>
...and vôila. RedBean will read your database configuration from CodeIgniter's standard file application/config/database.php and you will be able to use any R:: command from anywhere in your code. No includes, not additional code required :-)
I have a Joomla website where I have a custom module with mod_myModuleName.php and mod_myModuleName.xml files and a folder where there are several PHP scripts that add special functionality to my module. There is a config.php file in the folder that holds an associative array with variables and their values hard-coded. The module works just fine.
What I want though is to provide administrator area for the values of the variables in the array, so that I can put values in administrator panel and get their values in config.php. In my mod_myModuleName.php I use <?php echo $params->get('param')?> and it works like a charm.
But when I try to use the same technique in the config.php it breaks my code. I tried to get the values in mod_myModuleName.php and then include it in config.php and use the variables but it does not work either. I have not got so much experience in php and cannot understand what can be the reason.
It sometimes gives me an error of something non object and I guess it must be something connected with object oriented php, am I right? And if so is there a way to overcome this without object orientation or how can I solve my problem?
The problem will be with the way you're using your config.php.
When your modules entry point file mod_myModuleName.php is loaded by Joomla the $params object is already available in that context, you need to provide it to your scripts.
If you look at something like the mod_articles_latest module you will notice that the helper class is included with this line:
require_once __DIR__ . '/helper.php';
And then helper class is has it's getList() method called statically with the $params passed into it, so that $params is available to class context:
$list = ModArticlesLatestHelper::getList($params);
Inside the helper class ModArticlesLatestHelper you will notice that the getList() expects the $params to be passed in.
public static function getList(&$params)
{
...
}
I would strongly recommend reading the articles in the Modules section of Developers Portal on the Joomla Doc's.
Try the "Creating a simple module" article.
I've ran into a problem that can't seem to have an answer.
I am using CodeIgniter 2.1.4 with HMVC.
My question is: How can I load a core library in a controller of a module?
I want to load the "database" library (which is a core library) inside a method of a controller (or model for that matter) in order to avoid connecting to the database if there is no need to do it (when i have cached results inside a text file).
I understand that you can use autoload per module, but I only want to load the library if some conditions are met.
I also know you can load libraries that are inside the libraries folder of the module folder, but if I only need one database why would I paste the database library inside every single module just to make the connection.
When I try to load the library with "$this->load->library('database');", it gives me the following error:"Unable to load the requested class: database". Could you please help?
You can load a database with:
$this -> load -> database();
Whenever you want to load a specific database, you give the name of the database to the load function:
$this -> load -> database('DBname');
Where you specify your database propeties in config\database.php.
Note that you define your database name/group with the first key:
$db['This is your database group']['hostname'] = '127.0.0.1';
see here.
Hope this was helpful.
I'm not sure if this is possible, but various posts in the ExpressionEngine forums and in the documentation suggest that I should be able to let ExpressionEngine know what's going on in the CodeIgniter foundation that it's built on.
So, in light of that, I've got a CodeIgniter app, and an ExpressionEngine site. The /system directory is structured like this:
/system
/system/codeigniter
/system/codeigniter/application (this is my existing CodeIgniter application)
/system/codeigniter/system (this is the same CI folder that EE runs from)
/system/expressionengine
... (this is the normal EE - templates, add-ons, etc.
I'm fairly new to ExpressionEngine, but my understanding was that I could use this setup to built an EE module that would integrate well with CodeIgniter.
Currently, I have a module setup, and a method in it that does this, just as a starting point:
function __construct()
{
$this->EE =& get_instance();
$this->CI =& get_instance();
$this->auth =& $this->CI->load->library('mylibrary');
}
But then it doesn't recognize the library when I load the module (this is all happening inside the EE control panel, at this point, just so I can make a connection between the two). Says it doesn't exist. Is there anything I can do to make this connection, or am I going the wrong direction entirely?
Thanks,
Jonathan
Okay, thanks to http://expressionengine.com/forums/viewthread/208140/ I have an answer I think I can build upon.
Here's how it works, for posterity's sake:
EE and your module don’t know at all about your system/codeigniter/application folder. Try this, it > might work:
$this->EE =& get_instance();
$this->EE->load->add_package_path(BASEPATH.'../application/');
$this->EE->load->library('mylibrary');
$this->EE->mylibrary->do_stuff();
From there I was able to load my library, but it had issues loading its language file. The same thread gave an answer to that, as well. This code goes into the CI library file, and EE translates it as it loads.
$this->ci->lang->load('mylangfile', '', FALSE, TRUE, BASEPATH.'../application/');
In the past I've written simple wrapper add-ons for EE to access the CodeIgniter helpers, etc. Here's an example of a simple wrapper add-on.
Another option, it should be possible to this with PHP, which means you'll need to enable PHP in your template. In short, you load the helper/library/etc and then call the method you want. More info on how to do that in the EE docs.
You should simply put your library in /third_party/my_addon/libraries/ folder, then load it like so:
function __construct()
{
$this->EE =& get_instance();
$this->auth = $this->EE->load->library('../third_party/mhy_addon/libraries/mylibrary');
}