Yii2 Db Connection to another server - yii

I am trying to connect a db from another server.
Is it possible to connect Yii2 db connection from one server to another server db?

In db config you have to specify IP address of your database in your dsn:
'dsn' => 'mysql:host=YOUR_IP_HERE;dbname=YOUR_DB_HERE',
And that's all.
It's possible that second server doesn't allow connections on 3306 port, so you have to allow it in iptables (linux servers).

It is very possible, even you can connect yii2 application with many different database server. You just need to add a bit in the configuration file as below:
'components' => [
'db_server1' => [
'class' => 'yii\db\Connection',
'dsn' => 'pgsql:host=localhost;dbname=DB_NAME;port=PORT_CONNECTION',
'username' => 'DB_USERNAME',
'password' => 'DB_PASSWORD',
'charset' => 'utf8',
],
'db_server2' => [
'class' => 'yii\db\Connection',
'dsn' => 'pgsql:host=OTHER_HOST;dbname=DB_NAME;port=PORT_CONNECTION',
'username' => 'DB_USERNAME',
'password' => 'DB_PASSWORD',
'charset' => 'utf8',
],];
Hope can help you

You have to configure another db connection in your web.php inside the config folder:
'db2'=>[
'class'=>'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=dbname',
'username' => 'root',
'password' => '',
'charset' => 'utf8'
],
and then you call db2 component.

Related

How to connect CakePHP 3.4 with remote SQL database

I need to connect the CakePHP 3.4 application with the SQL database. Already application has a connection with MySQL database, but need to connect to this SQL database for other purposes.
It's a simple no extra configuration.
But check that hosting/MySQL provider allows wildcard entry. mean host other than the server can connect to your MySQL, so confirm that wildcard/prefer domain is enabled.
in your config.php
'Datasources' => [
'default' => [
'className' => Connection::class,
'driver' => Mysql::class,
'persistent' => false,
'host' => 'yourhost.com',
'username' => 'username_of_host',
'password' => 'password_of_host',
'database' => '**************',
'timezone' => 'UTC',
'flags' => [],
'cacheMetadata' => true,
'log' => false,
'quoteIdentifiers' => false,
'url' => env('DATABASE_URL', null),
.......................
]
],

How to allow my user to use their own external SMTP in Yii2

I have an app where I want my user to be able to add their own SMTP server, whether by Mailgun, Amazon SES, etc.
If we're taking the mailgun as an example, right now my Mailgun is set up in config/web.php like so
'mailgun' => [
'class' => 'boundstate\mailgun\Mailer',
'key' => 'MYKEY',
'domain' => 'DOMAIN',
],
Then I use the following to compose an email
Yii::$app->mailgun->compose()->setFrom($FROM)
->setReplyTo($contest_creator_email)
->setTo($email)
->setSubject($subject_line)
->setTextBody($plaintext)
->setHtmlBody($htmlemail)
->send();
How would I make it so that my user could set-up his own key instead of using mine? Is there a way to do this?
You can do this by setting mailer configuration.
Before sending email you can set mailer configuration in your api/controller.
//Set config value dynamicaly
Yii::$app->set('mailer', [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'YourHostName',
'username' => 'UserName',
'password' => 'Password',
'port' => 'Port',
'encryption' => 'Encryption'
],
]);
And send mail as below.
Yii::$app->mailer->compose()->setFrom($FROM)
->setReplyTo($contest_creator_email)
->setTo($email)
->setSubject($subject_line)
->setTextBody($plaintext)
->setHtmlBody($htmlemail)
->send();

How to set access rights for files uploaded to s3 server using fedemotta Yii2 extension?

I can properly upload files to my s3 bucket using fedemotta yii2-aws-sdk extension. I cannot access the files because I have to set access rights to each file I upload.
My config looks something like this:
'components' => [
'awssdk' => [
'class' => 'fedemotta\awssdk\AwsSdk',
'credentials' => [
'key' => 'ZXCV',
'secret' => 'zxcv',
],
'region' => 'us-east-1',
'version' => 'latest',
],
I found out that in the default yii2-file-upload extension, it can be done using setACL('public-read'). How do I do this in fedemotta extension?
Solved.
Just add 'ACL' => 'public-read', to your upload function in the model
$this->s3->putObject(array(
'Bucket' => $bucket,
'Key' => $keyname,
'SourceFile' => $filepath,
'ACL' => 'public-read',
));`

Encrypt Cakephp - Decrypt with vb.net and viceversa

I currently have the following configuration for Authentication component in CakePHP
public $components = array(
'Session',
'Auth' => array(
'authError' => 'Please login to your account',
'logoutRedirect' => array(
'controller' => 'pages',
'action' => 'display',
'home'
),
'authenticate' => array(
'Form' => array(
'passwordHasher' => array(
'className' => 'Simple',
'hashType' => 'sha256'
)
)
),
'authorize' => array('Controller') // Added this line
)
);
And my work is integrated with vb.net windows form application. Is there a way or an Authentication class that can be common between both vb.net & CakePHP?
Password hashing is an irreversible process. You cannot "decrypt" them.
If you want to use the same hashes for authentication in your vb.net code then use the same hashing algo to hash user provided plain password and then compare the hashes. When using password hasher with sha256, the hash is generated by appending security salt to the plain text string and then the resulting string is sha256 hashed. So do the same in your vb.net code.

Yii Parameterized Hostnames. what am I missing?

I am not able to get the yii parameterized hostnames working. I am trying to display http://member.testsite.com when the user clicks on login from http://www.testsite.com.
I have the member module created with SiteController.
In my rules, I have:
'rules' => array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'http://member.testsite.com' => 'member/site/index',
),
In my login, I have the URL pointing to
'url'=>array('member/site/index')
When I hover over login, it shows member.testsite.com, but when I click it takes me to website-unavailable.com
When I change the rules to
'http://member.testsite.com' => 'member/<controller>/<action>',
it takes me to testsite.com/member/site/index/
Am I missing any step?
Example:
'rules' => array(
'://<member:\w+>.<host>/<controller:\w+>/<action:\w+>'
=> '<controller>/<action>',
),