Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Phalcon response redirect not working

Hi guys,

I'm new to Phalcon and I'm using mvc/multiple (which I download from here: https://github.com/phalcon/mvc) example to explore things. In the mvc/multiple example, I have added:

<?php

    namespace Multiple\Frontend\Controllers;

    class IndexController extends \Phalcon\Mvc\Controller 
    {

    public function indexAction()
    {
            // I have added the below test code

              $this->view->disable();

            $response = new \Phalcon\Http\Response();

            $response->redirect("http://www.google.com",TRUE)

            echo "If this page is not redirected then it will display this";

    }
}

And I haven't modified anything else. All other things are kept intact. The redirect is not working and it's displaying the echo message. I have tried removing TRUE also so that Phalcon may automatically detect it as an external url. But still the same.

Phalcon Version: 1.3.2 
OS                     : Windows 7 32 bit
PHP version       : 5.5 (installed using XAMPP) [Phalcon installed perfectly using DLL]

What am I doing wrong here? Kindly provide a solution.

Hi,

You should return the object \Phalcon\Http\Response:

<?php

namespace Multiple\Frontend\Controllers;

class IndexController extends \Phalcon\Mvc\Controller 
{

    public function indexAction()
    {
        return $this->response->redirect("http://www.google.com",TRUE);
    }
}


2.6k
edited Jul '14

Thank you very much. That worked. But now I want to redirect it to Backend module which has the namespace Multiple\Backend\Controllers . I have a router like

$router->add("/login", array(
    'namespace' => 'Multiple\Backend\Module',
    'controller' => 'login',
    'action' => 'index',
))->setName("login");

When I try a redirect like this:

return $response->redirect(array("for"=>"login"));

it throws Phalcon\Mvc\Dispatcher\Exception: Multiple\Backend\Module\LoginController handler class cannot be loaded .

I have dispatchers in both front -end and backend module like this:

For front-end in Module.php of frontend folder

$di->set('dispatcher', function () {
    $dispatcher = new \Phalcon\Mvc\Dispatcher();
    $dispatcher->setDefaultNamespace("Multiple\Frontend\Controllers\\");
    return $dispatcher;
});

For backend-end in Module.php of backend folder

$di->set('dispatcher', function () {
    $dispatcher = new \Phalcon\Mvc\Dispatcher();
    $dispatcher->setDefaultNamespace("Multiple\Backend\Controllers\\");
    return $dispatcher;
});

In the boootstrap index.php file I have added this router:

$di->set('router', function(){

    $router = new \Phalcon\Mvc\Router();

    $router->setDefaultModule("frontend");

    $router->add('/:module/:controller/:action', array(
                'module' => 1,
                'controller' => 2,
                'action' => 3,
    ));

    $router->add("/login", array(
        'namespace' => 'Multiple\Backend\Module',
        'controller' => 'login',
        'action' => 'index',
    ))->setName("login");


    return $router;
});

I think the router is working and it is going to backend module but still see an exception.. If I change the

$router->setDefaultModule("backend"); // changed to backend

it goes to backend. but I'm not sure how to redirect it from one module to another module

Could you please explain this? Thanks you very much for answering my question

Can you push your code to github?



2.6k

Hi,

Sorry for the delay had a power cut. Here is the code : https://github.com/anamica90/sample



3.6k
Accepted
answer

Ok,

I fix your router. The problem is with the namespace in line 41. https://github.com/anamica90/sample/pull/1/files#diff-563900aa1d5ec7bc5c37742df25fbf50L41



2.6k
edited Jul '14

Thanks a lot. :) now I'm understand one thing. Make sure to check what I am going to post is correct or not before posting it. :)