Codeigniter HMVC: I can't call "database" library inside controller - php

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.

Related

Load Database in core file codeigniter

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('*')

Yii2: How to force using fallback MessageFormatter method?

My website is with a hosting provider that has the MessageFormatter class available on the server (Linux, PHP 7.0.27) but it is an old ICU version (4.2.1) that doesn't support my message {number,plural,=0{# available} =1{# available} other{# available}} and gives the error:
Message pattern is invalid: Constructor failed
msgfmt_create: message formatter creation failed: U_ILLEGAL_CHARACTER
...because of the =1 and =2 notation.
I'm not able to make changes to the server so how can I force using the fallback method provided by Yii2 which works just fine?
There is this hacky way you can try.
Copy the yii\i18n\MessageFormatter code to a new file. Name it MessageFormatter.php and place somewhere in your application (but not in vendor folder).
In this new file change the format() method to:
public function format($pattern, $params, $language)
{
$this->_errorCode = 0;
$this->_errorMessage = '';
if ($params === []) {
return $pattern;
}
return $this->fallbackFormat($pattern, $params, $language);
}
Don't change anything else (including namespace).
Now let's use Yii mapping.
Find a place in your application when you can put code that will be run every time in bootstrapping phase. Good place for this is common/config/bootstrap.php if you are using "Advanced Template"-like project.
Add there this line:
Yii::$classMap['yii\i18n\MessageFormatter'] = 'path/to/your/MessageFormatter.php';
Obviously change the path to the one you've chosen. Now Yii autoloader will load this class from your file instead of the original Yii vendor folder (as mentioned in Class Autoloading section of the Guide).
In the modified file MessageFormatter method presence of intl library is never checked so fallback is used as default.
The downside of this trick is that you need to update manually your file every time original Yii file is changed (so almost every time you upgrade Yii version).
Another approach is to configure I18N component in your application to use your custom MessageFormatter where you can extend the original file and just override format() method inside without modifying class map.

Warning: include(FME/Manufacturers/Controller/Router.php): failed to open stream: No such file or directory

Ive been trying for hours to figure out why this magento install will not work. At first it may look like a normal error and im missing a file, but in reality thats not the case. The controller
FME_Manufacturers_Controller
doesn't exist anymore and i'm sure its from an old extension. I cant seem to find anything in the codebase that has to do with the FME controller and I was wondering if this controller can be called from the database. If so, what table? The error is from line 93 in /lib/Varien/Autoload.php
Magento loads controllers and its action like so:
// #see Mage_Core_Controller_Varien_Router_Standard::match
// instantiate controller class
$controllerInstance = Mage::getControllerInstance($controllerClassName, $request, $front->getResponse());
First to find the place where the controller is loaded from you could search your Magento installation for Mage::getControllerInstance('FME_Manufacturers for example.
Second is you could look for lines like <?php echo $this->getUrl('frontname/controllername/action') ?>. Often this is used in templates to call controllers and their actions.
Another thing is, if you follow the error backtrace you should be able to see there this call comes from, shouldn't you?
To call a controller from database is not possible. The only thing is that a static block or a CMS page is including a template and in this template the controller is called. But you would find this place by scanning through all template files in app/design looking for FME.

Joomla 3! Module Parameters

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.

CodeIgniter, Know the controller which called function

I am trying to create library that will write "error" messages in database, I do not think that I will be able to write PHPs errors nor CodeIgniters, but I can write my own "errors" something like
$this->error->write("User is not allowed to go in here");
I know there is a error handling already built in CodeIgniter on top of all it only supports writing errors/infos/debugs in file not database.
The real question is: how to get controller that called function.
Lets say I am in controller -> ./admin/settings.php and error ocures, code will call my library and I want it to store controller that called it. (I may be forced to send it as parameter but I don't want to write manually that it was fcn("error text", "/settings.php");
Assumed output: /controllers/admin/settings.php somewhere inside class that is called by controller.
You can use the router class to get this info
Also works with Codeigniter v3.0.x
To get the controller(class)
$this->router->class
To get the method(function)
$this->router->method

Categories