I want to use Laravel Environment for my Database credential, I did this:
inside bootstrap\start.php :
$env = $app->detectEnvironment(array(
'local' => array('My_PC'),
'production' => array('server.example.com')
));
created .env.local.php on same directory where serve.php is, and inside this code:
return array(
'DATABASE_NAME' => 'laravel_db',
'DATABASE_USER' => 'root',
'DATABASE_PASSWORD' => '1234'
);
and inside app\config created a local\app.php file containing this code:
return array(
'debug' => true,
);
and inside the app\config\database.php for my secured mysql connection I did:
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => $_ENV['DATABASE_NAME'],
'username' => $_ENV['DATABASE_USER'],
'password' => $_ENV['DATABASE_PASSWORD'],
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
but I'm getting an error on the CLI:
{"error":{"type":"ErrorException","message":"Undefined index: DATABASE_NAME","file":"c:\\xampp\\htdocs\\Larave_project\\app\\config\\database.php","line":58}}
How to resolve this issue?
The hostname based env detection solution will only work on Unix type system.
It won't work on Windows.
In Laravel 4.2 you can detect environment this way:
$env = $app->detectEnvironment(function() {
if ($_SERVER['SERVER_NAME'] == "127.0.0.1") {
$domain = $_SERVER['HTTP_HOST'];
if (strpos($domain, 'localhost') !== FALSE || strpos($domain, "127.0.0.1") !== FALSE) {
die("Configure your local `hosts` file and go to address: http://{storeName}.local");
}
$len = strpos($domain, ".local");
if ($len !== FALSE) {
// will load .env.*.php
$len = strpos($domain, ".local");
$env = substr($domain, 0, $len);
return $env;
}
}
// will load .env.php
return 'production';
});
(this is bootstrap/start.php)
Then:
setup hosts file (in your operating system) to redirect mysuperstore.local to 127.0.0.1
go to URL like http://mysuperstore.local which loads .env.mysuperstore.php into $_ENV.
In configuration files (those placed in app/config/) refer to $_ENV. To see where $_ENV does come from take a look into documentation topic about "Protecting Sensitive Configuration".
Related
I recently downloaded a free copy of a school management system built in PHP/CodeIgniter and installed it on my cpanel shared hosting server. I have followed the installation instructions and made the necessary changes to the database and route files but I always get an 'Unauthorized!' error when i load the application in the browser where the login page is supposed to come up.
I am not sure if i am getting the routes wrong because i installed the app into a sub folder in the public_html folder.
Can anyone give me advice on what i may be doing wrong. I have been at it since the past 72 hours. I am now confused as per the routes since the application was not deployed to the root of the domain. Any help will be highly appreciated.
the code below i have been changing with no luck:
$route['default_controller'] = 'login';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
Did you checked your database.php file
application -> config -> database.php
check the details it should be like this:
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'user_name',
'password' => 'your_password',
'database' => 'db_name',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
I got the same error when installing the same project; the issue came in the Login controller. You have to remove the link to install process and make it just connect directly to the database without any effort.
Change the present index function to this;
public function index() {
//$results = $this->unlinkInstaller();
if ($this->session->userdata('admin_login') == 1)
redirect(base_url() . '/admin/dashboard', 'refresh');
if ($this->session->userdata('teacher_login') == 1)
redirect(base_url() . '/teacher/dashboard', 'refresh');
if ($this->session->userdata('librarian_login') == 1)
redirect(base_url() . '/librarian/dashboard', 'refresh');
if ($this->session->userdata('accountant_login') == 1)
redirect(base_url() . '/accountant/dashboard', 'refresh');
if ($this->session->userdata('hostel_login') == 1)
redirect(base_url() . '/hostel/dashboard', 'refresh');
if ($this->session->userdata('student_login') == 1)
redirect(base_url() . '/student/dashboard', 'refresh');
if ($this->session->userdata('parent_login') == 1)
redirect(base_url() . '/parents/dashboard', 'refresh');
// if ($results['RESULT'] == 'INVALID' || $results['RESULT'] == 'EMPTY' || $results['RESULT'] == 'INVALID_DOMAIN') {
// $this->load->view('backend/error');
// } else {
// $this->load->view('backend/login');
// }
$this->load->view('backend/login');
}
You can see that I just commented the first line to avoid going through installation process.
//$results = $this->unlinkInstaller();
Hope it still usefull.
I'm working on a Drupal 8 starter kit with Composer, similar to drupal-composer/drupal-project.
In my post-install script, I want to re-generate a settings.php file with my custom values.
I've seen that can be done with the drupal_rewrite_settings function.
For example, I'm rewriting the config_sync_directory value like that :
require_once $drupalRoot . '/core/includes/bootstrap.inc';
require_once $drupalRoot . '/core/includes/install.inc';
new Settings([]);
$settings['settings']['config_sync_directory'] = (object) [
'value' => '../config/sync',
'required' => TRUE,
];
drupal_rewrite_settings($settings, $drupalRoot . '/sites/default/settings.php');
Problem is I want my Drupal 8 project to have a Dotenv so the maintainers don't have to modify the settings.php but only a .env file in the root folder of the project. To make it work, my settings.php must look like this :
$databases['default']['default'] = [
'database' => getenv('MYSQL_DATABASE'),
'driver' => 'mysql',
'host' => getenv('MYSQL_HOSTNAME'),
'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
'password' => getenv('MYSQL_PASSWORD'),
'port' => '',
'prefix' => '',
'username' => getenv('MYSQL_USER'),
];
$settings['trusted_host_patterns'] = explode(',', '^'.getenv('SITE_URL').'$');
As you can see, the values are replaced by PHP functions, and I can't see a good way to print those values, to the point I'm not even sure that's possible.
So my question is : is it possible to escape a PHP function as an Array value when declaring this variable ?
Looks like it's not possible because of the way the Drupal function works.
Solution 1 by #misorude
Using the drupal_rewrite_settings function, we can add the value of settings as a String, like this :
$settings['settings']['trusted_host_patterns'] = (object) [
'value' => "FUNC[explode(',', '^'.getenv('SITE_URL').'$')]",
'required' => TRUE,
];
And after that, we can replace all occurrences of "FUNC[***]" by *** directly in the settings.php file.
Solution 2
Put all your settings in a separate file. Example here, a custom.settings.php file :
if (getenv('DEBUG') == 'true') {
$settings['container_yamls'][] = DRUPAL_ROOT . '/sites/dev.services.yml';
$config['system.performance']['css']['preprocess'] = FALSE;
$config['system.performance']['js']['preprocess'] = FALSE;
}
$databases['default']['default'] = [
'database' => getenv('MYSQL_DATABASE'),
'driver' => 'mysql',
'host' => getenv('MYSQL_HOSTNAME'),
'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
'password' => getenv('MYSQL_PASSWORD'),
'port' => '',
'prefix' => '',
'username' => getenv('MYSQL_USER'),
];
$settings['trusted_host_patterns'] = explode(',', '^'.getenv('SITE_URL').'$');
$settings['file_private_path'] = 'sites/default/files/private';
$settings['config_sync_directory'] = '../config/sync';
Then we can copy the default.settings.php and add our custom settings.
$fs = new Filesystem();
$settings_generated = $drupalRoot . '/sites/default/settings.php';
$settings_default = $drupalRoot . '/sites/default/default.settings.php';
$settings_custom = $drupalRoot . '/../includes/custom.settings.php';
$fs->remove($settings_generated);
$fs->dumpFile($settings_generated, file_get_contents($settings_default) . file_get_contents($settings_custom));
There's also a appendToFile method that seems way better than dumping a new file with dumpFile, but it was not working unfortunatly.
We want to connect one of too many databases as a default.
as per user requriment so we want take a database name in a variable or Global variable.
we are unable to use any variable in Database.php file
so guys help me for this problem.
view code here Codeigniter 4.0.0
It has been a while since I tried to do "weird" things like attempting to assign dynamic variables in configuration files so I'm not entirely sure if CodeIgniter supports it. However, even if it does, I would suggest using the function designed for these sort of things:
https://codeigniter4.github.io/userguide/database/connecting.html#connecting-with-custom-settings
$custom = [
'DSN' => '',
'hostname' => 'localhost',
'username' => '',
'password' => '',
'database' => '',
'DBDriver' => 'MySQLi',
'DBPrefix' => '',
'pConnect' => false,
'DBDebug' => (ENVIRONMENT !== 'production'),
'cacheOn' => false,
'cacheDir' => '',
'charset' => 'utf8',
'DBCollat' => 'utf8_general_ci',
'swapPre' => '',
'encrypt' => false,
'compress' => false,
'strictOn' => false,
'failover' => [],
'port' => 3306,
];
$db = \Config\Database::connect($custom);
I suppose, since all else is the same except for database name you could also access the database configuration property for your default database and use $custom = array_replace($configDb, ['database' => $_SESSION['db_name']) to change out the database name. Obviously check that db_name exists and isn't empty and all that jazz. You could even put this entire logic in to a class with db_name as a constructor so you don't have to keep doing this all the time.
E.g. (untested):
namespace App\Config;
use InvalidArgumentException;
class DatabaseChooser extends \Config\Database
{
protected $dbName;
public function __construct(string $dbName = '')
{
$this->dbName = $dbName;
$config = $this->buildArr();
return parent::connect($config);
}
private function buildArr()
{
if (empty($this->dbName)) {
throw InvalidArgumentException('dbName cannot be empty');
}
$default = $this->defaultGroup;
if (property_exists($this, $default) && is_array($this->$default)) {
return array_replace($this->$default, ['database' => $this->dbName]);
} else {
throw new Exception('Invalid configuration.');
}
}
}
I'm sure you've considered it, but CI4 isn't exactly production ready afaik. To be honest, I'd be using something like Laravel, Lumen, or Symfony which already have an extensive and tested codebase, as well as many compatible third party packages.
In Laravel 4.2, I'm using App::detectEnvironment to change database according to machine name. I also now need to have it change environment according to an environment variable. I've tried to combine but it doesn't work at the moment and I'm not sure how to combine the two techniques.
Using machine name:
$env = $app->detectEnvironment(array(
'local' => array('.local', 'homestead'),
'staging' => array('ip-staging'),
'production' => array('ip-production')
)
);
Using a server environment variable:
$env = $app->detectEnvironment(function()
{
// Default to local if LARAVEL_ENV is not set
return getenv('LARAVEL_ENV') ?: 'local';
}
Non-working combined code looks like:
$env = $app->detectEnvironment(function()
{
// Default to machine name if LARAVEL_ENV is not set
return getenv('LARAVEL_ENV') ?: array(
'local' => array('.local', 'homestead'),
'staging' => array('ip-staging'),
'production' => array('ip-production')
);
});
Found the answer - thanks to #Marwelln's hint.
The $env variable needs to come from the detectEnvironment function for it to be recognised by Laravel.
if (getenv('LARAVEL_ENV'))
{
$env = $app->detectEnvironment(function()
{
return getenv('LARAVEL_ENV');
});
}
else
{
$env = $app->detectEnvironment(array(
// local development environments are set with machine host names
// developers find this out by typing 'hostname' at command line
'local' => array('*.local', 'homestead'),
'staging' => array('ip-staging'),
'production' => array('ip-production')
));
}
This should do it. It sets to environment to what you have in getenv('LARAVEL_ENV') if it's set, otherwise it uses the default $app->detectEnvironment method.
$env = getenv('LARAVEL_ENV') ?: $app->detectEnvironment(array(
'local' => array('.local', 'homestead'),
'staging' => array('ip-staging'),
'production' => array('ip-production')
));
I have a Kohana 3.0.14 website that i want to put on multiple domains, having associated a virtual host each (different ips).
the difference between my websites is the configuration file and the boostrap file (where i set the language to be used).
All the sites are in production.
How can i 'breakup' the website, how can i include the files so that i would have all the kohana site in a single place, and the config and boostrap on every server, so that when i am fixing an error to be fixed on every site (every domain)?
thanks a lot!
You could do that by setting up an environment variable at the top of you index.php file. Then depending on this variable, you'll set the configuration variables, languages, etc. This is usually how I handle staging/live/local environments, and doing so allows you to keep all the code identical between the various installations.
For example, in index.php:
define("ENV", "staging")
Then in bootstrap.php:
$baseUrl = "http://defaultdomain.com/";
if (ENV == "staging") $baseUrl = "http://staging.somedomain.com/";
Kohana::init(array(
'base_url' => $baseUrl,
));
In database.php:
if (ENV == "live") {
$hostname = ...
$database = ...
$username = ...
$password = ...
} else if (ENV == "staging") {
$hostname = ...
$database = ...
$username = ...
$password = ...
}
return array
(
'default' => array
(
'type' => 'mysql',
'connection' => array(
'hostname' => $hostname,
'database' => $database,
'username' => $username,
'password' => $password,
'persistent' => FALSE,
),
'table_prefix' => '',
'charset' => 'utf8',
'caching' => FALSE,
'profiling' => TRUE,
)
If your hosting options are limited, your best bet is to choose the first primary domain and create an account using that. Then park more domains on top. Then simply get the URL to decide what language etc you want to use.