Undefined type 'kartik\slider\Slider'.intelephense(1009) - yii2

I have a problem with my Slider::widget
use kartik\slider\Slider;
.
.
.
<?=Slider::widget([
'name' => 'price',
'value' => isset($_GET['price']) ? $_GET['price'] : '0,2000',
'sliderColor' => "#2583ee",
'handleColor' => "#2583ee",
'pluginOptions' =>[
'min' => 0,
'max' => 2000,
'step' => 5,
'range' => true,
'tooltip' => 'always'
],
]);?>
I already installed kartik slider using composer
$ php composer.phar require kartik-v/yii2-slider "dev-master"
But it's sill showing me
Undefined type 'kartik\slider\Slider'.intelephense(1009)
I made sure that's in the composer.json
But it's still showing the error

Related

Yii2 Failing to instantiate component or class "db"

I use a Yii2 console application to run migrations. It's a very basic app, that goes like this (yii.php in the / folder of the project):
<?php
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/vendor/yiisoft/yii2/Yii.php';
$config = require __DIR__ . '/config/console.php';
(new yii\console\Application($config))->run();
?>
So when i run
php yii.php
Everything's fine, but when i run
php yii.php migrate/create create_user_table
I am getting an error message:
Error: Failed to instantiate component or class "db".
My Yii is v2.0.15.1
UPD 19:32 30/12/2018
When I add a db config to a config/console.php like this:
return [
'id' => 'school-console',
'basePath' => dirname(__DIR__),
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=school',
'username' => 'root',
'password' => 'Taras1love',
'charset' => 'utf8',
]
];
I get this:
Error: Setting read-only property: yii\console\Application::db
You are missing the database component configurations for the console application you need to add the following inside the config/console.php file. As you are usign the basic-app for Yii2 you must have a db.php file with the database configurations, you need to include it like below
//this goes on the top of your `console.php` file
$db = require __DIR__ . '/db.php',
return [
'id' => 'myapp-console',
'basePath' => dirname(__DIR__)
//add the db component
'components' => [
'db' => $db,
]
];
Your db.php should be like below inside the config folder, change the values for the username, password and dbname.
<?php
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=YOUR_DB_NAME',
'username' => 'DB_USER',
'password' => 'DB_PASS',
'charset' => 'utf8',
];
or you can assign it inside the config/console.php if you dont want to create a separate file
return [
'id' => 'myapp-console',
'basePath' => dirname(__DIR__)
//add the db component
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=YOUR_DB_NAME',
'username' => 'DB_USER',
'password' => 'DB_PASS',
'charset' => 'utf8',
]
]
];
I found that I got this problem if I ran yii directly from vendor/bin.
If I went to the console directory and ran it from there using ./yii, I did not get this error and was able to create the migration.
In other words:
cd <project-root>/vendor/bin
yii migrate/create xxx
did not work
But:
cd <project-root>/console
./yii migrate/create xxx
did work
this happened to me because I accidentally renamed my migration and it was not matching the file name

Run Codeception API Test with Yii2

I have been fighting with this problem for hours and cannot get through. I want to run API tests with Yii2 and (of course) Codeception. Here is my api.suite.yml
class_name: ApiTester
modules:
enabled:
- REST:
url: /mobile
depends: Yii2
part: Json
- \Helper\Api
config:
Yii2:
entryUrl: http://localhost:8080/index-test.php
and my test file UserLoginCept.php
<?php
$I = new ApiTester($scenario);
$I->wantTo('Test User Login');
$I->sendPOST('mobile/login', ['username' => 'uname', 'password' => '123456']);
$I->seeResponseCodeIs(\Codeception\Util\HttpCode::OK);
$I->seeResponseContainsJson(['success'=>true]);
Results are logged below. The problem is the Test is calling site/index which is in the root project not mobile module. I can sense that it is picking wrong URL somewhere as I cannot see any trace of the module being called. If I try URL on Browser it works fine
http://localhost:8080/index.php/mobile/api/login
{
"success": false,
"token": ""
}
can someone help me spot what am doing wrong? I have read as much as I could could not find the issue.
Codeception Results
$~ codecept --debug run api
Codeception PHP Testing Framework v2.2.10
Powered by PHPUnit 4.8.35 by Sebastian Bergmann and contributors.
Rebuilding ApiTester...
Api Tests (1) -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Modules: REST, Yii2, \Helper\Api
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
UserLoginCept: Test User Login
Signature: UserLoginCept
Test: tests/api/UserLoginCept.php
Scenario --
I send post "/mobile/api/login",{"username":"uname","password":"123456"}
[Request] POST /mobile/mobile/api/login {"username":"uname","password":"123456"}
[Request Headers] []
[yii\db\Connection::open] 'Opening DB connection: mysql:host=localhost;dbname=database_name'
ERROR
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1) UserLoginCept: Test user login
Test tests/api/UserLoginCept.php
[Error] Call to a member function isAdmin() on null
Scenario Steps:
1. $I->sendPOST("/mobile/api/login",{"username":"uname","password":"123456"}) at tests/api/UserLoginCept.php:4
#1 /Users/hosanna/Projects/Volcano/WebApp/vendor/yiisoft/yii2/base/View.php:328
#2 /Users/hosanna/Projects/Volcano/WebApp/vendor/yiisoft/yii2/base/View.php:250
#3 /Users/hosanna/Projects/Volcano/WebApp/vendor/yiisoft/yii2/base/Controller.php:396
#4 /Users/hosanna/Projects/Volcano/WebApp/vendor/yiisoft/yii2/base/Controller.php:382
#5 /Users/hosanna/Projects/Volcano/WebApp/controllers/SiteController.php:74
#6 app\controllers\SiteController->actionIndex
#7 /Users/hosanna/Projects/Volcano/WebApp/vendor/yiisoft/yii2/base/InlineAction.php:57
#8 /Users/hosanna/Projects/Volcano/WebApp/vendor/yiisoft/yii2/base/Controller.php:156
#9 /Users/hosanna/Projects/Volcano/WebApp/vendor/yiisoft/yii2/base/Module.php:523
#10 /Users/hosanna/Projects/Volcano/WebApp/vendor/yiisoft/yii2/web/Application.php:102
<!DOCTYPE html>
<html lang="en-US">
..... rest of HTML.....
So here is how I solved it:
changed suite.api.yaml to use test-index.php
class_name: ApiTester
modules:
enabled:
- Yii2
- REST:
url: http://localhost:8080/index-test.php/mobile/
depends: Yii2
part: Json
configFile: 'config/test.php'
- \Helper\Api
config:
Yii2:
I then changed the config file referred by text-index (config/test.php) to include pretty URLs:
<?php
$params = require(__DIR__ . '/params.php');
$dbParams = require(__DIR__ . '/test_db.php');
/**
* Application configuration shared by all test types
*/
return [
'id' => 'basic-tests',
'basePath' => dirname(__DIR__),
'language' => 'en-US',
'modules' => [
'mobile' => [
'class' => 'app\modules\mobile\Module',
],
],
'components' => [
'db' => $dbParams,
'mailer' => [
'useFileTransport' => true,
],
'assetManager' => [
'basePath' => __DIR__ . '/../web/assets',
],
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => false,
'showScriptName' => true,
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => 'mobile/api'],
],
],
'user' => [
'identityClass' => 'app\modules\mobile\models\User',
],
'request' => [
'cookieValidationKey' => 'test',
'enableCsrfValidation' => false,
// but if you absolutely need it set cookie domain to localhost
/*
'csrfCookie' => [
'domain' => 'localhost',
],
*/
],
],
'params' => $params,
];
After that tests were running fine!

Send message over SMTP using Swiftmailer and Yii2

I need some advice over the following case:
I have configured the config/web.php file as follows
'components' => [
...
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'useFileTransport' => false,
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 'myusername',
'password' => 'mypassword',
'port' => '587',
'encryption' => 'tls',
],
],
Also in the Controller:
$message = Swift_Message::newInstance()
->setSubject('...')
->setFrom(['mymail'])
->setTo(['recipient'])
->setBody('......');
$transport = Swift_SmtpTransport::newInstance();
$mailer = Swift_Mailer::newInstance($transport);
$mailer->send($message);
I get the Error: 'Connection could not be established with host ' (Swift_TransportException)
Any ideas? Thank you.
Since you have got this configured as Yii component use it properly.
See documentation example:
Yii::$app->mailer->compose('contact/html', ['contactForm' => $form])
->setFrom('from#domain.com')
->setTo($form->email)
->setSubject($form->subject)
->send();
Also: see this SO question.
In your config set it as
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => 'yourusername',
'password' => 'yourpassword',
'port' => '587',
'encryption' => 'tls',
]
],
In your controller you can compose the email.
Yii::$app->mailer->compose('#app/mail/layouts/reset',['model' =>$model])
->setTo('to#example.com')
->setFrom('from#example.com')
->setSubject('Subject Here')
->setTextBody('You can either render the layout or declare your body here')
->send();
In the above code I am using layouts. You can use layouts and pass variables to the layout and use them. If you dont want to use layouts that works as well just call the compose method
Yii::$app->mailer->compose()
I did the following and worked:
$transport = Swift_SmtpTransport::newInstance(Yii::$app->components['mailer']['transport']['host'], Yii::$app->components['mailer']['transport']['port']);
$transport->setUsername(Yii::$app->components['mailer']['transport']['username']);
$transport->setPassword(Yii::$app->components['mailer']['transport']['password']);

Codeigniter showing error: No database selected

I am using Codeigniter's DBForge class to create a database and table within that database.
Here is code:
if ($this->dbforge->create_database('new_db')) {
$fields = array(
'blog_id' => array(
'type' => 'INT',
'constraint' => 5,
'unsigned' => TRUE,
'auto_increment' => TRUE
),
'blog_title' => array(
'type' => 'VARCHAR',
'constraint' => '100',
),
'blog_author' => array(
'type' => 'VARCHAR',
'constraint' => '100',
'default' => 'King of Town',
),
'blog_description' => array(
'type' => 'TEXT',
'null' => TRUE,
),
);
$this->dbforge->add_field($fields);
$this->dbforge->add_key('blog_id', TRUE);
$this->dbforge->create_table('blog', TRUE);
}
The above mentioned code is written inside index function of controller. When the following code is executed, it shows error as, No database selected. Does anyone have ant idea why it is happening. Any help is appreciated.
You need to specify
$db['default']['database'] = "new_db";
in database.php
Just refer to database.php file inside application/config folder. Make sure you have proper values for database name and other things in the following lines present in database.php file
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'your username', //By default it is root
'password' => 'your password', //By default this field is empty
'database' => 'database name',//The error that you are getting is because of this only. Specify your database name here
'dbdriver' => 'mysqli',
Hope it helps
If you have set the default database in config/database.php and it is still not working, you probably should save the file in case you forget to do that.

Codeigniter Migrations + Multiple Tables + One Migration File Per Table

I want to begin using DBForge and migrations class thats built into CI but im not sure how to go about creating migrations for all of my tables.
My thoughts are in my installer process to have a migration file for each of the following tables: advertisements, announcements, config, users, points.. When the user is installing the app, it will automatically run through these migration files and create the tables.
IE: 001_advertisements, 001_announcements, 001_config, 001_users, 001_points
001_map_advertisements
class Migration_map_advertisements extends CI_Migration {
public function up(){
$this->dbforge->add_field(array(
'id' => array(
'type' => 'INT',
'constraint' => 11,
'unsigned' => TRUE,
'auto_increment' => TRUE
),
'youtube_id' => array(
'type' => 'VARCHAR',
'constraint' => '255',
),
'status' => array(
'type' => 'int',
'constraint' => 11,
'null' => FALSE,
'default' => 1
),
'timestamp' => array(
'type' => 'int',
'constraint' => 11
),
'type' => array(
'type' => 'VARCHAR',
'default' => 'video'
),
'filename' => array(
'type' => 'VARCHAR',
'constraint' => '255'
),
'url' => array(
'type' => 'varchar',
'constraint' => '255'
),
'description' => array(
'type' => 'varchar',
'constraint' => 64
),
'title' => array(
'type' => 'varchar',
'constraint' => 64
)
));
$this->dbforge->create_table('map_advertisements', TRUE);
}
public function down()
{
$this->dbforge->drop_table('map_advertisements');
}
However if i do this and attempt to run a secured controller:
class Developer extends ADMIN_Controller {
function __construct(){
parent::__construct();
} #end constructor function
public function migrate($index){
$this->load->library('migration');
if ( !$this->migration->version($index) ){
show_error($this->migration->error_string());
}else{
echo 'migrated';
}
}
}
i get an error stating that there are multiple version 1 migrations and it fails the migration process. Is there another way to go about this in CI?
Yes, you can only have one migration file per version. A migration is not table-specific, but more like "what schema changes are needed to go from version x to version x+1 of my app?"
Combine all of your existing migration files into 1, and name it something like 001_initial_schema.php. Then, when add a new feature, create a new schema file for that feature. If you have a deployment cycle (like weekly SCRUM sprints), it's nice to have one of these migration files per deployment.
According to http://www.codeigniter.com/user_guide/libraries/migration.html, the best way to "version" your migrations is using the YYYYMMDDHHMMSS format. This should effectively overcome versioning issues as expressed. It is also suggested that if the the filename was "201507071208_advertisements.php" that the contained migration class should be called "Migration_Advertisements".