We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 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 :

header("location:/");

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

<?php

    try {

    //Register an autoloader
    $loader = new \Phalcon\Loader();
    $loader->registerDirs(
        array(
            '../app/controllers/',
            '../app/models/'
            )
        )->register();    

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

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

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

        $view->setViewsDir('../app/views/');

        return $view;

    }, true);

    $app->setDI($di);

    $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");
        }      
        else{
            //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
            // https://localhost/public/index/index

            $app->response->redirect("index/index")->sendHeaders();
        }
    });

    $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");
            $app->response->send();
        }
        //We render the index page, which will run the ajax call that will surely check 
        // if it's a real Page not found error.
        else{
            echo $app['view']->render("index/index");
        }
    });

    $app->handle();

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


98.9k

Try adding a definition for the URL service:

$di['url'] = function(){
        $url = new \Phalcon\Mvc\Url();
        $url->setBaseUri('/');
        return $url;
};