getting HttpException:400 in log files - yii2

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?

Related

Yii2 queue TTR attribute doesn't applies

I have the following queue config:
'queue' => [
'class' => \yii\queue\db\Queue::class,
'db' => 'db',
'tableName' => '{{%queue}}',
'channel' => 'default',
'mutex' => \yii\mutex\MysqlMutex::class,
'ttr' => 14400,
],
I wrote simple job that outputs current time in the console.
use yii\queue\JobInterface;
use yii\queue\Queue;
class DummyTimer implements JobInterface
{
public function run()
{
$strtime = time();
while($strtime + (4*60*60) > time())
echo date('H:i:s') . "\r\n";
}
public function execute($queue)
{
$this->run();
}
}
It runs only 300 seconds, and then gets killed with the next output in terminal:
2020-05-27 23:29:51 [8] app\modules\queue\DummyTimer (attempt: 1, pid:
7637) - Error
Symfony\Component\Process\Exception\ProcessTimedOutException: The process "'/usr/bin/php7.2' 'yii' 'queue/exec' '8' '300' '1' '7637'
'--color=1' '--verbose=1'" exceeded the timeout of 300 seconds.
So how to increase maximum lifetime of a job?
The original solution should be working just fine:
'queue' => [
'class' => \yii\queue\db\Queue::class,
'db' => 'db',
'tableName' => '{{%queue}}',
'channel' => 'default',
'mutex' => \yii\mutex\MysqlMutex::class,
'ttr' => 14400,
],
Stumbled upon the same issue and seemed that ttr in queue config did not work. But What I forgot to do was to re-start the queue process after changing the config. Works just fine with the configuration change if the queue is properly re-loaded with the new configuration.

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!

Unable to run console command yii2

I am trying to run yii but unable to this. I am getting following error:
commond : php yii
Exception 'yii\base\UnknownPropertyException' with message 'Setting unknown property: yii\console\Request::parsers'
in /var/www/html/alpha/html/vendor/yiisoft/yii2/base/Component.php:201
Stack trace:
#0 /var/www/html/alpha/html/vendor/yiisoft/yii2/BaseYii.php(529): yii\base\Component->__set('parsers', Array)
#1 /var/www/html/alpha/html/vendor/yiisoft/yii2/base/Object.php(105): yii\BaseYii::configure(Object(yii\console\Request), Array)
#2 [internal function]: yii\base\Object->__construct(Array)
#3 /var/www/html/alpha/html/vendor/yiisoft/yii2/di/Container.php(381): ReflectionClass->newInstanceArgs(Array)
#4 /var/www/html/alpha/html/vendor/yiisoft/yii2/di/Container.php(156): yii\di\Container->build('yii\\console\\Req...', Array, Array)
#5 /var/www/html/alpha/html/vendor/yiisoft/yii2/BaseYii.php(348): yii\di\Container->get('yii\\console\\Req...', Array, Array)
#6 /var/www/html/alpha/html/vendor/yiisoft/yii2/di/ServiceLocator.php(135): yii\BaseYii::createObject(Array)
#7 /var/www/html/alpha/html/vendor/yiisoft/yii2/console/Application.php(219): yii\di\ServiceLocator->get('request')
#8 /var/www/html/alpha/html/vendor/yiisoft/yii2/base/Application.php(380): yii\console\Application->getRequest()
#9 /var/www/html/alpha/html/yii(31): yii\base\Application->run()
#10 {main}
parsers property is available in yii\web\Request, not in yii\console\Request.
Probably you have got your console configuration merged with web configuration where this property is set. If so, make sure console configuration is set with separate request component setup. If this is not the case of configs merge remove this property in console configuration.
It`s work for my issue:
Edit file : console/config/main.php
'components' => [
'log' => [
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'request' => [
'parsers' => new \yii\helpers\UnsetArrayValue(), //Add this line
],
],

Yii2 Errors/No view using AccessControl "as beforeRequest" with custom roles

I use following code:
'as beforeRequest' => [
'class' => 'yii\filters\AccessControl',
'rules' => [
[
'actions' => ['login', 'forgot', 'error'], // guests can just login and nothing else
'allow' => true,
'roles' => ['?'],
],
[
'allow' => true,
'roles' => ['admin', 'access'],
],
],
],
Using this code causes following errors (if a user is logged in, but have not the role admin or access):
An Error occurred while handling another error:
yii\web\ForbiddenHttpException: Sie dürfen diese Aktion nicht durchführen. in /Applications/XAMPP/xamppfiles/htdocs/cms/vendor/yiisoft/yii2/filters/AccessControl.php:154
Stack trace:
0 /Applications/XAMPP/xamppfiles/htdocs/cms/vendor/yiisoft/yii2/filters/AccessControl.php(137): yii\filters\AccessControl->denyAccess(Object(yii\web\User))
1 /Applications/XAMPP/xamppfiles/htdocs/cms/vendor/yiisoft/yii2/base/ActionFilter.php(75): yii\filters\AccessControl->beforeAction(Object(yii\web\ErrorAction))
2 [internal function]: yii\base\ActionFilter->beforeFilter(Object(yii\base\ActionEvent))
3 /Applications/XAMPP/xamppfiles/htdocs/cms/vendor/yiisoft/yii2/base/Component.php(545): call_user_func(Array, Object(yii\base\ActionEvent))
4 /Applications/XAMPP/xamppfiles/htdocs/cms/vendor/yiisoft/yii2/base/Module.php(676): yii\base\Component->trigger('beforeAction', Object(yii\base\ActionEvent))
5 /Applications/XAMPP/xamppfiles/htdocs/cms/vendor/yiisoft/yii2/base/Controller.php(144): yii\base\Module->beforeAction(Object(yii\web\ErrorAction))
6 /Applications/XAMPP/xamppfiles/htdocs/cms/vendor/yiisoft/yii2/base/Module.php(523): yii\base\Controller->runAction('error', Array)
7 /Applications/XAMPP/xamppfiles/htdocs/cms/vendor/yiisoft/yii2/web/ErrorHandler.php(97): yii\base\Module->runAction('site/error')
8 /Applications/XAMPP/xamppfiles/htdocs/cms/vendor/yiisoft/yii2/base/ErrorHandler.php(111): yii\web\ErrorHandler->renderException(Object(yii\web\ForbiddenHttpException))
9 [internal function]: yii\base\ErrorHandler->handleException(Object(yii\web\ForbiddenHttpException))
10 {main}
Previous exception:
yii\web\ForbiddenHttpException: Sie dürfen diese Aktion nicht durchführen. in /Applications/XAMPP/xamppfiles/htdocs/cms/vendor/yiisoft/yii2/filters/AccessControl.php:154
Stack trace:
0 /Applications/XAMPP/xamppfiles/htdocs/cms/vendor/yiisoft/yii2/filters/AccessControl.php(137): yii\filters\AccessControl->denyAccess(Object(yii\web\User))
1 /Applications/XAMPP/xamppfiles/htdocs/cms/vendor/yiisoft/yii2/base/ActionFilter.php(75): yii\filters\AccessControl->beforeAction(Object(yii\base\InlineAction))
2 [internal function]: yii\base\ActionFilter->beforeFilter(Object(yii\base\ActionEvent))
3 /Applications/XAMPP/xamppfiles/htdocs/cms/vendor/yiisoft/yii2/base/Component.php(545): call_user_func(Array, Object(yii\base\ActionEvent))
4 /Applications/XAMPP/xamppfiles/htdocs/cms/vendor/yiisoft/yii2/base/Module.php(676): yii\base\Component->trigger('beforeAction', Object(yii\base\ActionEvent))
5 /Applications/XAMPP/xamppfiles/htdocs/cms/vendor/yiisoft/yii2/base/Controller.php(144): yii\base\Module->beforeAction(Object(yii\base\InlineAction))
6 /Applications/XAMPP/xamppfiles/htdocs/cms/vendor/yiisoft/yii2/base/Module.php(523): yii\base\Controller->runAction('', Array)
7 /Applications/XAMPP/xamppfiles/htdocs/cms/vendor/yiisoft/yii2/web/Application.php(102): yii\base\Module->runAction('', Array)
8 /Applications/XAMPP/xamppfiles/htdocs/cms/vendor/yiisoft/yii2/base/Application.php(380): yii\web\Application->handleRequest(Object(yii\web\Request))
9 /Applications/XAMPP/xamppfiles/htdocs/cms/backend/web/index.php(17): yii\base\Application->run()
10 {main}
If I use ['#'] instead of ['admin', 'access'] it works fine. That means that site/error is rendering probably.
But using ['admin, 'access'] causes errors (in text form) and site/error is NOT rendering probably. Why?
admin is a defined rule and access is a permission. Everything works great using in controllers.
Does anyone have an idea what's wrong here?
Thank you!
It seems that you have an error and the application try to show the error page. But the user don't have access to the error page too.

Unable to create a db table with laravel

I am learning how to use schema builder to create tables. I have got phpmyadmin running on 127.0.0.1:81/phpmyadmin. I created a db in phpmyadmin named testdb.
On my laravel app in app/config/database.php i have made changes to 'mysql' section as:
'mysql' => array(
'driver' => 'mysql',
'host' => '127.0.0.1:81',
'database' => 'testdb',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
On app/routes.php i wrote :
Route::get('/', 'HomeController#showWelcome');
On app/controller/HomeController.php i wrote:
public function showWelcome()
{
Schema::create('employee', function($emp_table){
$emp_table->increments('id');
$emp_table->string('name');
$emp_table->integer('salary');
});
return View::make('hello');
}
Now i am getting a 'Something went wrong' message when reloading localhost:8000. It should be creating the table 'employee' with fields 'id', 'name' and 'salary'. The 'app/storage/logs/laravel.log' says:
[2015-01-14 06:09:49] production.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Maximum execution time of 60 seconds exceeded' in C:\wamp\www\stylop-dev\bootstrap\compiled.php:9301
Stack trace:
#0 [internal function]: Illuminate\Exception\Handler->handleShutdown()
#1 {main} [] []
Please help, i am stuck fixing this error and have hit a dead end :(
Well i guess i figured out why i was getting the error. I was using 127.0.0.1:81 so i changed it back to the default port 80.
Now it works