How to debug flysystem - yii2

I install Yii 2 File kit extendsion, and set local file storage success.
Then i config SFTP then run command
Yii::$app->fileStorage1->getFilesystem()->createDir('demo')
So it not work. and only show result false.
So i don't know what the error.
I try conffig:
'fileStorage1' => [
'class' => '\trntv\filekit\Storage',
//'baseUrl' => '#storageUrl1',
//'filesystemComponent' => 'fs'
'filesystemComponent' => 'sftpFs'
],
'sftpFs' => [
'class' => 'creocoder\flysystem\SftpFilesystem',
'host' => 'Myserver',
'port' => 22,
'username' => 'u',
'password' => 'p',
//'privateKey' => '/path/to/or/contents/of/privatekey',
// 'timeout' => 60,
'root' => 'public_html',
// 'permPrivate' => 0700,
// 'permPublic' => 0744,
],

Related

Yii2 module or controller routing

I try use routing for module and controller
'<controller>'=>'<controller>/index',
'<controller>/<id:\d+>'=>'<controller>/view',
'<controller>/<action>'=>'<controller>/<action>',
'<module>' => '<module>/default/index',
'<module>/<id:\d+>' => '<module>/default/view',
'<module>/edit/<id:\d+>' => '<module>/default/edit',
'<module>/delete/<id:\d+>' => '<module>/default/delete',
'<module>/<action>' => '<module>/default/<action>',
'<module>/<controller>' => '<module>/<controller>/index',
'<module>/<controller>/edit/<id:\d+>' => '<module>/<controller>/edit',
'<module>/<controller>/delete/<id:\d+>' => '<module>/<controller>/delete',
'<module>/<controller>/<id:\d+>' => '<module>/<controller>/view',
'<module>/<controller>/<action>' => '<module>/<controller>/<action>',
but for some reason, if the first matching rule did not work, then the rest are not checked, you get a 404 error.
For example, if the controller is not found, then 404 is obtained, and the check does not reach the modules.
url manager config
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'store/upload'=>'store/upload',
'stores'=>'store/index',
// 'post/<id:\d+>'=>'post/view',
'comment/send'=>'comment/send',
['class' => 'yii\rest\UrlRule', 'controller' => 'Api', 'prefix' => 'api'],
'api/<id:\d+>'=>'api/view',
[
'pattern' => 'users',
'route' => 'users/index',
'suffix' => '/',
'normalizer' => false, // отключаем нормализатор для этого правила
],
// '<controller>'=>'<controller>/index',
// '<controller>/<id:\d+>'=>'<controller>/view',
// '<controller>/<action>'=>'<controller>/<action>',
'<module>' => '<module>/default/index',
'<module>/<id:\d+>' => '<module>/default/view',
'<module>/edit/<id:\d+>' => '<module>/default/edit',
'<module>/delete/<id:\d+>' => '<module>/default/delete',
'<module>/<action>' => '<module>/default/<action>',
'<module>/<controller>' => '<module>/<controller>/index',
'<module>/<controller>/edit/<id:\d+>' => '<module>/<controller>/edit',
'<module>/<controller>/delete/<id:\d+>' => '<module>/<controller>/delete',
'<module>/<controller>/<id:\d+>' => '<module>/<controller>/view',
'<module>/<controller>/<action>' => '<module>/<controller>/<action>',
],
],
Request Url http://yii2/post
To PostController/actionIndex()
I fix routing, It work.The controllers are checked first, then the modules.
'<controller:\w+>'=>'<controller>/index',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action>'=>'<controller>/<action>',
'<module:\w+>' => '<module>/default/index',
'<module:\w+>/<id:\d+>' => '<module>/default/view',
'<module:\w+>/edit/<id:\d+>' => '<module>/default/edit',
'<module:\w+>/delete/<id:\d+>' => '<module>/default/delete',
'<module:\w+>/<action:\w+>' => '<module>/default/<action>',
'<module:\w+>/<controller:\w+>' => '<module>/<controller>/index',
'<module:\w+>/<controller:\w+>/edit/<id:\d+>' => '<module>/<controller>/edit',
'<module:\w+>/<controller:\w+>/delete/<id:\d+>' => '<module>/<controller>/delete',
'<module:\w+>/<controller:\w+>/<id:\d+>' => '<module>/<controller>/view',
'<module:\w+>/<controller:\w+>/<action>' => '<module>/<controller>/<action>',

How to change db name for yii2 Query Builder

iam using multiple databases for my yii2 project.In my config file, I have the configuration like
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=db',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'tablePrefix' => 'tbl_',
],
'db2' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=db2',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'tablePrefix' => 'tbl_',
],
And i have a query like
$query = (new \yii\db\Query())
->select(['A.id', 'A.category_english', 'COUNT(B.category_id)'])
->from(['A' => 'tbl_categories', 'B' => $filteredData])
->where('A.id = B.category_id')
->groupBy(['A.id', 'A.category_english'])
->orderBy(['COUNT(B.category_id)'=>SORT_DESC]);
$data = $query->all();
The above query is fetching details from the first DB. How can I change that to the second? Where I can set the db name in query builder.

How can I add another database in my laravel project in heroku

I have my laravel project deployed in heroku, but I only migrated the database that I have created eg: users, news. But I have another database that is not migrated from my laravel project. Meaning, it is a existing database and I'm only connecting it on my project. In my development stage, I can connect the second database using my codes below. But now, I will deploy my project in heroku and I dont know how can I connect the second database because in heroku's postgresSQL you can only create and migrate the database based on the migration folder in laravel. I dont know how to upload an sql file in heroku's postgresSql and in that way I can connect the second database using the codes below. Is this possible in heroku? Because the second database is important in my landing page. It includes some few select query.
Here are some of my codes including the connection of the second database.
.env
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:HoQcNyCc5KEGw4yjqpBIdKzTC+yeDoOJcerVMEVx+fs=
APP_DEBUG=true
APP_URL=http://localhost
LOG_CHANNEL=stack
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=adminpanel
DB_USERNAME=root
DB_PASSWORD=
DB_CONNECTION_SECOND=mysql2
DB_HOST_SECOND=127.0.0.1
DB_PORT_SECOND=3306
DB_DATABASE_SECOND=ricjac8_orocoin
DB_USERNAME_SECOND=root
DB_PASSWORD_SECOND=
database.php
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'url' => env('DATABASE_URL'),
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
],
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
'mysql2' => [
'driver' => 'mysql',
'host' => env('DB_HOST_SECOND'),
'port' => env('DB_PORT_SECOND'),
'database' => env('DB_DATABASE_SECOND'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD','forge'),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
GraphController.php - The one that has select query from my other database
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use Illuminate\Support\Facades\DB;
use View;
use App\News;
use Charts;
use App\Graph;
use App\Roadmap;
class GraphController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$allroad = Roadmap::all();
$news = News::all();
//the second database query
$graphdetails = Graph::select()->where('id', 1)->get();
return view('coin.news',compact('news','graphdetails','allroad'));
}
}
graph.blade.php - the result of the select query being rendered in a chart
//other blade codes i didint include here.
#foreach ($graphdetails as $item)
#endforeach
chart.legend = new am4charts.Legend();
chart.data = [{
"tokens": "Sold Tokens",
"values": {{$item->total_tokens}} - {{$item->sales_token}}
},{
"tokens": "Unsold Tokens",
"values": {{$item->sales_token}}
}];
});
Graph.php model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Auth;
use Cache;
use Charts;
class Graph extends Model
{
//
protected $connection = 'mysql2';
protected $table = 'ico_stages';
protected $fillable = [
'name', 'start_date', 'end_date', 'total_tokens', 'base_price', 'min_purchase', 'max_purchase', 'soft_cap', 'hard_cap',
'display_mode','private','user_panel_display','sales_token','sales_amount','status',
];
}
Try another free shared hosting that uses MySQL. PostreSQL is a little bit different than MySQL.

Yii2 Mailer isn't actually sending emails

I followed a Yii2 tutorial for sending e-mails with attachment,
(See the link below if you need)
https://www.youtube.com/watch?v=c5pebmTUQjs&index=21&list=PLRd0zhQj3CBmusDbBzFgg3H20VxLx2mkF
And it worked on a level that the system reads the email info and the attachment that I want to send, and saves the info and attachment link in the db.
But when I tried to send emails to a real recipient and changed the mailer config, then tried to create a new email
there was a problem:
Invalid Configuration – yii\base\InvalidConfigException
Setting unknown property: Swift_MailTransport::host
Here is the config for mailer:
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#app/mail',
'useFileTransport' => false,
'transport' => [
'host' => 'smtp.live.com',
'username' => 'username#live.com',
'password' => 'password',
'port' => '587',
'encryption' => 'tls',
]
],
And here is the actionCreate part of the EmailsController:
public function actionCreate()
{
$model = new Emails();
if ($model->load(Yii::$app->request->post()))
{
$model->attachment= UploadedFile::getInstance($model,'attachment');
if ($model->attachment)
{
$time=time();
$model->attachment->saveAs ('attachments/' .$time.'.' .$model->attachment->extension);
$model->attachment='attachments/'.$time.'.'.$model->attachment->extension;
}
if ($model->attachment)
{
$value= Yii::$app->mailer->compose()
->setFrom (['sharqpress#hotmail.com'=>'Al-Sharq Printing Press'])
->setTo ($model->reciever_email)
->setSubject ($model->subject)
->setHtmlBody ($model->content)
->attach ($model->attachment)
->send();
}
else
{
$value= Yii::$app->mailer->compose()
->setFrom (['sharqpress#hotmail.com'=>'Al-Sharq Printing Press'])
->setTo ($model->reciever_email)
->setSubject ($model->subject)
->setHtmlBody ($model->content)
->send();
}
$model->save();
return $this->redirect(['view','id'=>$model->id]);}
else
return $this -> render('create',['model'=>$model,]);
}
Your configuration needs additional class:
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#app/mail',
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport', // <-- here
'host' => 'smtp.live.com',
'username' => 'username#live.com',
'password' => 'password',
'port' => '587',
'encryption' => 'tls',
]
],

Zend-F2: How to configure "Mysqli: The ext/mysqli driver"

I am using ZF v2.2. I am having a problem during database connectivity with "Mysqli: The ext/mysqli driver". Actually i am unable to figure out the process of "connecting to database & then using $sql->select(), $sql->update() etc".
Any idea how to configure this?
File: autoload/db.local.php
return array(
'db' => array(
'driver' => 'Mysqli',
'username' => 'root',
'password' => 'redhat',
'database' => 'mydb',
'host' => 'localhost'
),
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
),
'aliases' => array(
'db' => 'Zend\Db\Adapter\Adapter',
),
),
);
public function indexAction() {
$db=$this->getServiceLocator()->get('db'); ` `echo $db->getDriver()->getConnection()->isConnected();
//**returns false**.
}