Slim 3 - Error is not shown - php

I am trying to use zeuxisoo/slim-whoops to show errors, but for some reason I am only getting white screen without any message show. This is the code I am using (I don't know if it does matter, I am using quick PHP host (php -S localhost:8000):
require __DIR__ . '/../vendor/autoload.php';
$app = new Slim\App([
'settings' => [
'displayErrorDetails' => true,
'debug' => true,
'whoops.editor' => 'sublime',
]
]);
$app->add(new \Zeuxisoo\Whoops\Provider\Slim\WhoopsMiddleware);

Make sure that your file begins with <?php and that after your $app->add(...); you call $app->run();:
<?php
require __DIR__ . '/../vendor/autoload.php';
$app = new Slim\App([
'settings' => [
'displayErrorDetails' => true,
'debug' => true,
'whoops.editor' => 'sublime',
]
]);
$app->add(new \Zeuxisoo\Whoops\Provider\Slim\WhoopsMiddleware);
$app->run();

Related

Auth0 integration in PHP - getUser() always returning NULL

Within login.php in the Auth0 starter app for PHP, if you run the following code, which comes straight from the documentation, you won't be able to get the user information.
I.e., $auth0->getUser(); will return NULL.
<?php
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/dotenv-loader.php';
$auth0 = new Auth0\SDK\Auth0([
'domain' => $_ENV['AUTH0_DOMAIN'],
'client_id' => $_ENV['AUTH0_CLIENT_ID'],
'redirect_uri' => $_ENV['AUTH0_CALLBACK_URL'],
'audience' => $_ENV['AUTH0_AUDIENCE'],
'scope' => 'openid profile email',
]);
$auth0->login();
It took me a while to figure this out, but you'll need to add the client secret within the new instance of auth0. I.e. add the following snippet: 'client_secret' => $_ENV['AUTH0_CLIENT_SECRET'],
The final code should look like:
<?php
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/dotenv-loader.php';
$auth0 = new Auth0\SDK\Auth0([
'domain' => $_ENV['AUTH0_DOMAIN'],
'client_id' => $_ENV['AUTH0_CLIENT_ID'],
'redirect_uri' => $_ENV['AUTH0_CALLBACK_URL'],
'audience' => $_ENV['AUTH0_AUDIENCE'],
'scope' => 'openid profile email',
]);
$auth0->login();
At this point, when you call $auth0->getUser(); it will return the full user object.

Woocommerce RestAPI not working properly 401 Error code

I've been trying to Get products details from my existing wordpress woocomerce website. I have created the Key and able to get data when use $woocommerce->get('') but when use $woocommerce->get('orders') or $woocommerce->get('products') it's giving 401 error.
Following is my code:
<?php
require __DIR__ . '/vendor/autoload.php';
use Automattic\WooCommerce\Client;
use Automattic\WooCommerce\HttpClient\HttpClientException;
$woocommerce = new Client(
'https://mycustomdomain.com',
'ck_XXXXXXXXXXXXXXXXXXXXXXXXXX',
'cs_XXXXXXXXXXXXXXXXXXXXXXXXXXX',
[
'wp_api' => true,
'version' => 'wc/v3',
'query_string_auth' => true,
]
);
$results = $woocommerce->get('orders');
//$results = $woocommerce->get('');
echo "<pre>";
print_r($results);
echo "</pre>";
I have also uninstalled all the plugins and upgraded the PHP version to the latest one and also, increased the memory which is now 256m but nothing works properly.
//maybe if you change this:
[
'wp_api' => true,
'version' => 'wc/v3',
'query_string_auth' => true,
]
// enter code here
to this:
[
'wp_api' => false,
'version' => 'wc/v3',
'query_string_auth' => false,
]

Slim v4 Creating Log File

So, my issue is that I'm having trouble to make Slim record all its actions inside a file (for example : app.log). I ran across a lot of tutorials and other forum similar to this one but the issue was that they were using the v3 of the Slim Framework.
I saw some post suggesting things like this inside a settings.php :
return [
'settings' => [
'displayErrorDetails' => true, // set to false in production
'addContentLengthHeader' => false, // Allow the web server to send the content-length header
// Renderer settings
'renderer' => [
'template_path' => __DIR__ . '/../templates/',
],
// Monolog settings
'logger' => [
'name' => 'my-app',
'path' => __DIR__ . '/../logs/' . $logDate->format('Y-m-d') . 'app.log',
],
],
];
But the issue with that method is that, well, settings aren't set up this way anymore in v4. So here I am. Stuck. If anybody could give me a hand, it'll help a lot !
To load the settings within a container you have to add a container definition for it.
Example settings file: config/settings.php
return [
// set to false in production
'displayErrorDetails' => true,
// Renderer settings
'renderer' => [
'template_path' => __DIR__ . '/../templates/',
],
// Monolog settings
'logger' => [
'name' => 'my-app',
'path' => __DIR__ . '/../logs/' . date('Y-m-d') . '_app.log',
],
];
Example container entry in config/container.php:
use Psr\Container\ContainerInterface;
// ...
return [
// ...
'settings' => function () {
return require __DIR__ . '/settings.php';
},
// Add more entries here ...
}
To fetch the settings within the container use this:
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
// ...
return [
// ...
LoggerInterface::class => function (ContainerInterface $container) {
$settings = $container->get('settings');
$name = $settings['logger']['name'];
$logger = new Logger($name);
// Add logger handler...
return $logger;
},
// ...
}
Tip: For autowiring support it's better to use an collection object instead of an simple "string" as container identifier. Read more

Encrypted Cookies in Slim Framework v3

I am writing and application in Slim Framework v3.1. I am a lot confused on how to correctly set and get the cookies using Slim's methods.
I need your help in understanding what is the right way to read and write cookies with encryption enabled.
I also need to know how to enable encryption and decryption for the same.
Currently my $app is initialised this way -
$settings = require __DIR__ . '/../src/settings.php';
$app = new \Slim\App($settings);
My settings.php looks like below -
return [
'settings' => [
'displayErrorDetails' => true, // set to false in production
'addContentLengthHeader' => false, // Allow the web server to send the content-length header
// Renderer settings
'renderer' => [
'template_path' => __DIR__ . '/../templates/',
],
// Cookies Encryption
'cookies.encrypt' => true,
'cookies.secret_key' => '53cr3t',
'cookies.cipher' => OPENSSL_CIPHER_AES_256_CBC,
'cookies.cipher_mode' => MCRYPT_MODE_CBC,
],
];

Persisting Sessions in Slim

I'm working on a web app using Slim, but I'm facing an issue with setting and persisting sessions.
Here is my index.php. I am trying to set a csrfToken key in the $_SESSION array, so that every request that is made through the app checks if the user has a csrfToken key, if not it will create one.
I'm just confused as to why it isn't persisting because on the next request it's gone. session_start is being called, it's being called automatically by '\Slim\Middleware\SessionCookie'.
Any ideas why this wouldn't be working? And would it be better to place this into middleware or use a hook?
use duncan3dc\Laravel\Blade;
use duncan3dc\Helpers\Env;
# TODO: Bootstrap the app. Move this to a seperate file. Dev only.
R::setup('mysql:host=localhost;dbname=somedb','user','pass');
$app = new \Slim\Slim(array(
'mode' => 'development',
'templates.path' => './views',
'cookies.encrypt' => true,
'cookies.secret_key' => 'mylongsecretkey',
'cookies.cipher' => MCRYPT_RIJNDAEL_256,
'cookies.cipher_mode' => MCRYPT_MODE_CBC
));
$app->add(new \Slim\Middleware\SessionCookie(array(
'expires' => '10 minutes',
'path' => '/',
'domain' => 'site.com',
'secure' => false, # Contact client to discuss using SSL
'httponly' => false,
'name' => '_sus',
'secret' => 'mylongsecretkey', # Do I need this twice?
'cipher' => MCRYPT_RIJNDAEL_256,
'cipher_mode' => MCRYPT_MODE_CBC
)));
# Not persisting ...
if(!isset($_SESSION['csrfToken']))
$_SESSION['csrfToken'] = hash("sha512",mt_rand(0,mt_getrandmax()));
# TODO: Bootstrap these.
require 'routes/index.php';
require 'routes/dashboard.php';
require 'routes/signup.php';
require 'routes/contactus.php';
require 'routes/privacypolicy.php';
require 'routes/testimonials.php';
require 'routes/login.php';
$app->run();
I figured out how to do it after reading more into hooks.
$app->hook('slim.before.router', function() use ($app){
if(!isset($_SESSION['csrfToken']))
$_SESSION['csrfToken'] = hash("sha512",mt_rand(0,mt_getrandmax()));
});

Categories