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

Just defined routes

Hi, I'm starting to set custom routes and works but I still can access through the controller name. I wish I could only access through the paths defined in the routing files. Sorry for my English

this is my file services.php

/**
 * Services are globally registered in this file
 *
 * @var \Phalcon\Config $config
 */

use Phalcon\Di\FactoryDefault;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Url as UrlResolver;
use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;
use Phalcon\Mvc\View\Engine\Volt as VoltEngine;
use Phalcon\Mvc\Model\Metadata\Memory as MetaDataAdapter;
use Phalcon\Session\Adapter\Files as SessionAdapter;
use Phalcon\Security;
use Phalcon\Mvc\Router as router;

$di = new FactoryDefault();

......

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

And this my file routes.php

//ruta para login de usuarios
$router->add("/login","login::index");

/*
 *rutas para catalogo asignaciones
 */
$router->add("/asignacionesss", "asignaciones::index");

Hi, can u describe much more what u try to accomplish?

hello, I can now access the controller and his actions in this way. / controller / action this without a defined routing file. I wish I could enter only through the paths defined in the routes.php

Just put false in constructor of router. It disabling this behaviour.



4.7k
Accepted
answer
edited Dec '15

:) First, to know u add router in right way- test did u have /login - did load controller login, action index (as u define in your routes.php). Next- if all its okey, check documentation - u can find that Router has some defaults like controler / action / id / params . You can stop default behaviour and put only your routes, but in that case u must add defaults too (depends/maybe): some like this:

<?php

$router = new Phalcon\Mvc\Router(false);
$router->removeExtraSlashes(true);

$router->setDefaultModule("admin");
$router->setDefaultNamespace("Admin\Controllers");
$router->setDefaultController('index');
$router->setDefaultAction('index');

// Basic Public
$router->add('/:controller', array(
    'controller' => 1,
    'action' => 'index'
));

$router->add('/:controller/:action', array(
    'controller' => 1,
    'action' => 2
));

$router->add('/:controller/:action/:params', array(
    'controller' => 1,
    'action' => 2,
    'params' => 3,
));

$router->add('/:controller/:action/([a-z0-9\-]+)/:params', array(
    'controller' => 1,
    'action' => 2,
    'slug' => 3,
    'params' => 4
));

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

return $router;

There is couple of ways to describe your routes (i think your is good- more short and clear).

Just an advice: do not create whole controller just for login- create one like account- inside u can have action login, logout, settings, etc.

Good luck :)

Did you ever heard about something like SOLID ? Putting login/logout in accountcontroller ? WTF ? Account controller is for managing your account. Not logging and logout it.

:) First, to know u add router in right way- test did u have /login - did load controller login, action index (as u define in your routes.php). Next- if all its okey, check documentation - u can find that Router has some defaults like controler / action / id / params . You can stop default behaviour and put only your routes, but in that case u must add defaults too (depends/maybe): some like this:

<?php

$router = new Phalcon\Mvc\Router(false);
$router->removeExtraSlashes(true);

$router->setDefaultModule("admin");
$router->setDefaultNamespace("Admin\Controllers");
$router->setDefaultController('index');
$router->setDefaultAction('index');

// Basic Public
$router->add('/:controller', array(
   'controller' => 1,
   'action' => 'index'
));

$router->add('/:controller/:action', array(
   'controller' => 1,
   'action' => 2
));

$router->add('/:controller/:action/:params', array(
   'controller' => 1,
   'action' => 2,
   'params' => 3,
));

$router->add('/:controller/:action/([a-z0-9\-]+)/:params', array(
   'controller' => 1,
   'action' => 2,
   'slug' => 3,
   'params' => 4
));

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

return $router;

There is couple of ways to describe your routes (i think your is good- more short and clear).

Just an advice: do not create whole controller just for login- create one like account- inside u can have action login, logout, settings, etc.

Good luck :)

edited Dec '15

...now i am wonder why i even try to answer u.

Perfect. Thanks.