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

index.phtml view is being loaded for all routes

For the life of me I can't get my views to work correctly. Any route I access will result in the index.phtml route to load.

My routes appear to work because if I do die() inside the Track::single route it will halt the script.

router.php

use Phalcon\Mvc\Router;

$router = new Router();

$router->add("/track/{slug: [0-9a-zA-Z-]+}", "Track::single");
$router->add("/download/{key: [0-9a-zA-Z-]+}", "Track::download");

$router->handle();

index.php

....

// Setup the view
$di->set('view', function() use ($config){

    // Create an events manager
    $eventsManager = new EventsManager();
    $eventsManager->attach('view:afterRender', new BanWordsPlugin());

    $view = new View();
    $view->setViewsDir($config->phalcon->viewsDir);

    $view->setVars(array(
        'title' => $config->site->title
    ));

    $view->setEventsManager($eventsManager);

    return $view;
});

require '../app/router.php';
$di->set('router', $router);

...

TrackController

/*
 * Selects a single track based on it's slug value
 */
public function singleAction()
{
    $slug = $this->dispatcher->getParam('slug');
    $ip = $this->request->getClientAddress();
    $timestamp = time();

    /*
     *  Fetch the track from the database
     */
    $track = Tracks::findFirstBySlug($slug);

    /*
     *  Check if track exists, redirect to 404 if not
     */

    /*
     *  Encrypt the slug, IP address and timestamp
     */
    $crypt = new Crypt();

    $downloadHash = $crypt->encryptBase64(
        sprintf("%s{%s{%s", $track->id, $ip, $timestamp),
        $this->config->security->key);

    /*
     *  Set the view paramaters
     */
    $this->view->downloadHash = $downloadHash;
}

My view is located in app/views/track/single.phtml

If I load /track/single/fetty-wap-trap-queen-98391 the right controller and action is used, but it's still loading the view from /app/views/index.phtml

Any ideas what I'm doing wrong?

Remove that file :)

Removing index.phtml works. But I'm still having the issue with the routes not working at all.

... What about routes? Can u provide more details.... and how u make your links and what u get from them?



17.5k

This was happening to me because I did not have mod-rewrite enabled in apache.