How to fix this error in Slim framework? - slim

I installed Slim framwork using composer in wamp server.
But displays following error.
I am new to Slim.
Slim Application Error
The application could not run because of the following error:
Details
Type: ErrorException Code: 2 Message:
file_get_contents(templates/index.html): failed to open stream: No
such file or directory File:
D:\wamp\www\photometa\vendor\twig\twig\lib\Twig\Loader\Filesystem.php
Line: 131 Trace
0 [internal function]: Slim\Slim::handleErrors(2, 'file_get_conten...', 'D:\wamp\www\pho...', 131, Array)
1 D:\wamp\www\photometa\vendor\twig\twig\lib\Twig\Loader\Filesystem.php(131):
file_get_contents('templates/index...')
2 D:\wamp\www\photometa\vendor\twig\twig\lib\Twig\Environment.php(397):
Twig_Loader_Filesystem->getSource('index.html')
3 D:\wamp\www\photometa\vendor\slim\views\Twig.php(87): Twig_Environment->loadTemplate('index.html')
4 D:\wamp\www\photometa\vendor\slim\slim\Slim\View.php(255): Slim\Views\Twig->render('index.html', NULL)
5 D:\wamp\www\photometa\vendor\slim\slim\Slim\View.php(243): Slim\View->fetch('index.html', NULL)
6 D:\wamp\www\photometa\vendor\slim\slim\Slim\Slim.php(757): Slim\View->display('index.html')
7 D:\wamp\www\photometa\public\index.php(33): Slim\Slim->render('index.html')
8 [internal function]: {closure}()
9 D:\wamp\www\photometa\vendor\slim\slim\Slim\Route.php(468): call_user_func_array(Object(Closure), Array)
10 D:\wamp\www\photometa\vendor\slim\slim\Slim\Slim.php(1357): Slim\Route->dispatch()
11 D:\wamp\www\photometa\vendor\slim\slim\Slim\Middleware\Flash.php(85):
Slim\Slim->call()
12 D:\wamp\www\photometa\vendor\slim\slim\Slim\Middleware\MethodOverride.php(92):
Slim\Middleware\Flash->call()
13 D:\wamp\www\photometa\vendor\slim\slim\Slim\Middleware\PrettyExceptions.php(67):
Slim\Middleware\MethodOverride->call()
14 D:\wamp\www\photometa\vendor\slim\slim\Slim\Slim.php(1302): Slim\Middleware\PrettyExceptions->call()
15 D:\wamp\www\photometa\public\index.php(37): Slim\Slim->run()
16 {main}
This is index.php
<?php
require '../vendor/autoload.php';
// Prepare app
$app = new \Slim\Slim(array(
'templates.path' => '../templates',
));
// Create monolog logger and store logger in container as singleton
// (Singleton resources retrieve the same log resource definition each time)
$app->container->singleton('log', function () {
$log = new \Monolog\Logger('slim-skeleton');
$log->pushHandler(new \Monolog\Handler\StreamHandler('../logs/app.log', \Monolog\Logger::DEBUG));
return $log;
});
// Prepare view
$app->view(new \Slim\Views\Twig());
$app->view->parserOptions = array(
'charset' => 'utf-8',
'cache' => realpath('../templates/cache'),
'auto_reload' => true,
'strict_variables' => false,
'autoescape' => true
);
$app->view->parserExtensions = array(new \Slim\Views\TwigExtension());
// Define routes
$app->get('/', function () use ($app) {
// Sample log message
$app->log->info("Slim-Skeleton '/' route");
// Render index view
$app->render('index.html');
});
// Run app
$app->run();

Try this :
$app = new \Slim\Slim(array(
'templates.path' => __DIR__ . '/../templates/',
));

Related

How write unit test for yii2 project

I read every documents that i found and set up codeception to write unit test for yii2 application.
My project using mongodb as database and when i run my unit test to test save action of my model then i see that db component not found.
It's true because i'm using mongodb and don't need db for sql. anyway when i change my settings to rename mongodb database setting to db and still using mongodb connection settings i see error that means yii2 are trying to use SQL activerecord methods.
My test class:
namespace common\tests;
use common\models\Developer;
use common\tests\fixtures\DeveloperFixture;
use Faker\Factory;
class DeveloperTest extends \Codeception\Test\Unit
{
/**
* #var \common\tests\UnitTester
*/
protected $tester;
/**
* #return array
*/
public function _fixtures()
{
return [
'user' => [
'class' => DeveloperFixture::class,
'dataFile' => codecept_data_dir() . 'developer.php'
]
];
}
/**
* Test to saving user in database.
* We are using Factory object to create dynamic test cases.
*/
public function testSaving()
{
// use the factory to create a Faker\Generator instance
$faker = Factory::create();
$developer = new Developer([
'name' => $faker->name,
'description' => $faker->sentences
]);
$this->assertTrue($developer->save(), 'Developer object saved into database.');
}
protected function _before()
{
}
protected function _after()
{
}
}
My commont/config/test-local.php
<?php
return yii\helpers\ArrayHelper::merge(
require __DIR__ . '/main.php',
require __DIR__ . '/main-local.php',
require __DIR__ . '/test.php',
[
'components' => [
'mongodb' => require_once ('conf.d/test-db.php')
],
]
);
My common/config/conf.d/test-db.php
<?php
return
[
'class' => '\yii\mongodb\Connection',
'dsn' => 'mongodb://mongodb/mytestdb', //Using docker container
];
My fixture class:
<?php
namespace common\tests\fixtures;
use yii\mongodb\ActiveFixture;
/**
* Class Developer
* Active fixture for using Developer model.
*
* #package common\tests\fixtures
*/
class DeveloperFixture extends ActiveFixture
{
public $modelClass = \common\models\Developer::class;
}
After that i run vendor/bin/codecept -c core/common run unit models/DeveloperTest
I see below error:
---------
1) DeveloperTest: Saving
Test tests/unit/models/DeveloperTest.php:testSaving
[yii\base\InvalidConfigException] Failed to instantiate component or class "db".
#1 /app/vendor/yiisoft/yii2/di/Instance.php:139
#2 /app/vendor/yiisoft/yii2/di/Container.php:428
#3 /app/vendor/yiisoft/yii2/di/Container.php:364
#4 /app/vendor/yiisoft/yii2/di/Container.php:156
#5 /app/vendor/yiisoft/yii2/di/Instance.php:167
#6 /app/vendor/yiisoft/yii2/di/Instance.php:137
#7 /app/vendor/yiisoft/yii2/test/DbFixture.php:41
#8 /app/vendor/yiisoft/yii2/base/BaseObject.php:109
#9 yii\base\BaseObject->__construct
#10 /app/vendor/yiisoft/yii2/di/Container.php:375
#1 /app/vendor/yiisoft/yii2/di/Container.php:428
#2 /app/vendor/yiisoft/yii2/di/Container.php:364
#3 /app/vendor/yiisoft/yii2/di/Container.php:156
#4 /app/vendor/yiisoft/yii2/di/Instance.php:167
#5 /app/vendor/yiisoft/yii2/di/Instance.php:137
#6 /app/vendor/yiisoft/yii2/test/DbFixture.php:41
#7 /app/vendor/yiisoft/yii2/base/BaseObject.php:109
#8 yii\base\BaseObject->__construct
#9 /app/vendor/yiisoft/yii2/di/Container.php:375
#10 /app/vendor/yiisoft/yii2/di/Container.php:156
--
There was 1 failure:
---------
1) DeveloperTest: Saving
Test tests/unit/models/DeveloperTest.php:testSaving
Developer object saved into database.
Failed asserting that false is true.
#1 /app/core/common/tests/unit/models/DeveloperTest.php:42
And when i change mongodb in test-local.php to db i see below error log:
---------
1) DeveloperTest: Saving
Test tests/unit/models/DeveloperTest.php:testSaving
[yii\base\UnknownMethodException] Calling unknown method: yii\mongodb\Command::checkIntegrity()
#1 /app/vendor/yiisoft/yii2/base/BaseObject.php:222
#2 /app/vendor/yiisoft/yii2/test/InitDbFixture.php:96
#3 /app/vendor/yiisoft/yii2/test/InitDbFixture.php:78
#4 /app/vendor/yiisoft/yii2/test/FixtureTrait.php:117
#5 /app/vendor/symfony/event-dispatcher/EventDispatcher.php:212
#6 /app/vendor/symfony/event-dispatcher/EventDispatcher.php:44
--
There was 1 failure:
---------
1) DeveloperTest: Saving
Test tests/unit/models/DeveloperTest.php:testSaving
Developer object saved into database.
Failed asserting that false is true.
#1 /app/core/common/tests/unit/models/DeveloperTest.php:42
ERRORS!
Tests: 1, Assertions: 1, Errors: 1, Failures: 1.
Anyone can help me?
This is a bug in core of framework module for codeception. You can duplicate database connection in common/config/test-local.php:
'db' => [
'class' => yii\mongodb\Connection::class,
'dsn' => 'mongodb://localhost:27017/app_test_db',
],
'mongodb' => [
'class' => yii\mongodb\Connection::class,
'dsn' => 'mongodb://localhost:27017/app_test_db',
],

Facebook PHP SDK "couldn't connect to host"

I'm trying to connect to facebook API
require_once '/composer/vendor/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => 'xxxx', // obviously, I've put my app ID in here
'app_secret' => 'xxxx', // and my app secret in here
'default_graph_version' => 'v2.4',
]);
To do the following request, which, if I'm right, should give me the number of shares of the URL :
$target = 'http://stackoverflow.com';
$node = $fb->get('/?id='.urlencode($target).'&fields=share')->getGraphNode();
$shareCount = $node['share']['share_count'];
However, I get the following error :
Fatal error:
Uncaught exception 'Facebook\Exceptions\FacebookSDKException'
with message 'couldn't connect to host'
in /backend/composer/vendor/facebook/php-sdk-v4/src/Facebook/HttpClients/FacebookCurlHttpClient.php:83
Stack trace:
#0 /backend/composer/vendor/facebook/php-sdk-v4/src/Facebook/FacebookClient.php(216):
Facebook\HttpClients\FacebookCurlHttpClient->send('https://graph.f...', 'GET', '', Array, 60)
#1 /backend/composer/vendor/facebook/php-sdk-v4/src/Facebook/Facebook.php(504):
Facebook\FacebookClient->sendRequest(Object(Facebook\FacebookRequest))
#2 /backend/composer/vendor/facebook/php-sdk-v4/src/Facebook/Facebook.php(377):
Facebook\Facebook->sendRequest('GET', '/?id=http%3A%2F...', Array, NULL, NULL, NULL)
#3 /backend/the_script_i_am_currently_trying_to_run.php(79):
Facebook\Facebook->get('/?id=http%3A%2F...')
#4 {main} thrown in /backend/composer/vendor/facebook/php-sdk-v4/src/Facebook/HttpClients/FacebookCurlHttpClient.php on line 83
From what I have read, it could be that I'm not allowed to use cURL on my server, but I'm using it somewhere else and it works, so I don't think the problem comes from here.
Any idea of what I'm doing wrong ?
Thanks

Zend Framework 2 Zend_Http_Client SSL Connection Error

i want to parse all kinds of ssl/https Sites.
E.g i want the content of https://microsoft.de, but it do not work. Following Erros appears:
Fatal error: Uncaught exception 'ErrorException' with message 'stream_socket_enable_crypto() [<a href='function.stream-socket-enable-crypto'>function.stream-socket-enable-crypto</a>]:
SSL operation failed with code 1.
OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed'
in C:\Users\Privat\Desktop\Server\Apache2\htdocs\allsubmitter\server\vendor\zendframework\zend-http\Zend\Http\Client\Adapter\Socket.php:276
Stack trace: #0 [internal function]: Zend\Stdlib\ErrorHandler::addError(2, 'stream_socket_e...', 'C:\Users\Privat...', 276, Array) #1 C:\Users\Privat\Desktop\Server\Apache2\htdocs\allsubmitter\server\vendor\zendframework\zend-http\Zend\Http\Client\Adapter\Socket.php(276): stream_socket_enable_crypto(Resource id #35, true, 2)
#2 C:\Users\Privat\Desktop\Server\Apache2\htdocs\allsubmitter\server\vendor\zendframework\zend-http\Zend\Http\Client.php(1356): Zend\Http\Client\Adapter\Socket->connect('microsoft.de', 443, true) #3 C:\Users\Privat\Desktop\Server\Apache2\htdocs\ in C:\Users\Privat\Desktop\Server\Apache2\htdocs\allsubmitter\server\vendor\zendframework\zend-http\Zend\Http\Client\Adapter\Socket.php on line 299
$config = array(
'adapter' => 'Zend_Http_Client_Adapter_Socket',
'ssltransport' => 'tls'
);
$client = new \Zend\Http\Client("https://microsoft.de");
You can tell the client not to verify the SSL, try this config:
$clientConfig = array(
'adapter' => 'Zend\Http\Client\Adapter\Curl',
'curloptions' => array(
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_SSL_VERIFYPEER => FALSE
),
);
$this->_client = new \Zend\Http\Client('https://microsoft.de', $clientConfig);

mblox soap api - php soap Payment Request not working

mblox soap api - php soap Payment Request not working and gives error.
I use as below code in subscribe.php file...
$client = new SoapClient("https://ngp.us.mblox.com/client-gateway/services?wsdl", array('trace' => 1, 'encoding' => 'UTF-8', 'soap_version' => SOAP_1_2));
$array = array(
'SecurityContext'=>array('userId'=>'aaaaaaaa','password'=>'bbbbbbbb'),
'ClientDetails'=>array('shortcode'=>'234242', 'brandName'=>'aaaaaaaaaa.com', 'programSponsor'=>'aaaaaaaaaa', 'originatingUrl'=>'www.aaaaaaaaaa.com', 'minPageUrl'=>'www.aaaaaaaaaa.com', 'successUrl'=>'www.aaaaaaaaaa.com/subscribConfirm.php', 'cancelUrl'=>'www.aaaaaaaaaa.com', 'tcUrl'=>'www.aaaaaaaaaa.com/terms.html', 'postBackUrl'=>'http://aaaaaaaaaa/subscribe.php'),
'paymentDetails'=>array('paymentType'=>'PSMS', 'amount'=>'9.99', 'currency'=>'USD', 'billingFrequency'=>'MONTHLY'),
'msisdn'=>'243233232',
'serviceId'=>'332',
'operatorId'=>'33343',
'productDescription'=>'Text test',
'optInBody'=>'aaaaaaaaaa',
'browserSessionId'=>'123456',
);
$result = $client->initiatePayment($array);
It shows error as below on last line => $result = $client->initiatePayment($array);
Fatal error: Uncaught SoapFault exception: [(null)] in
/aaaaaaaa/Source/developement/PHP/ver1/subscribe.php:97 Stack trace:
0 /aaaaaaaa/Source/developement/PHP/ver1/subscribe.php(97): SoapClient->__call('initiatePayment', Array) #1
/aaaaaaaa/Source/developement/PHP/ver1/subscribe.php(97):
SoapClient->initiatePayment(Array) #2 {main} thrown in
/aaaaaaaa/Source/developement/PHP/ver1/subscribe.php on line 97
I got things working using curl,
I use This
as a reference and code working.
Thankx

Zend Mail keeps giving me a Socket Error

I'm having trouble with sending email using the zend framework. I keep getting a "Could not open socket" error.
I don't know whats wrong here - it used to work on my other host. Ever since I shifted it to another host I can't send emails. I've set up the configuration values to match the new email server.
Heres my code:
$config = array('auth' => _config('mail', 'auth'),
'username' => _config('mail', 'email'),
'password' => _config('mail', 'password'));
$tr = new Zend_Mail_Transport_Smtp(_config('mail', 'smtp'), $config);
$mail = new Zend_Mail();
$mail->setDefaultTransport($tr);
$mail->setFrom(_config('mail','email'), _config('mail','name'));
$mail->addTo($account_email);
$mail->setSubject($mailTitle);
$mail->setBodyText($mailContent);
$mail->send($tr);
EDIt ===
Well the code posted above is my actual code - I don't know whats wrong with it as it used to work on another host.
The following is the exact error I'm getting
Could not open socketstring(1237) "#0 /home/india/public_html/demo/library/Zend/Mail/Protocol/Smtp.php(167): Zend_Mail_Protocol_Abstract->_connect('tcp://mail.indi...')
#1 /home/india/public_html/demo/library/Zend/Mail/Transport/Smtp.php(199): Zend_Mail_Protocol_Smtp->connect()
#2 /home/india/public_html/demo/library/Zend/Mail/Transport/Abstract.php(348): Zend_Mail_Transport_Smtp->_sendMail()
#3 /home/india/public_html/demo/library/Zend/Mail.php(1194): Zend_Mail_Transport_Abstract->send(Object(Zend_Mail))
#4 /home/india/public_html/demo/application/controllers/AccountController.php(2153): Zend_Mail->send(Object(Zend_Mail_Transport_Smtp))
#5 /home/india/public_html/demo/library/Zend/Controller/Action.php(513): AccountController->forgetPasswordAction()
#6 /home/india/public_html/demo/library/Zend/Controller/Dispatcher/Standard.php(295): Zend_Controller_Action->dispatch('forgetPasswordA...')
#7 /home/india/public_html/demo/library/Zend/Controller/Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#8 /home/india/public_html/demo/application/bootstrap.php(26): Zend_Controller_Front->dispatch() #9 /home/india/public_html/demo/html/index.php(4): Bootstrap::run() #10 {main}"
marhaba Ali! ,
digging the code of Zend mail shows http://framework.zend.com/svn/framework/standard/trunk/library/Zend/Mail/Protocol/Abstract.php
protected function _connect($remote)
{
$errorNum = 0;
$errorStr = '';
// open connection
$this->_socket = #stream_socket_client($remote, $errorNum, $errorStr, self::TIMEOUT_CONNECTION);
if ($this->_socket === false) {
if ($errorNum == 0) {
$errorStr = 'Could not open socket';
}
/**
* #see Zend_Mail_Protocol_Exception
*/
require_once 'Zend/Mail/Protocol/Exception.php';
throw new Zend_Mail_Protocol_Exception($errorStr);
}
if (($result = $this->_setStreamTimeout(self::TIMEOUT_CONNECTION)) === false) {
/**
* #see Zend_Mail_Protocol_Exception
*/
require_once 'Zend/Mail/Protocol/Exception.php';
throw new Zend_Mail_Protocol_Exception('Could not set stream timeout');
}
return $result;
}
and usually the error number 0 because of
if ($errorNum == 0) {
$errorStr = 'Could not open socket';
}
from : http://php.net/manual/en/function.stream-socket-client.php
On failure the errno and errstr
arguments will be populated with the
actual system level error that
occurred in the system-level connect()
call. If the value returned in errno
is 0 and the function returned FALSE,
it is an indication that the error
occurred before the connect() call.
This is most likely due to a problem
initializing the socket. Note that the
errno and errstr arguments will always
be passed by reference.
I guess its some firewall blocking the connection to be sent out ,
system-level or network-level error
If you update your answer with more detailed info , i would be happy to help