Custom .env variable on Codeigniter 4 - environment-variables

Normally if i want to change the database hostname in CI4 project i will change it in .env file and change the
database.default.hostname = localhost
but now i need to use MYSQL_HOST in env to change the hostname like so
MYSQL_HOST = localhost
can i do that in CI4? it will give error if i change the Database.php file to
public $default = [
'DSN' => '',
'hostname' => getenv('MYSQL_HOST'),
'username' => '',
'password' => '',
'database' => '',
'DBDriver' => 'MySQLi',
'DBPrefix' => '',
'pConnect' => false,
'DBDebug' => (ENVIRONMENT !== 'production'),
'charset' => 'utf8',
'DBCollat' => 'utf8_general_ci',
'swapPre' => '',
'encrypt' => false,
'compress' => false,
'strictOn' => false,
'failover' => [],
'port' => 3306,
];

I found the answer, lets say you have this in you .env file
MYSQL_HOST = localhost
MYSQL_USERNAME = root
MYSQL_PASSWORD = root
so if you want to change hostname of CI4 database you can add
$this->default['hostname'] = getenv('MYSQL_HOST');
inside __construct() in app/config/Database.php
so it will be look like this
public function __construct()
{
parent::__construct();
// Ensure that we always set the database group to 'tests' if
// we are currently running an automated test suite, so that
// we don't overwrite live data on accident.
if (ENVIRONMENT === 'testing') {
$this->defaultGroup = 'tests';
}
$this->default['hostname'] = getenv('MYSQL_HOST');
$this->default['username'] = getenv('MYSQL_USERNAME');
$this->default['password'] = getenv('MYSQL_PASSWORD');
}
well this is only if you want to custom your .env and dont want to use the default CI4 database.default.hostname

Related

my session timeout config don't work correctly in yii 2

I use this config for manage login duration time of users
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
'enableSession' => true,
'authTimeout' => 3600*24*120,
'loginUrl' => ['user/login']
//'class' => 'app\components\User',
],
'session' => [
'class' => 'yii\web\Session',
'timeout' => 3600*24*120,
'useCookies' => true,
],
and in login time set duration of login by :
Yii::$app->user->login($this->getUser(), 3600*24*120);
this is getUser() :
private $_user = false;
public function getUser()
{
if ($this->_user === false) {
$this->_user = User::find()
->where(['mobile' => trim($this->mobile)])
->andWhere(['not',['status' => User::STATUS['SUSPEND']]])
->one();
}
return $this->_user;
}
These code work correctly in local , but in server don't work.
I was forced change manually session.gc_maxlifetime value in php.ini for my target. it's work
Question: Why don't work these config in server without change php.ini ? where is the problem ?

How to call DB2 stored procedure in Zend Framework 2 placed in IBM i

I have configured Zend Framework2 in my local machine (ubuntu 14.04 LTS) with IMB_db2 driver as below.
global.php
$conn_string = "DATABASE='';HOSTNAME='xxx';PORT='xxx';PROTOCOL=TCPIP;UID='xxx';PWD='xxx'";
return array(
'db' => array(
'driver' => 'ibmdb2',
'database' => $conn_string,
'username' => '',
'password' => '',
'hostname' => '',
'port' => '',
'driver_options' => array(
'DB2_ATTR_CASE' => DB2_CASE_LOWER
),
'platform_options' => array(
'quote_identifiers' => false
)
),
'service_manager' => array(
'factories' => array('Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory'),
),
);
local.php
return array('db' => array('username' => 'xxx','password' => 'xxx',));
Using zend adapter I have called the stored procedure placed in a iseries box as below.
$sm = $this->getServiceLocator();
$this->adapter = $sm->get('Zend\Db\Adapter\Adapter');
$stmt = db2_prepare($this->adapter, 'Call <Procedure name>("xxx","xxx","?","?")');
if (!$stmt) {
die('Preparing Statement failed.' . db2_stmt_error());
}
From the above snippet I am getting 'Preparing Statement failed.' with out any error message.
When I print $this->adapter, it is giving connection object.
Can any one help me on this, please?

firewall pattern with parameters on anonymous asking for login

I have a route that has a parameter and its tripping my firewall thinking it needs to be logged in first. I tried to setup the pattern to use the name form at as used in the route but it still saying it requires authentication.
is there a special way to get the patter to work with parameters? I'm failing to see how to so that.
Thanks
$app->register(new Silex\Provider\SecurityServiceProvider(), [
'security.firewalls' => [
'login' => [
'pattern' => '^/login$',
'anonymous' => true
],
'pwdRecovery' => [
'pattern' => '^/recover',
'anonymous' => true
],
'newPassword' => [
'pattern' => '^/newpassword$',
'anonymous' => true
],
// Any other URL requires auth.
'authenticated' => [
'pattern' => '^.*$',
'form' => [
'login_path' => '/login',
'check_path' => '/authenticate'
],
'anonymous' => false,
'logout' => ['logout_path' => '/logout'],
'users' => $app->share(function() use ($app) {
return new App\Providers\UserServiceProvider();
}),
]
],
'security.access_rules' => [
['^/admin', 'ROLE_ADMIN']
],
'security.encoder.digest' => $app->share(function() {
return new BCryptPasswordEncoder(15);
})
]);
This may be a regex problem. I can pass parameters to the ^/recover route just fine.
$app->get('/recover/{id}', function (Request $request, $id) use ($app) {
error_log(print_r((int) $id,1).' '.__FILE__.' '.__LINE__,0);
});
But if I add $ to that route like ^/recover$ then it redirects to login because the dollar sign dictates end of string.

Global and module config interaction

Let's imagine I have global application configuration
return array(
'languages' => array(
'allowed' => array('de', 'en'),
),
);
And I have module configuration with the routes description. I need routes, based on global configuration, so I need to read global application config within the module to compose my routes according to languages->allowed values (constraints for the segment type route)
What is the best way to get global configuration values from the module configuration script? is it correct at all to manipulate data in configuration file instead of simple array return?
You should think a bit more ahead of your problem. You want to create a route structure based on your configuration. The configuration could come from everywhere: module config, local config and global config. It is therefore quite hard to base your module's config on a global one.
What you can do, is create the routes later. For example, you create in your module Foo the config like this:
'routes_foo' => array(
'bar' => array(
'type' => 'segment',
'options' => array(
'route' => ':locale/foo/bar',
'constraints' => array(
'locale' => '%LOCALE%',
),
),
),
),
And in your module class:
namespace Foo;
class Module
{
public function onBootstrap($e)
{
$app = $e->getApplication();
$sm = $app->getServiceManager();
$config = $sm->get('config');
$routes = $config['routes_foo');
$locales = $config['languages']['allowed'];
$routes = $this->replace($routes, array(
'%LOCALE%' => sprintf('(%s)', implode('|', $locales)
);
$router = $sm->get('router');
$router->routeFromArray($routes);
}
public function replace($array, $variables)
{
foreach ($array as $key => $value) {
if (is_array($value)) {
$array[$name] = $this->replace($value, $variables);
}
if (array_key_exists($value, $variables)) {
$array[$name] = $variables[$value];
}
}
return $array;
}
}
What happens is you grab the routes from your config (those are not automatically injected in the router). There you also load all languages from your global config. Then your "custom" routes have (at several places) a "magic" configuration key, which will be replaced by a regex constraint for the locales: (en|de). That parsed config is then injected into the router.

Where is SugarFullTest_Version2.php? (Sugar CRM and SOAP)

In regards to using SOAP to connect to Sugar CRM, the documentation for Sugar 6.1 Community Edition states:
"See /examples/SugarFullTest_Version2.php for more examples on usage."
source:
http://developers.sugarcrm.com/docs/OS/6.1/-docs-Developer_Guides-Sugar_Developer_Guide_6.1.0-Chapter%202%20Application%20Framework.html#9000244
This file is not in the examples folder. Where is it?
If this file does not exist, where can I find a working example of connecting to Sugar CRM with SOAP? None of the test scripts in the /examples/ folder work.
Couldn't find the file either, so made an example (PHP script connecting to sugarCRM v6 SOAP) for you.
<?php
require_once('include/nusoap/lib/nusoap.php');
$myWsdl = 'http://mysite.com/soap.php?wsdl';
$myAuth = array(
'user_name' => 'xxxx',
'password' => MD5('xxxx'),
'version' => '0.1'
);
$soapClient = new nusoap_client($myWsdl,true);
// Create lead
// (Can be made without login, i.e. sessionid)
$leadParams = array('user_name' => 'xxxx',
'password' => MD5('xxxx'),
'first_name' => 'Test',
'last_name' => '2',
'email_address' => '2#'
);
$leadResult = $soapClient->call('create_lead', $leadParams);
$leadId = $leadResult;
print_r($leadResult);
// Login
$loginParams = array('user_auth' => $myAuth, 'application_name' => 'WebForm');
$loginResult = $soapClient->call('login', $loginParams);
$sessionId = $loginResult['id'];
// Modules
// (Need login, so sessionID is used)
$modulesResult = $soapClient->call('get_available_modules', array('session' => $sessionId));
print_r($modulesResult);
// Get account list
$accountParams = array('session' => $sessionId,
'module_name' => 'Accounts',
'query' => "accounts.name = 'Amarelo'",
'order_by' => '',
'deleted' => 0
);
$accountResult = $soapClient->call('get_entry_list', $accountParams);
print_r($accountResult);
// Get entry
$leadParams = array('session' => $sessionId,
'module_name' => 'Leads',
'id' => "$leadId"
);
$leadResult = $soapClient->call('get_entry', $leadParams);
print_r($leadResult);
// Logout
$logoutResult = $soapClient->call('logout', array('session' => $sessionId));
?>
For debugging and testing SoapUI is very helpful.

Resources