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

How to enable dashes in modules in url?

I have following codes for routes.

$moduleName = "usermanagement";
$router->add('/'.$moduleName,array(
    'module' => $moduleName,
    'namespace'=>'Mynamespace\\'.ucwords($moduleName).'\Controllers\\',
    'controller'=>"index",
    'action' => 'index'
));

The url https://www.mydomain.com/usermanagement works pretty well. But however I have do a little change in url so that https://www.mydomain.com/user-management can be used to access the module. I have seen allowing dashes in actions but haven't found anything like such for modules.

Can anybody help me on this??

Thanks



85.5k
edited May '18

i use https://docs.phalcon.io/uk/3.3/routing#defining-groups-of-routes to handle my multi module routing

here is a copy of my ModuleRoutes.php

<?php

namespace Whatever\Products;

use \Phalcon\Mvc\Router\Group;

/**
 * This class defines routes for the Whatever\Products module
 * which will be prefixed with '/:lang/products/'
 */
class ModuleRoutes extends Group
{
    /**
     * Initialize the router group for the Product module
     */
    public function initialize() 
    {

        $this->setPrefix('/{language:[a-z]{2}}/products');

        /**
         * Configure the instance
         */
        $this->setPaths(
            [
            'module' => 'products',
            'controller' => 'index',
            'action' => 'index'
            ]
        );

        $this->add('/{name}/{id}', [
            'controller' => "index",
            'action'     => "index",
        ])->setName('products-action');

        /*$this->add('/:controller/:action/:params', [
            'controller' => 2,
            'action'     => 3,
            'params'     => 4
        ])->setName('products-action');*/
    }
}

Thanks for the reply, but it seem the different than what I have asked. Multi module is working fine, I just want to enable dashes in module name So that https://www.mysite.com/usermanagement can be accessed as https://www.mysite.com/user-management

i use https://docs.phalcon.io/uk/3.3/routing#defining-groups-of-routes to handle my multi module routing

here is a copy of my ModuleRoutes.php

<?php

namespace Whatever\Products;

use \Phalcon\Mvc\Router\Group;

/**
* This class defines routes for the Whatever\Products module
* which will be prefixed with '/:lang/products/'
*/
class ModuleRoutes extends Group
{
   /**
    * Initialize the router group for the Product module
    */
   public function initialize() 
   {

       $this->setPrefix('/{language:[a-z]{2}}/products');

       /**
        * Configure the instance
        */
       $this->setPaths(
           [
           'module' => 'products',
           'controller' => 'index',
           'action' => 'index'
           ]
       );

       $this->add('/{name}/{id}', [
           'controller' => "index",
           'action'     => "index",
       ])->setName('products-action');

       /*$this->add('/:controller/:action/:params', [
           'controller' => 2,
           'action'     => 3,
           'params'     => 4
       ])->setName('products-action');*/
   }
}


85.5k

well you can make any url open any module name ?

a route like a.com/hjsadufghsduogsdgohsdfgiofsdhogsd-123432-423 can open a module called user without any issues ?

Can you please tell me how to do that?

well you can make any url open any module name ?

a route like a.com/hjsadufghsduogsdgohsdfgiofsdhogsd-123432-423 can open a module called user without any issues ?



85.5k
Accepted
answer
edited May '18

     $this->setPrefix('/hjsadufghsduogsdgohsdfgiofsdhogsd-123432-423');

       /**
        * Configure the instance
        */
       $this->setPaths(
           [
           'module' => 'users',
           'controller' => 'index',
           'action' => 'index'
           ]
       );

you are providing 'module' in your router config as well. Perhaps even this would solv your problem


$routes = [
    "usermanagment" => "user-managment"
];

foreach ($routes as $module => $url ) {
$router->add('$url ,array(
    'module' => $module,
    'namespace'=>'Mynamespace\\'.ucwords($module).'\Controllers\\',
    'controller'=>"index",
    'action' => 'index'
));
}

Thanks, I think this solved my problem, haven't tested much but seems it is working. Thanks buddy


    $this->setPrefix('/hjsadufghsduogsdgohsdfgiofsdhogsd-123432-423');

      /**
       * Configure the instance
       */
      $this->setPaths(
          [
          'module' => 'users',
          'controller' => 'index',
          'action' => 'index'
          ]
      );

you are providing 'module' in your router config as well. Perhaps even this would solv your problem


$routes = [
  "usermanagment" => "user-managment"
];

foreach ($routes as $module => $url ) {
$router->add('$url ,array(
   'module' => $module,
   'namespace'=>'Mynamespace\\'.ucwords($module).'\Controllers\\',
   'controller'=>"index",
   'action' => 'index'
));
}

FYI, this is the solution I ended up using :

$router->add('/([a-zA-Z0-9\_\-]+)', [
    'controller'    => 1,
    'action'        => "index"
]);

$router->add('#^/([a-zA-Z0-9\_\-]+)[/]{0,1}$#', [
    'controller' => 1,
]);

$router->add('#^/([a-zA-Z0-9\_\-]+)/([a-zA-Z0-9\_\-]+)(/.*)*$#', [
    'controller' => 1,
    'action' => 2,
    'params' => 3,
]);