Saturday 24 August 2019

php - White Screen of Death, Twig_Node_Expression _Constant not found

I had a site that I was making in XAMPP in windows environment which was and still is working fine. However I have switched it over to LAMP using linux. And now I getting a white screen of death. I read up here that I should use this code;



error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);



at the top of my index.php file to display errors. Which I have done but now get the error;



Fatal error: Class 'Twig_Node_Expression_Constant' not found in /var/www/vendor/twig/twig/lib/Twig/ExpressionParser.php on line 206


I am not to sure how to fix this as this file is in the vendor folder. As mentioned before it seems to work fine on XAMPP in windows but on linux I keep getting this error and a white screen. I also looked here and here but these links just seemed to say the same thing as the first link I visited to display the errors





error_reporting(E_ALL);
ini_set('display_errors',TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');
require_once __DIR__.'/vendor/autoload.php';
require_once 'src/provider/AuthenticationServiceProvider.php';
require_once 'src/provider/TutorServiceProvider.php';

use Symfony\Component\HttpFoundation\Request;


$app = new Silex\Application();
$app['debug'] = true;

$app->register(new Silex\Provider\TwigServiceProvider(), array(
'twig.path' => __DIR__.'/views',
));

$app->register(new Silex\Provider\SessionServiceProvider());


$app->register(new Tutor\Provider\AuthenticationServiceProvider());

$app->register(new Tutor\Provider\TutorServiceProvider());

$app->get('/', function() use($app) {
$tutors = $app['tutor']->get_tutors();
return $app['twig']->render('index.twig', array('active_page' => 'home', 'is_user_logged_in' => $app['auth']->is_user_logged_in(), 'tutors' => $tutors));
});

$app->get('/about-us', function() use($app) {

return $app['twig']->render('about-us.twig', array('active_page' => 'about-us', 'is_user_logged_in' => $app['auth']->is_user_logged_in()));
});

$app->get('/home-tuition', function() use($app) {
return $app['twig']->render('home-tuition.twig', array('active_page' => 'home-tuition', 'is_user_logged_in' => $app['auth']->is_user_logged_in()));
});

$app->get('/group-tuition', function() use($app) {
$groups = $app['tutor']->get_group_tuition($app['auth']->get_user()['email']);
return $app['twig']->render('group-tuition.twig', array('active_page' => 'group-tuition', 'is_user_logged_in' => $app['auth']->is_user_logged_in(), 'groups' => $groups, 'user' => $app['auth']->get_user()));

});

$app->get('/group-tuition/{tutoremail}/{starttime}', function($tutoremail, $starttime) use($app) {
$user = $app['auth']->get_user();
if (null !== $user && ($user['type'] == 'admin' || $user['email'] == $tutoremail)) {
$group = $app['tutor']->get_group_tuition_details($tutoremail, $starttime);
return $app['twig']->render('group-tuition-details.twig', array('active_page' => 'group-tuition', 'is_user_logged_in' => $app['auth']->is_user_logged_in(), 'group' => $group));
} else {
return $app->redirect('/group-tuition');
}

});

$app->get('/contact-us', function() use($app) {
return $app['twig']->render('contact-us.twig', array('active_page' => 'contact-us', 'is_user_logged_in' => $app['auth']->is_user_logged_in()));
});

$app->get('/tutor/{username}', function($username) use($app) {
$tutor = $app['tutor']->get_tutor($username);
if (null !== $tutor) {
return $app['twig']->render('tutor.twig', array('active_page' => 'tutor', 'is_user_logged_in' => $app['auth']->is_user_logged_in(), 'tutor' => $tutor));

} else {
return $app->redirect('/');
}
});

$app->get('/login', function() use($app) {
if ($app['auth']->is_user_logged_in()) {
return $app->redirect('/');
} else {
return $app['twig']->render('login.twig', array('active_page' => 'login', 'is_user_logged_in' => $app['auth']->is_user_logged_in()));

}
});

$app->post('/login', function(Request $request) use($app) {
$email = $app['request']->get('email');
$password = $app['request']->get('password');
if ($app['auth']->login($email, $password)) {
return $app->redirect('/');
} else {
return $app->redirect('/login');

}
});

$app->get('/logout', function() use($app) {
$app['auth']->logout();
return $app->redirect('/');
});

$app->get('/register', function() use($app) {
if ($app['auth']->is_user_logged_in()) {

return $app->redirect('/');
} else {
return $app['twig']->render('register.twig', array('active_page' => 'register', 'is_user_logged_in' => $app['auth']->is_user_logged_in()));
}
});


$app->post('/register', function(Request $request) use($app) {
$email = $app['request']->get('email');
$password = $app['request']->get('password');

$name = $app['request']->get('name');
$address = $app['request']->get('address');
$phone = $app['request']->get('phone');
if ($app['auth']->register($email, $password, $name, $address, $phone, 'student')) {
return $app->redirect('/');
} else {
return $app->redirect('/register');
}
});




$app->get('/reset-password', function() use($app) {
return $app['twig']->render('reset-password.twig',array('active_page' => 'reset-password','is_user_logged_in' => $app['auth']->is_user_logged_in()));
});

$app->post('/reset-password', function(Request $request) use($app){
$email = $app['request']->get('email');
if($app['auth']->sendEmail($email)){
return $app->redirect('/');

}else{
return $app->redirect('/reset-password');
}
});

$app->get('/admin', function() use($app) {
$tutor = $app['tutor']->get_tutors();
$user = $app['auth']->get_user();
if (null !== $user && $user['type'] == 'admin' && null !== $tutor){
return $app['twig']->render('admin.twig', array('active_page' => 'admin', 'is_user_logged_in' => $app['auth']->is_user_logged_in(), 'contacts' => $app['tutor']->get_user(), 'items' => $app['tutor']->get_user_id(), 'tutor' => $tutor));

}else{
return $app->redirect('/login');
}
});



$app->post('/charge', function(Request $request) use($app) {
$description = $app['request']->get('description');
$tutoremail = $app['request']->get('tutoremail');

$starttime = $app['request']->get('starttime');
$user = $app['auth']->get_user();
$studentemail = $user['email'];

// Set your secret key: remember to change this to your live secret key in production
// See your keys here https://dashboard.stripe.com/account/apikeys
\Stripe\Stripe::setApiKey("sk_test_fktchFYkd0XywraJftt8Z9uc");

// Get the credit card details submitted by the form
$token = $app['request']->get('stripeToken');


// Create the charge on Stripe's servers - this will charge the user's card
try {
$charge = \Stripe\Charge::create(array(
"amount" => 2000, // amount in cents, again
"currency" => "gbp",
"source" => $token,
"description" => $description . ' paid by ' . $studentemail . ' for session ' . $tutoremail . '/' . $starttime
));
$app['tutor']->add_group_tuition_booking($tutoremail, $starttime, $studentemail);

} catch(\Stripe\Error\Card $e) {
// The card has been declined
}
return $app->redirect('/group-tuition');
});

$app->run();

?>



Composer.json file



{
"require": {
"silex/silex": "~1.1",
"symfony/browser-kit": "~2.3",
"symfony/console": "~2.3",
"symfony/config": "^3.0",
"symfony/css-selector": "~2.3",

"symfony/dom-crawler": "~2.3",
"symfony/filesystem": "~2.3",
"symfony/finder": "~2.3",
"symfony/form": "^3.0",
"symfony/locale": "~2.3",
"symfony/process": "~2.3",
"symfony/security": "~2.3",
"symfony/serializer": "~2.3",
"symfony/translation": "^3.0",
"symfony/validator": "^3.0",

"symfony/monolog-bridge": "~2.3",
"symfony/twig-bridge": "~2.3",
"doctrine/dbal": ">=2.2.0,<2.4.0-dev",
"swiftmailer/swiftmailer": "5.*",
"twig/twig": "^1.24",
"symfony/security-csrf": "^3.0",
"stripe/stripe-php": "3.*"
}
}



SOLUTION:



After reading around a lot over the internet I managed to solve both problem.



1) For the first problem I was copying over the composer.json file and vendor directory just expecting them to work. However as mentioned in the comments above by @Federico I simply copied over my composer.json file and ran composer update. This solved the TWIG_NODE_EXPRESSION_CONSTANT error.



2) The second error I mentioned was that I was getting a 404 when navigating to other pages;



For this I read around the internet and learnt that you need to edit the httpd.conf file to get the htaccess file to work with apache. However as I have a different version of ubuntu which meant the apache2.conf file (/etc/apache2/apache2.conf), so I changed the code;




from this




Options Indexes FollowSymLinks
AllowOverride None
Require all granted




to this




Options Indexes FollowSymLinks
AllowOverride All
Require all granted



However after this I was still getting an 500 internal error. So I found out that the /etc/apache2/apache2/sites-available/000-default.conf (on some versions maybe /etc/apache2/sites-available/default this was not the case on the ubuntu 14.04 that I am using) needs to have the above code inserted into it aswell. However as I installed lamp according to ubuntu's instructions this /etc/apache2/apache2/sites-available/000-default.conf file was copied over to /etc/apache2/apache2/sites-available/mysite.conf and this is where I added in the code;





Options Indexes FollowSymLinks
AllowOverride All
Require all granted



This finally got it to work. Not sure why apache changed their directory/files conventions around guess its to do with different version upgrades.

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...