# Slim $app-\>redirect Issues

**URL:** <https://discourse.slimframework.com/t/slim-app-redirect-issues/360>\
**Category:** Questions\
**Created:** [June 3, 2016, 3:39pm UTC](https://discourse.slimframework.com/t/slim-app-redirect-issues/360 "2016-06-03T15:39:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![enail131](https://avatars.discourse-cdn.com/v4/letter/e/b2d939/32.png) [@enail131](https://discourse.slimframework.com/u/enail131)\
**Post date:** [June 3, 2016, 3:39pm UTC](https://discourse.slimframework.com/t/slim-app-redirect-issues/360/1 "2016-06-03T15:39:08Z")

</div>

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:

```auto
Type: BadMethodCallException
Message: Method redirect is not a valid method
File: C:\wamp\www\endesign\vendor\slim\slim\Slim\App.php

```

contact.twig code:

```auto
{% 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:

```auto
<?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

---

<div class="post-metadata">

**Author:** ![enail131](https://avatars.discourse-cdn.com/v4/letter/e/b2d939/32.png) [@enail131](https://discourse.slimframework.com/u/enail131)\
**Post date:** [June 3, 2016, 6:40pm UTC](https://discourse.slimframework.com/t/slim-app-redirect-issues/360/2 "2016-06-03T18:40:50Z")

</div>

If anyone else has this issue I have figured it out. Below is the code where the redirect comes in to play, which happens when the form submission is successful.

```auto

	if($result > 0){
		return $response->withRedirect($router->pathFor('home'));
	} else {
		return $response->withRedirect($router->pathFor('contact'));
	}

```
