I am having some issues with the $app->redirect() method. I am using trying to redirect to a new page when a form submission goes through successfully. I am getting this error:
Type: BadMethodCallException
Message: Method redirect is not a valid method
File: C:\wamp\www\endesign\vendor\slim\slim\Slim\App.php
contact.twig code:
{% extends 'base.twig' %}
{% block content %}
<div class="col-xs-12 col-md-8 col-md-offset-2">
<form actions="index.php" method="post">
<input name="name" type="text" placeholder="Full Name">
<input name="email" type="email" placeholder="Email Address">
<textarea name="message" placeholder="Enter your message here..."></textarea>
<input class="button" type="submit" value="Send Message!">
</form>
</div>
{% endblock %
index.php code:
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require 'vendor/autoload.php';
$config = [
'settings' => [
'displayErrorDetails' => 1,
],
];
$app = new \Slim\App($config);
$container = $app->getContainer();
// Register component on container
$container['view'] = function ($container) {
$view = new \Slim\Views\Twig('templates');
$view->addExtension(new \Slim\Views\TwigExtension(
$container['router'],
$container['request']->getUri()
));
return $view;
};
//Routes
$app->get('/', function ($request, $response, $args) {
return $this->view->render($response, 'home.twig');
})->setName('home');
$app->get('/about', function ($request, $response, $args) {
return $this->view->render($response, 'about.twig');
})->setName('about');
$app->get('/services', function ($request, $response, $args) {
return $this->view->render($response, 'services.twig');
})->setName('services');
$app->get('/portfolio', function ($request, $response, $args) {
return $this->view->render($response, 'portfolio.twig');
})->setName('portfolio');
$app->get('/contact', function ($request, $response, $args) use($app) {
return $this->view->render($response, 'contact.twig');
})->setName('contact');
$app->get('/photography', function ($request, $response, $args) {
return $this->view->render($response, 'photography.twig');
})->setName('photography');
//SwiftMailer for Contact Page
$app->post('/contact', function(Request $request, Response $response, $args) use($app) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
if (!empty($name) && !empty($email) && !empty($message)){
$cleanName = filter_var($name, FILTER_SANITIZE_STRING);
$cleanEmail = filter_var($email, FILTER_SANITIZE_EMAIL);
$cleanMessage = filter_var($message, FILTER_SANITIZE_STRING);
} else {
$app->redirect('contact');
}
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
->setUsername('**************')
->setPassword('*************');
$mailer = \Swift_Mailer::newInstance($transport);
$message = \Swift_Message::newInstance();
$message->setSubject('New Email');
$message->setFrom(array(
$cleanEmail => $cleanName
));
$message->setTo(array('email@email.com'));
$message->setBody($cleanMessage);
$result = $mailer->send($message);
if($result > 0){
$app->redirect('home');
} else {
$app->redirect('contact');
}
});
// Run app
$app->run();
I’m not entirely sure what I am doing wrong here. Does anyone have any insight for me?
Thank you in advance