Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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 http://www.mydomain.com/usermanagement works pretty well. But however I have do a little change in url so that http://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



78.1k
edited May '18

i use https://docs.phalconphp.com/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 http://www.mysite.com/usermanagement can be accessed as http://www.mysite.com/user-management

i use https://docs.phalconphp.com/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');*/
   }
}


78.1k

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 ?



78.1k
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'
));
}