简体   繁体   中英

Phalcon: why router's routes are not being called?

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?

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 , you've to visit /legacy/api/v1 . 而是访问/legacy/api/v1 或者如果要访问 ,则必须访问/legacy/api/v1

Greetings!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM