We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Response object not working

I cannot get the response object to work and I have tried many implementations referenced by the documentation. I don't have an external configuration for the response object, only what is in the code below.

When this code is run an empty page is displayed (there is no view linked with this action). I get no error messages in the browser / apache / php logs either.

I've checked the path to the controllers directory, which is located at:

__DIR__ . '/../controllers/'

My code:

public function sendAction()
    {
        $this->response->redirect("contact/index");
    }

If I place the following code in the method I see the expected page, so I know for sure the action is being called...

echo phpInfo();
die();

I have tried all combinations below:

$this->response->redirect("contact/index");

// Example shown in Phalcon Docs

$this->response->redirect("https://en.wikipedia.org", true);
return $this->response->redirect("https://en.wikipedia.org", true);

// Should navigate to the base URI

$this->response->redirect();
return $this->response->redirect();

// Creating a new instance of \Phalcon\Http\Response

$resp = new \Phalcon\Http\Response();
$resp->redirect("https://en.wikipedia.org", true);

I'm really not sure what else to try. I have scoured forums and documentation but haven't found anything that solves my issue.

Yes this question has been asked before but the answer didn't help!

I'm running Phalcon 2.0.1 | PHP 5.6.27 | Chrome v.54 | OSX 10.12

My opinion is that the code isn't wrong, but something must not be configured properly. I have tried installing php and Phalcon a couple of times, neither went smoothly and I downloaded a number of required packages and missing elements as I came across them trying to install.

I then backed up my data and wiped my hard drive to start fresh.

I've installed PHP 5.6.27, Phalcon 2.0.13, memcached

edited Nov '16

Phalcon 2.0.13 is not supported, but it will still work nicely with PHP 5.6.x. Next, your problem might be with how you bootstrap your app, and especially PATHS.

The best is to post here your full bootstrap (i.e. index.php), and your services definition.

P.S. Which web server you run? Apache or Nginx?

edited Nov '16

Ok, thanks for your response.

I initially downloaded Phalcon 3 and PHP 7.0, but because a friend that worked on the project was running the versions mentioned above, I installed the same versions as him. Are newer versions of both Phalcon and PHP backwards compatible?

I'm running Apache, I'll post the code for the bootstrap

root/public/index.php

<?php

use Phalcon\Mvc\Application;

define('APP_PATH', realpath('..'));

try {

    $config = require APP_PATH . "/config/config.php";

    /**
     * Include services
     */
    require APP_PATH . '/config/services.php';

    /**
     * Handle the request
     */
    $application = new Application($di);

    /**
     * Include modules
     */
    require APP_PATH . '/config/modules.php';

    // Handle the application:
    echo $application->handle()->getContent();
} 
catch (\Exception $e) {
    echo $e->getMessage() . '<br>';
    echo '<pre>' . $e->getTraceAsString() . '</pre>';
}

root/config/config.php

<?php

return new \Phalcon\Config(array(
    'database' => array(
        ...
    ),
    'application' => array(
        'controllersDir' => __DIR__ . '/../controllers/',
        'modelsDir'      => __DIR__ . '/../models/',
        'migrationsDir'  => __DIR__ . '/../migrations/',
        'viewsDir'       => __DIR__ . '/../views/',
        'baseUri'        => '/',
        'cacheDir'       => APP_PATH . '/cache/',
    )
));

root/config/services.php

<?php
/**
 * Services are globally registered in this file
 *
 * @var \Phalcon\Config $config
 */

use Phalcon\Loader;
use Phalcon\Mvc\Router;
use Phalcon\Mvc\Url as UrlResolver;
use Phalcon\Di\FactoryDefault;
use Phalcon\Session\Adapter\Files as SessionAdapter;
use Phalcon\Mvc\Model\Metadata\Memory as MetaDataAdapter;
use Phalcon\Mvc\View;
use Phalcon\Mvc\View\Engine\Volt as VoltEngine;
use Phalcon\Flash\Direct as FlashDirect;
use Phalcon\Flash\Session as FlashSession;

/**
 * The FactoryDefault Dependency Injector automatically registers the right services to provide a full stack framework
 */
$di = new FactoryDefault();

/**
 * Create a loader to register all files.
 */
$loader = new Loader();
$loader->registerNamespaces(array(
    'Apps' => APP_PATH . '/Apps/',
));
$loader->register();

/**
 * Registering a router
 */
$di->setShared('router', function () {
    $useSlugExtension = true;
    $router = new \Apps\Source\Plugins\Routing\Router($useSlugExtension);
    // $router = new \Apps\Source\Plugins\Routing\Router();

    $router->setDefaultModule('frontend');
    $router->setDefaultNamespace('\Apps\Modules\Frontend\Controllers');

    return $router;
});

/**
 * The URL component is used to generate all kinds of URLs in the application
 */
$di->setShared('url', function () use ($config) {
    $url = new UrlResolver();
    $url->setBaseUri($config->application->baseUri);

    return $url;
});

/**
 * Setting up the view component
 */
$di->setShared('view', function () use ($config) {

    $view = new View();

    $view->setViewsDir($config->application->viewsDir);

    $view->registerEngines(array(
        '.volt' => function ($view, $di) use ($config) {

            $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);

            $volt->setOptions(array(
                'compiledPath' => $config->application->cacheDir,
                'compiledSeparator' => '_'
            ));

            return $volt;
        },
        '.phtml' => function ($view, $di) use ($config) {

            $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);

            $volt->setOptions(array(
                'compiledPath' => $config->application->cacheDir,
                'compiledSeparator' => '_'
            ));
            // Add functions to the compiler:
            $volt->getCompiler()->addFunction(
                'in_array', 'in_array'
            );

            return $volt;
        }
    ));

    return $view;
});

/**
 * Database connection is created based in the parameters defined in the configuration file
 */
$di->setShared('db', function () use ($config) {
    $dbConfig = $config->database->toArray();
    $adapter = $dbConfig['adapter'];
    unset($dbConfig['adapter']);

    $class = 'Phalcon\Db\Adapter\Pdo\\' . $adapter;

    return new $class($dbConfig);
});

/**
 * If the configuration specify the use of metadata adapter use it or use memory otherwise
 */
$di->setShared('modelsMetadata', function () {
    return new MetaDataAdapter();
});

/**
 * Starts the session the first time some component requests the session service
 */
$di->setShared('session', function () {
    $session = new SessionAdapter();
    $session->start();

    return $session;
});

/**
 * Add authentication management
 */
$di->setShared('auth', function() use ($di) {
    $session = $di->get('session');
    $auth = \Apps\Source\Components\AuthManager::getInstance($session);
    return $auth;
});

/**
 * Date manager
 */
$di->setShared('date', function(){
    return new \Apps\Source\Plugins\Date\Manager();
});

/**
 * Register the direct flash service with the Twitter Bootstrap classes
 */
$di->set('flash', function () {
    return new FlashDirect(
        array(
            'error'   => 'alert alert-danger',
            'success' => 'alert alert-success',
            'notice'  => 'alert alert-info',
            'warning' => 'alert alert-warning'
        )
    );
});

/**
 * Register the session flash service with the Twitter Bootstrap classes
 */
$di->set('flashSession', function () {
    return new FlashSession(
        array(
            'error'   => 'alert alert-danger',
            'success' => 'alert alert-success',
            'notice'  => 'alert alert-info',
            'warning' => 'alert alert-warning'
        )
    );
});

/**
 * Set the default namespace for dispatcher
 */
$di->setShared('dispatcher', function() use ($di) {
    $eventsManager = new \Phalcon\Events\Manager();
    $eventsManager->attach("dispatch:beforeException", function($event, $dispatcher, $exception) {
        //Handle 404 exceptions
        if ($exception instanceof \Phalcon\Mvc\Dispatcher\Exception) {
            $dispatcher->forward(array(
                'namespace' => 'Apps\Modules\Frontend\Controllers',
                'module' => 'frontend',
                'controller' => 'error',
                'action' => 'show404'
            ));
            return false;
        }
    });
    $dispatcher = new \Phalcon\Mvc\Dispatcher();

    //Bind the EventsManager to the dispatcher
    $dispatcher->setEventsManager($eventsManager);

    return $dispatcher;
});