default route not working in phalcon or may i don't know how to do it.

Hi,

Below is the code of routing which I have added to my app, but the default route is not picking the / default route for home, what is the issue , and notfound working when default route / called, any idea?

<?php

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

/**
 * Nfi routes
 */


$router->add("/", array(
    'module' => 'site',
    'namespace' => 'FZ\Site\Controllers\\',
    'controller' => 'home',
    'action' => 'index'
));


$router->add("/:controller", array(
    'module' => 'site',
    'namespace' => 'FZ\Site\Controllers\\',
    'controller' => 1,
    'action' => 'index'
));

$router->add("/:controller/", array(
    'module' => 'site',
    'namespace' => 'FZ\Site\Controllers\\',
    'controller' => 1,
    'action' => 'index'
));

//frontend website module set
$router->add("/:controller/:action/:params", array(
    'module' => 'site',
    'namespace' => 'FZ\Site\Controllers\\',
    "controller" => 1,
    "action" => 2,
    "params" => 3,
));


$router->add("/nfi", array(
    'module' => 'nfi',
    'namespace' => 'FZ\Nfi\Controllers\\',
    'controller' => "session",
    'action' => 'index'
));


$router->add("/nfi/", array(
    'module' => 'nfi',
    'namespace' => 'FZ\Nfi\Controllers\\',
    'controller' => "session",
    'action' => 'index'
));


$router->add("/nfi/:controller", array(
    'module' => 'nfi',
    'namespace' => 'FZ\Nfi\Controllers\\',
    'controller' => 1,
    'action' => 'index'
));


$router->add("/nfi/:controller/", array(
    'module' => 'nfi',
    'namespace' => 'FZ\Nfi\Controllers\\',
    'controller' => 1,
    'action' => 'index'
));


$router->add("/nfi/:controller/:action/:params", array(
    'module' => 'nfi',
    'namespace' => 'FZ\Nfi\Controllers\\',
    "controller" => 1,
    "action" => 2,
    "params" => 3,
));

//Set 404 paths
 $router->notFound(array(
    'module' => 'site',
    'namespace' => 'FZ\Site\Controllers\\',
    'controller' => 'error',
    'action' => 'fof'
));

return $router;


81.7k
edited Aug '14

Are you registering the router service like this one: https://github.com/phalcon/forum/blob/master/app/config/services.php#L189

What URI are you accessing that is not being matched by the router?

edited Aug '14

yes I'm registering the router service same as you mentioned in the link, and currently I'm working in the localhost so my baseuri is '/' but router is not working, but amazingly it's working on live enviorment, so what is the issue in localhost enviornment. do you have any idea.

Thanks

edited Aug '14

Sorry for promoting my plugin, but i think that is a simple and fast solution how to resolve routes, without definition lot of routes.

If you have, app as module, if not make the app as multi-module with one module and it will be works fine.

AutoRoute Plugin

If you will be have some problems with implementation or with reading documentation ( because english in documentation is not best ) feel free to contact me on skype: softdream2122



81.7k

What URI are you accessing that is not being matched by the router?

edited Aug '14

@Muhammad Adil

And update your routes, it doesnt needed to have duplicate routes for definition with "/" at the end of url just use:

$router->add("/nfi[/]", array(
    'module' => 'nfi',
    'namespace' => 'FZ\Nfi\Controllers\\',
    'controller' => "session",
    'action' => 'index'
));

So back to your problem the problem it could be in the order of your routes, i don't know how exactly work routes in phalcon, but i think that strict definition must be first and then general for example try to put all definitions with "/nfi" to top and definitions like: "/:controller" below

@Phalcon

My base uri is "/"

and 1 more thing i created folder for media on the root folder but unable to access this media/docs/images. like localhost/project/media/docs/images/image.jpg Thanks

Adil

Back to discussion. Have the similar problem: annotations router without default routing scheme ignores homepage route.

One action is set as notFound:

$router->notFound([
    'controller' => 'index',
    'action' => 'notFound'
]);

Another one (IndexController::indexAction) annotated with @Route("/"), its controller with @RoutePrefix(""), controller added to router with addResource.

Next I try to open homepage (like mydomain.com/) - and notFoundAction executed, ignoring indexAction for some reason. Any other URL (for example, if I change @Route("/") to @Route("/test") and try to open mydomain.com/test) works fine.

Same problem here. Was anyone able to solve this ?

IIRC, that year I did a workaround for this:

  • homepage action is defined with a /homepage route, not a /
  • changed index.php to replace / with /homepage when passing requested URL to router

But this can be fixed in current Phalcon versions.