We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

How to redirect to index using Phalcon php Micro application and custom routing

I'm building an Ajax driven web application using Php and the Phalcon framework.

When a user access the website, his request will be handle in my pageNotFound method, which will render the index.php view, and this view will call my server adding the Page prefix to the current url to load HTML in a div.

So www.mywebsite.com/home will call my server www.mywebsite.com/Page/Home which will render the html in the div.

I want to redirect the user to the home page if he's trying to access the Page url directly from it's url input in its browser.

But when I try to redirect him, the url becomes : ww.mywebsite.com/public/index/index

Do someone know why Phalcon his doing this ? And what could be a possible solution to my problem ?

The only way I was able to do what I wanted what with the following code :


Here's my original code. StackOverflow : http://stackoverflow.com/questions/22444295/how-to-redirect-to-index-using-phalcon-php-micro-application-and-custom-routing


    try {

    //Register an autoloader
    $loader = new \Phalcon\Loader();

    //Create a DI
    $di = new Phalcon\DI\FactoryDefault();
    $app = new \Phalcon\Mvc\Micro();

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

        $view = new Phalcon\Mvc\View\Simple();


        return $view;

    }, true);


    $app->map('/', function () use ($app) { 
        echo $app['view']->render("index/index");

    $app->map('/Page/Home', function () use ($app) {  
        if ($app->request->isAjax()) {
            echo $app['view']->render("page/Home");
            //It means that the user is trying to access the page manually in the url input
            //I would like to redirect him to my index page / page not found.
            //But when I try to do it redirect me to the following url
            // http://localhost/public/index/index


    $app->notFound(function () use ($app) {          
        //Ajax "Page" request that is not found
        if ($app->request->isAjax() && $app->request->isGet()) {
            echo $app['view']->render("page/pageNotFound");
        //Ajax "API" request that is not found
        elseif ($app->request->isAjax() && $app->request->isPost()) {
            $app->response->setStatusCode(400, "Bad Request");
        //We render the index page, which will run the ajax call that will surely check 
        // if it's a real Page not found error.
            echo $app['view']->render("index/index");


    } catch(\Phalcon\Exception $e) {
       echo "PhalconException: ", $e->getMessage();


Try adding a definition for the URL service:

$di['url'] = function(){
        $url = new \Phalcon\Mvc\Url();
        return $url;