YII2 error - The "id" configuration for the Application is required - php

I'm trying to start with YII2 (I should say that quite difficult after ASP.NET MVC) and got this error, but can't get what's wrong - id property has been set.
<?php
return [
'id' => 'crmapp',
'basePath' => realpath(__DIR__ . '/../'),
'components' => [
'request' => [
'cookieValidationKey' => 'somekey'
],
'urlManager'=>[
'enablePrettyUrl'=>true,
'showScriptName'=>false
]
],
'db'=> [
require(__DIR__.'/db.php'),
]];
Here is full error text:
Fatal error: Uncaught exception 'yii\base\InvalidConfigException' with message 'The "id" configuration for the Application is required.' in C:\xampp\htdocs\crmapp\vendor\yiisoft\yii2\base\Application.php:220 Stack trace: #0 C:\xampp\htdocs\crmapp\vendor\yiisoft\yii2\base\Application.php(202): yii\base\Application->preInit('C:\\xampp\\htdocs...') #1 C:\xampp\htdocs\crmapp\web\index.php(10): yii\base\Application->__construct('C:\\xampp\\htdocs...') #2 {main} thrown in C:\xampp\htdocs\crmapp\vendor\yiisoft\yii2\base\Application.php on line 220
Here is web/index.php
<?php
require(__DIR__.'/../vendor/yiisoft/yii2/Yii.php');
$config = (__DIR__.'/../config/web.php');
(new yii\web\Application($config))->run();

Here's your problem:
$config = (__DIR__.'/../config/web.php');
$config contains the path to web.php, not its contents. It should be:
$config = require(__DIR__ . '/../config/web.php');

Related

Yii2 Invalid Configuration, cookieValidationKey must be configured with a secret key error when accessing the API

I am encountering an error whenever I am trying to access my self written API.
{
"name": "Invalid Configuration",
"message": "yii\web\Request::cookieValidationKey must be configured with a secret key.",
"code": 0,
"type": "yii\base\InvalidConfigException",
"file": "F:\xampp\htdocs\inventory-web\vendor\yiisoft\yii2\web\Request.php",
"line": 1669,
"stack-trace": [
"#0 F:\xampp\htdocs\inventory-web\vendor\yiisoft\yii2\web\Request.php(1651): yii\web\Request->loadCookies()",
"#1 F:\xampp\htdocs\inventory-web\vendor\yiisoft\yii2\web\Request.php(1739): yii\web\Request->getCookies()",
"#2 F:\xampp\htdocs\inventory-web\vendor\yiisoft\yii2\web\Request.php(1721): yii\web\Request->loadCsrfToken()",
"#3 F:\xampp\htdocs\inventory-web\vendor\yiisoft\yii2\web\User.php(279): yii\web\Request->getCsrfToken(true)",
"#4 F:\xampp\htdocs\inventory-web\vendor\yiisoft\yii2\web\User.php(261): yii\web\User->regenerateCsrfToken()",
"#5 F:\xampp\htdocs\inventory-web\vendor\yiisoft\yii2\web\User.php(299): yii\web\User->login(Object(common\models\User))",
"#6 F:\xampp\htdocs\inventory-web\vendor\yiisoft\yii2\filters\auth\HttpHeaderAuth.php(62): yii\web\User->loginByAccessToken('aa9d0c9e05a7f35...', 'yii\\filters\\aut...')",
"#7 F:\xampp\htdocs\inventory-web\vendor\yiisoft\yii2\filters\auth\AuthMethod.php(59): yii\filters\auth\HttpHeaderAuth->authenticate(Object(yii\web\User), Object(yii\web\Request), Object(yii\web\Response))",
"#8 F:\xampp\htdocs\inventory-web\vendor\yiisoft\yii2\base\ActionFilter.php(77): yii\filters\auth\AuthMethod->beforeAction(Object(yii\base\InlineAction))",
"#9 [internal function]: yii\base\ActionFilter->beforeFilter(Object(yii\base\ActionEvent))",
"#10 F:\xampp\htdocs\inventory-web\vendor\yiisoft\yii2\base\Component.php(627): call_user_func(Array, Object(yii\base\ActionEvent))",
"#11 F:\xampp\htdocs\inventory-web\vendor\yiisoft\yii2\base\Controller.php(276): yii\base\Component->trigger('beforeAction', Object(yii\base\ActionEvent))",
"#12 F:\xampp\htdocs\inventory-web\vendor\yiisoft\yii2\web\Controller.php(185): yii\base\Controller->beforeAction(Object(yii\base\InlineAction))",
"#13 F:\xampp\htdocs\inventory-web\vendor\yiisoft\yii2\base\Controller.php(155): yii\web\Controller->beforeAction(Object(yii\base\InlineAction))",
"#14 F:\xampp\htdocs\inventory-web\vendor\yiisoft\yii2\base\Module.php(528): yii\base\Controller->runAction('refdatajson', Array)",
"#15 F:\xampp\htdocs\inventory-web\vendor\yiisoft\yii2\web\Application.php(103): yii\base\Module->runAction('v1/survey/refda...', Array)",
"#16 F:\xampp\htdocs\inventory-web\vendor\yiisoft\yii2\base\Application.php(386): yii\web\Application->handleRequest(Object(yii\web\Request))",
"#17 F:\xampp\htdocs\inventory-web\api\web\index.php(35): yii\base\Application->run()",
"#18 {main}"
]
}
I have looked at this problem in detail and tried this solution in my config/main.php
$params = array_merge(
require(__DIR__ . '/../../common/config/params.php'),
require(__DIR__ . '/../../common/config/params-local.php'),
require(__DIR__ . '/params.php'),
require(__DIR__ . '/params-local.php'),
require(__DIR__ .'/main-local.php')
);
'components' => [
'request' => [
'enableCookieValidation' => false,
'enableCsrfValidation' => false,
],
'request' => [
'enableCookieValidation' => false,// also set it to true
'enableCsrfValidation' => false, // also set it to true
],
In /config/main-local.php I have following
'components' => [
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'someRandomKey',
],
],
It's not working for me. Any help would be highly appreciated.
Remove the request component from the common/config/main-local.php and just keep it into the frontend/config/main-local.php.
Then add the following in your api/config/main.php under components to turn off cookie validation for the API.
'components' => [
'request' => [
'enableCookieValidation' => false,
'enableCsrfValidation' => false,
],

WooCommerce Rest API returning invalid paramter 'shipping_lines' in create order

Creating a order from php rest api of woo-commerce version: wc/v3 and getting error
other apis are working fine tried with v2 still getting the same error
<?php
require __DIR__ . '/vendor/autoload.php';
use Automattic\WooCommerce\Client;
$wc = new Client(
'https://example.com/',
'client key',
'client secret',
[
'wp_api' => true,
'version' => 'wc/v3',
]
);
$data = [
'payment_method' => 'cod',
....
'shipping_lines' => [
[
'method_id' => 'flat_rate',
'method_title' => 'Flat Rate',
'total' => 10
]
]
];
print_r($wc->post('orders', $data));
Getting this error, if i remove shipping lines working fine and creating the order
[07-May-2019 06:39:45 UTC] PHP Fatal error: Uncaught Automattic\WooCommerce\HttpClient\HttpClientException: Error: Invalid parameter(s): shipping_lines [rest_invalid_param] in /home/gathhnaw/public_html/mapi/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php:350
Stack trace:
#0 /home/gathhnaw/public_html/mapi/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php(386): Automattic\WooCommerce\HttpClient\HttpClient->lookForErrors(Object(stdClass))
#1 /home/gathhnaw/public_html/mapi/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php(422): Automattic\WooCommerce\HttpClient\HttpClient->processResponse()
#2 /home/gathhnaw/public_html/mapi/vendor/automattic/woocommerce/src/WooCommerce/Client.php(56): Automattic\WooCommerce\HttpClient\HttpClient->request('orders', 'POST', Array)
#3 /home/gathhnaw/public_html/mapi/create_order.php(46): Automattic\WooCommerce\Client->post('orders', Array)
#4 {main}
thrown in /home/gathhnaw/public_html/mapi/vendor/automattic/woocommerce/src/WooCommerce/HttpClient/HttpClient.php on line 350
There seems to be a typo or documentation error on woo api. Just surround the total value between single quotes and run it
https://www.infocaptor.com/dashboard/woocommerce-rest-api-php-example-error-creating-order

Fatal error: Uncaught Elasticsearch\Common\Exceptions\BadRequest400Exception

So I am using elasticsearch.
I have this code:
<?php
error_reporting(E_ALL);ini_set('display_errors', 1);
require 'vendor/autoload.php';
use Elasticsearch\ClientBuilder;
$hosts = [
'http://localhost:80', // SSL to localhost
];
$clientBuilder = ClientBuilder::create(); // Instantiate a new ClientBuilder
$clientBuilder->setHosts($hosts); // Set the hosts
$client = $clientBuilder->build();
$params = [
'index' => 'my_index',
'type' => 'my_type',
'id' => 'my_id',
'body' => ['testField' => 'abc']
];
$response = $client->index($params);
print_r($response);
I get this error:
Fatal error: Uncaught Elasticsearch\Common\Exceptions\BadRequest400Exception: 405 Method Not Allowed Method Not Allowed The requested method PUT is not allowed for the URL /my_index/my_type/my_id. in C:\Bitnami\wampstack-7.0.0RC7-\apache2\htdocs\vendor\elasticsearch\elasticsearch\src\Elasticsearch\Connections\Connection.php:615 Stack trace: #0 C:\Bitnami\wampstack-7.0.0RC7-\apache2\htdocs\vendor\elasticsearch\elasticsearch\src\Elasticsearch\Connections\Connection.php(279): Elasticsearch\Connections\Connection->process4xxError(Array, Array, Array) #1 C:\Bitnami\wampstack-7.0.0RC7-\apache2\htdocs\vendor\react\promise\src\FulfilledPromise.php(25): Elasticsearch\Connections\Connection->Elasticsearch\Connections{closure}(Array) #2 C:\Bitnami\wampstack-7.0.0RC7-\apache2\htdocs\vendor\guzzlehttp\ringphp\src\Future\CompletedFutureValue.php(55): React\Promise\FulfilledPromise->then(Object(Closure), NULL, NU in C:\Bitnami\wampstack-7.0.0RC7-\apache2\htdocs\vendor\elasticsearch\elasticsearch\src\Elasticsearch\Connections\Connection.php on line 615
I GOT IT!
I just had to change
$hosts = [
'http://localhost:80', // SSL to localhost
];
To
$hosts = [
'http://localhost:80' // SSL to localhost
];
(remove the comma)

How to set up a Mapping while indexing data through bulk API in Elasticsearch-PHP?

I am trying to follow this tutorial, and I need to do it in PHP, so I have to use Elasticsearch-php. In this particular step, I am trying to set up a mapping for elasticsearch and also bulk index data from a .json file. So I looked into examples here and [here][3], and following is my code.
The problem is taht it gives me
Elasticsearch PHP client created successfully!
Fatal error: Uncaught Elasticsearch\Common\Exceptions\BadRequest400Exception: {"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"Action/metadata line [1] contains an unknown parameter [dynamic]"}],"type":"illegal_argument_exception","reason":"Action/metadata line [1] contains an unknown parameter [dynamic]"},"status":400} in /var/www/html/Tests/Test/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php:610 Stack trace: #0 /var/www/html/Tests/Test/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php(273): Elasticsearch\Connections\Connection->process4xxError(Array, Array, Array) #1 /var/www/html/Tests/Test/vendor/react/promise/src/FulfilledPromise.php(25): Elasticsearch\Connections\Connection->Elasticsearch\Connections\{closure}(Array) #2 /var/www/html/Tests/Test/vendor/guzzlehttp/ringphp/src/Future/CompletedFutureValue.php(55): React\Promise\FulfilledPromise->then(Object(Closure), NULL, NULL) #3 /var/www/html/Tests/Test/vendor/guzzlehttp/ringphp/src/ in /var/www/html/Tests/Test/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php on line 610
and when I comment out the line "dynamic" => "false", I get
Elasticsearch PHP client created successfully!
Fatal error: Uncaught Elasticsearch\Common\Exceptions\BadRequest400Exception: {"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"Malformed action/metadata line [1], expected a simple value for field [properties] but found [START_OBJECT]"}],"type":"illegal_argument_exception","reason":"Malformed action/metadata line [1], expected a simple value for field [properties] but found [START_OBJECT]"},"status":400} in /var/www/html/Tests/Test/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php:610 Stack trace: #0 /var/www/html/Tests/Test/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php(273): Elasticsearch\Connections\Connection->process4xxError(Array, Array, Array) #1 /var/www/html/Tests/Test/vendor/react/promise/src/FulfilledPromise.php(25): Elasticsearch\Connections\Connection->Elasticsearch\Connections\{closure}(Array) #2 /var/www/html/Tests/Test/vendor/guzzlehttp/ringphp/src/Future/CompletedFutureValue.php(55): React\Promise\FulfilledPromise->then(O in /var/www/html/Tests/Test/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php on line 610
What am I missing? How should I fix this?
<?php
require_once("vendor/autoload.php");
use Elasticsearch\ClientBuilder;
$esClient = ClientBuilder::create()->build();
echo "Elasticsearch PHP client created successfully!<br>";//check
$indexParams = array();
$indexParams['index'] = "test_packets_index";
$indexParams['type'] = "testPacketsType";
$indexParams['body'] = [
'mappings' => [
"testPacketsType" => [
//"dynamic" => "false",
"properties" => [
"timestamp" => [
"type" => "date"
],
"layers" => [
"properties" => [
"ip" => [
"properties" => [
"ip_ip_src" => [
"type" => "ip"
],
"ip_ip_dst" => [
"type" => "ip"
]
]
]
]
]
]
]
]
];
$jsonFileHandle = fopen("packets.json", "r");
if ($jsonFileHandle) {
while ( ($line = fgets($jsonFileHandle)) !== FALSE ) {
if ( !($line == "") ) {
$indexParams['body'][] = json_decode($line, true);
}
}
}
fclose($jsonFileHandle);
#print_r($indexParams['body']);//check
$indexingResponse = $esClient->bulk($indexParams);
echo "Result: "; print_r($indexingResponse); echo "<br>";//check
?>
[3]: https : //www.elastic.co/guide/en/elasticsearch/client/php-api/current/_indexing_documents.html

Setting up environment in Yii Framework

I'm having trouble setting an Environment for Yii Framework. Probably is an easy task for someone who knows it, but the documentation of this ir rather poor and I couldn't solve it by myself. Any help would be apprecitated, I don't get to SO until I waste all my resources.
The error I get is:
Fatal error: Uncaught exception 'Exception' with message '"SetEnv YII_ENVIRONMENT <mode>" not defined in Apache config.' in /home/(ommited)/app/extensions/Environment.php:235
Stack trace: #0 /home/(ommited)/app/extensions/Environment.php(209): Environment->getMode(NULL, false) #1 /home/marcelo/myprojects/bocaweb/boca-sitioweb/html/index.php(10):
Environment->__construct() #2 {main} thrown in /home/(ommited)/app/extensions/Environment.php on line 235
My mode_development.php
<?php
return array(
'yiiDebug' => false,
'yiiTraceLevel' => 3,
'configWeb' => array(
'components' => array(// Database
'mongodb' => array(
'class' => 'EMongoClient',
'server' => 'mongodb://(ommited)',
'db' => '(ommited)',
'RP' => array('RP_PRIMARY', array())
),
),
),
'configConsole' => array(),
);
index.php
<?php
error_reporting(E_ALL ^ E_NOTICE);
ini_set('display_errors', 1);
//Envirment and global
require_once(dirname(dirname(__FILE__)) . '/app/components/ArtfosEnvironment.php');
require_once(dirname(dirname(__FILE__)) . '/app/globals.php');
$env = new ArtfosEnvironment();
defined('YII_DEBUG') or define('YII_DEBUG', $env->yiiDebug);
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL', $env->yiiTraceLevel);
require_once($env->yiiPath);
$env->runYiiStatics(); // like Yii::setPathOfAlias()
$yiiApp = Yii::createWebApplication($env->configWeb);
$yiiApp->run();
Is the Yii framework folder in the directory beside your local? Make sure that your site knows the location of the framework as that's where it will get all it's resources.
Try I would like to move yii framework folder outside of www access

Categories