My app that's using Yii 2 advanced template is not able to write into the app.log file. Both the frontend/runtime/logs folder and its contents (app.log file) have the www-data has the owner (user and group). I've tried setting full permissions 777 on both the folder and app.log file just to debug, but the app isn't still able to write to the log file.
This is my current config in /frontend/config/main.php
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
[
'class' => 'yii\log\EmailTarget',
'mailer' => 'mailer',
'levels' => ['error', 'warning'],
'message' => [
'from' => 'admin#mydomain.com',
'to' => ['logs#mydomain.com'],
'subject' => 'Error / Warning',
],
],
],
],
These are my permissions inside the runtime folder:
drwxrwxr-x 2 www-data www-data 4096 Jan 10 2018 debug
drwxrwxrwx 2 www-data www-data 4096 Apr 28 15:46 logs
and for the app.log file:
-rwxrwxrwx 1 www-data www-data 174248 Jan 10 2018 app.log
This is the error that I get when the app fails to write into the log file:
[warning][yii\log\Dispatcher::dispatch] Unable to send log via yii\log\FileTarget: Exception (Invalid
Configuration) 'yii\base\InvalidConfigException' with message 'Unable
to append to log file:
/var/www/html/my-yii2-advanced/frontend/runtime/logs/app.log'
in /var/www/html/my-yii2-advanced/vendor/yiisoft/yii2/log/FileTarget.php:109
Stack trace:
#0 /var/www/html/my-yii2-advanced/vendor/yiisoft/yii2/log/Target.php(133):
yii\log\FileTarget->export()
#1
/var/www/html/my-yii2-advanced/vendor/yiisoft/yii2/log/Dispatcher.php(189):
yii\log\Target->collect(Array, true)
#2 /var/www/html/my-yii2-advanced/vendor/yiisoft/yii2/log/Logger.php(177):
yii\log\Dispatcher->dispatch(Array, true)
#3
/var/www/html/my-yii2-advanced/vendor/yiisoft/yii2/base/ErrorHandler.php(113):
yii\log\Logger->flush(true)
#4 [internal function]:
yii\base\ErrorHandler->handleException(Object(yii\web\NotFoundHttpException))
#5 {main}
The issue happened while compiling the container's image using Docker. Basically, the permissions were being overridden and the owner wasn't "www-data". I just had to add the proper owner permissions to "www-data" in the Dockerfile.
Related
My Yii2 app is emailing me these errors.
2021-08-15 21:23:54
[192.228.155.xxx][-][a217lked49000ii06qtrivbrnl][error][yii\web\HttpException:400]
yii\web\BadRequestHttpException: Unable to verify your data
submission. in
/var/www/html/vendor/yiisoft/yii2/web/Controller.php:218
Stack trace:
#0 /var/www/html/vendor/yiisoft/yii2/base/Controller.php(179):
yii\web\Controller->beforeAction()
#1 /var/www/html/vendor/yiisoft/yii2/base/Module.php(534):
yii\base\Controller->runAction()
#2 /var/www/html/vendor/yiisoft/yii2/web/Application.php(104):
yii\base\Module->runAction()
#3 /var/www/html/vendor/yiisoft/yii2/base/Application.php(392):
yii\web\Application->handleRequest()
#4 /var/www/html/frontend/web/index.php(64):
yii\base\Application->run()
#5 {main}
2021-08-15 21:23:54
[192.228.155.xxx][-][a217lked49000ii06qtrivbrnl][info][application]
$_POST = [
'_csrf'=>
'pejM1w_BV9Xj_HRgo9AsjsNW130aTEzqqx61C1PXFyLuvfWvX7d6oLKMJgzanWfenDi8KSopC66ab9JqIadzDw=='
'calculatorForm' => [
'instype' => '1'
'txtSumInsuredMtr' => '10,001'
'CCMtr' => '201.3'
'txtBasicPremiumSub' => ''
'NCDMtr' => ''
'txtBasicPremiumTotalMotor' => ''
'rider' => '1'
'stampDutyMotor' => ''
]
'ajax' => 'calculatorForm'
]
I have no idea why. Line 64 in index.php is this
(new yii\web\Application($config))->run();
The post request in the log is a calculator form. it works fine when i test it. Any ideas?
I am creating a module with the following structure:
common
L modules
LL blog
LLL backend
LLL frontend
LLL common
LLL migrations
I found in yii2 documentation a section about "Separated Migrations"
In console/config/main.php I have set:
'migrate-blog' => [
'class' => 'yii\console\controllers\MigrateController',
'migrationNamespaces' => ['app\common\modules\blog\migrations'],
'migrationTable' => 'migration_blog',
'migrationPath' => null,
]
Then I go to console and run following command:
php yii migrate/create app\\common\\modules\\blog\\migrations\\create_table_blog_post
It returns an error:
Error: Namespace 'app\common\modules\blog\migrations' not found in `migrationNamespaces`
am I missing any settings?
Did you add the following info to config of console.php
'controllerMap' => [
// Migrations for the specific project's module
'migrate-module' => [
'class' => 'yii\console\controllers\MigrateController',
'migrationNamespaces' => ['app\module\migrations'],
'migrationTable' => 'migration_module',
'migrationPath' => null,
],
],
I have seen that you have the config in console/config/main.php then the check the yii file is having the following line.
$config = require(__DIR__ . '/console/config/main.php');
After this instead of running
php yii migrate/create app\\common\\modules\\blog\\migrations\\create_table_blog_post
Run the following command
php yii/migrate-blog/create create_table_blog_post
I hope this helps.
This question already has an answer here:
Override Yii2 Swiftmailer Recipient
(1 answer)
Closed 5 years ago.
During the development and testing, I want the Yii2 mailer (swiftmailer) not to send emails to actual addresses, but replace it with developers` emails. Is there any setting in config to do it?
Just set the useFileTransport to true in component configuration for the development environment and all emails will not be sent but will be saved as files so you can easily test everything.
You should set configs for prod and dev environment.
Path for dev config (if you have advanced application template): yourProject/environments/dev/common/config/main-local.php and for prod: yourProject/environments/prod/common/config/main-local.php.
You may use EmailTarget class for logs.
Example of config for dev environment to reach the clue:
return [
'bootstrap' => ['log'],
'components' => [
...
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
'viewPath' => '#common/mail',
'useFileTransport' => true,//set this property to false to send mails to real email addresses
//comment the following array to send mail using php's mail function
],
'log' => [
'targets' =>
[
[
'class' => 'yii\log\EmailTarget',
'levels' => ['error'],
'except' => ['yii\web\HttpException:404'],
'message' => [
'to' => ['example#mail.ru'],
'from' => ['yourproject#mail.ru'],
'subject' => ' Errors ',
]
],
],
],
],
];
And similarly for prod environment but with different emails.
If you don't want to use EmailTarget class you may just set your emails for dev in params config here: yourProject/environments/dev/common/config/params-local.php.
And path for prod: yourProject/environments/prod/common/config/params-local.php
Params config example:
return [
'sendToEmails' => ['email1#mail.ru', 'email2#mail.ru']
];
And then you may use the variable in your code this way: Yii::$app->params['sendToEmails'] to get the array of emails to send messages to.
Don't forget to do php init command in your project after compliting your configs.
You may see the detailed docs about environments here.
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!
I need to install an app written in Yii 2 on my local computer. I installed composer and initiated the app with:
php /path/to/yii-application/init
Now I need to "create a new database and adjust the components['db'] configuration in common/config/main-local.php accordingly."
I have no clue how to do that.
1) Create database on your server.
2) Open common/config/main-local.php
Edit components to:
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=DATABASE_NAME',
'username' => 'DATABASE_USER',
'password' => 'DATABASE_PASSWORD',
'charset' => 'utf8',
],
If using MAMP with mac then edit dsn line to :
'dsn' => 'mysql:unix_socket=/Applications/MAMP/tmp/mysql/mysql.sock;dbname=DATABASE_NAME'