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

Routing doesn't work

I've created simple application by Phalcon Dev Tools.

I've added created router.php under condig directory:

<?php

/*
* Define custom routes. File gets included in the router service definition.
*/

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

$router->add("/upload.\php", array(
    'controller' => 'upload',
    'action'     => 'index'
));

return $router;

I've set up dispatched in services:

$di->set('dispatcher', function () use ($di) {
    $eventsManager = new EventsManager;

    /**
     * Check if the user is allowed to access certain action using the SecurityPlugin
     */

    $eventsManager->attach('dispatch:beforeDispatch', new SecurityPlugin);

    /**
     * Handle exceptions and not-found exceptions using NotFoundPlugin
     */

    $eventsManager->attach('dispatch:beforeException', new NotFoundPlugin);

    $dispatcher = new Dispatcher;
    $dispatcher->setEventsManager($eventsManager);

    return $dispatcher;
});

And router:

/**
 * Loading routes from the routes.php file
 */
$di->set('router', function () {
    return require __DIR__ . '/routes.php';
});

However everytime I'm trying to get to upload.php I'm redirected to log in page.

I've tried to change router to:

$router->add("/upload.\php", array(
    'controller' => 'dashboard',
    'action'     => 'index'
));

To test if it works but even it doesn't redirect me to dashboard page.

What I' doing wrong?



17.5k
edited Sep '15

Change your route to:

    $router->add("/upload", [
        'controller' => 'upload',
        'action' => 'index'
    ]);

Then inside of UploadController:

    public function indexAction() {
        //perform the redirect here
    }

If you do a redirect, you'll have to use Response. There is an example of redirects here in activateAction(): https://github.com/zhegwood/phalcon.angular.bootstrap/blob/master/app/controllers/NoauthController.php