0

I have the following index.php:

<?php

use Phalcon\Loader;
use Phalcon\Mvc\View;
use Phalcon\DI\FactoryDefault;
use Phalcon\Mvc\Url as UrlProvider;
use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;
use Phalcon\Mvc\Micro;

try {
    // Register an autoloader
    $loader = new Loader();
    $loader->registerDirs(array(
        __DIR__ . '/controllers/',
        __DIR__ . '/models/'
    ))->register();

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

    $app->setDI($di);

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

    // Setup a base URI so that all generated URIs include the "legacy" folder
    $di->set('url', function () {
        $url = new UrlProvider();
        $url->setBaseUri('/legacy/api/v1/');
        return $url;
    });

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

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

        $router->add('/', array(
            "controller" => "site",
            "action" => "index"
        ));

        $router->notFound(
                [
                    "controller" => "site",
                    "action" => "ping"
                ]
        );
        var_dump($router);

        return $router;
    });

    $app->handle();
} catch (\Exception $e) {
    // Do Something I guess, return Server Error message
    $app->response->setStatusCode(500, "Server Error");
    $app->response->setContent($e->getMessage());
    $app->response->send();
}

and the following structure: --api

----v1

------config

-------- routes.php

------controllers

--------SiteController.php

------models

------views

The problem is that I think my application ignores the router, because I'm getting the following error: if I navigate to "/" - Matched route doesn't have an associated handler. While if I go to some random location like "/12321" it returns Not-Found handler is not callable or is not defined.

Any idea what I'm doing wrong?

AngryChicken
  • 360
  • 2
  • 16

2 Answers2

0

Remember that you're setting as base uri /legacy/api/v1/ so all your routes are intended to be appended to that.

 $di->set('url', function () {
     $url = new UrlProvider();
     $url->setBaseUri('/legacy/api/v1/');
     return $url;
 });

You can't simply access: / but instead, you've to visit /legacy/api/v1/ or if you want to visit /user, you've to visit /legacy/api/v1/user.

Greetings!

soutoner
  • 571
  • 2
  • 14
  • I don't know because I wasn't able to get the `router` DI to work with a `Micro` app. I think the `Micro` app behavior is not very well explained in the docs. I use [Micro collections](https://docs.phalconphp.com/es/latest/api/Phalcon_Mvc_Micro_Collection.html) for routes, I think is the way to go. – soutoner Jan 05 '16 at 11:04
0

I also had some problems with phalcon (1.3.x) routes too. Here's what I did to make them working the way I wanted. Took me a long time to figure that out:

/** @var \Phalcon\Mvc\Router $router */
$router = new Phalcon\Mvc\Router(FALSE); // does not create default routes
$router->removeExtraSlashes(TRUE);

$router->setDefaults(array(
    'namespace' => 'Namespace\Controller',
    'controller' => 'index',
    'action' => 'index'
));

$router->notFound(array(
    'controller' => 'index',
    'action' => 'show404'
));

Hope it helps.

dompie
  • 711
  • 1
  • 7
  • 12