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.

All routes lead to IndexController.

I am trying to make a simple route file.

/config/routes.php


<?php

use Phalcon\Mvc\Router;

// Create the router
$router = new Router();

// Define a route
$router->addGet(
    '/hello',
    [
        'controller' => 'welcome',
        'action'     => 'hello',
    ]
);

// Another route
$router->addGet(
    '/bye',
    [
        "controller" => "welcome",
        'action'     => 'bye',
    ]
);

$di = new \Phalcon\DI\FactoryDefault();
$router = new Router(false);
$router->setDI($di);

$router->setUriSource(\Phalcon\Mvc\Router::URI_SOURCE_SERVER_REQUEST_URI);

$router->handle();

I have two controllers with actions exactly as routes.php file shows, but whatever I try to curl, it always takes me to IndexController.

I also have .htrouter and .htaccess files from this tutorial https://docs.phalconphp.com/cs/3.3/tutorial-base

What do I do wrong?

My index.php:


<?php

use Phalcon\Loader;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Application;
use Phalcon\Di\FactoryDefault;
use Phalcon\Mvc\Url as UrlProvider;

// Define some absolute path constants to aid in locating resources
define('BASE_PATH', dirname(__DIR__));
define('APP_PATH', BASE_PATH . '/app');
// ...

$loader = new Loader();

$loader->registerDirs(
    [
        APP_PATH . '/controllers/',
        APP_PATH . '/models/',
        APP_PATH . '/config/',
    ]
);

$loader->register();

// Create a DI
$di = new FactoryDefault();

// Setup the view component
$di->set(
    'view',
    function () {
        $view = new View();
        $view->setViewsDir(APP_PATH . '/views/');
        return $view;
    }
);

$di->set('router', function(){
    require APP_PATH.'/config/routes.php';
    return $router;
});

// Setup a base URI
$di->set(
    'url',
    function () {
        $url = new UrlProvider();
        $url->setBaseUri('/');
        return $url;
    }
);

$application = new Application($di);

try {
    // Handle the request
    $response = $application->handle();

    $response->send();
} catch (\Exception $e) {
    echo 'Exception: ', $e->getMessage();
}

I use php -S to serve my project.

Thanks in advance. I cannot move further.



268
Accepted
answer

Solved by adding this

$di = new \Phalcon\DI\FactoryDefault();
$router = new Router(false);
$router->setDI($di);

$router->setUriSource(\Phalcon\Mvc\Router::URI_SOURCE_SERVER_REQUEST_URI);

to the top of the file not the bottom