Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 669303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:08:11+00:00 2026-05-14T00:08:11+00:00

I’m looking for a one line route to route dashed controller and method names

  • 0

I’m looking for a one line route to route dashed controller and method names to the actual underscored controller and method names.

For example the URL

/controller-name/method-name-which-is-long/

would route to

/controller_name/method_name_which_is_long/

see: http://codeigniter.com/forums/viewreply/696690/ which gave me the idea to ask 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T00:08:12+00:00Added an answer on May 14, 2026 at 12:08 am

    That is exactly my requirement too and I was using routes like

    $route['logued/presse-access'] = "logued/presse_access";
    

    In my previous project I needed to create 300-400 routing rules, most of them are due to dash to underscore conversion.

    For my next project I eagerly want to avoid it. I have done some quick hack and tested it, though have not used in any live server, its working for me. Do the following..

    Make sure the subclass_prefix is as follows in your system/application/config/config.php

    $config['subclass_prefix'] = 'MY_';
    

    Then upload a file named MY_Router.php in system/application/libraries directory.

    <?php
    
    class MY_Router extends CI_Router { 
        function set_class($class) 
        {
            //$this->class = $class;
            $this->class = str_replace('-', '_', $class);
            //echo 'class:'.$this->class;
        }
    
        function set_method($method) 
        {
    //      $this->method = $method;
            $this->method = str_replace('-', '_', $method);
        }
    
        function _validate_request($segments)
        {
            // Does the requested controller exist in the root folder?
            if (file_exists(APPPATH.'controllers/'.str_replace('-', '_', $segments[0]).EXT))
            {
                return $segments;
            }
            // Is the controller in a sub-folder?
            if (is_dir(APPPATH.'controllers/'.$segments[0]))
            {       
                // Set the directory and remove it from the segment array
                $this->set_directory($segments[0]);
                $segments = array_slice($segments, 1);
    
                if (count($segments) > 0)
                {
                    // Does the requested controller exist in the sub-folder?
                    if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().str_replace('-', '_', $segments[0]).EXT))
                    {
                        show_404($this->fetch_directory().$segments[0]);
                    }
                }
                else
                {
                    $this->set_class($this->default_controller);
                    $this->set_method('index');
    
                    // Does the default controller exist in the sub-folder?
                    if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
                    {
                        $this->directory = '';
                        return array();
                    }
    
                }
    
                return $segments;
            }
    
            // Can't find the requested controller...
            show_404($segments[0]);
        }
    }
    

    Now you can freely use url like http://example.com/logued/presse-access and it will call the proper controller and function by automatically converting dash to underscore.

    Edit:
    Here is our Codeigniter 2 solution which overrides the new CI_Router functions:

    <?php
    
    class MY_Router extends CI_Router { 
        function set_class($class) 
        {
            $this->class = str_replace('-', '_', $class);
        }
    
        function set_method($method) 
        {
            $this->method = str_replace('-', '_', $method);
        }
    
        function set_directory($dir) {
            $this->directory = $dir.'/';
        }
    
        function _validate_request($segments)
        {
            if (count($segments) == 0)
            {
                return $segments;
            }
    
            // Does the requested controller exist in the root folder?
            if (file_exists(APPPATH.'controllers/'.str_replace('-', '_', $segments[0]).'.php'))
            {
                return $segments;
            }
    
            // Is the controller in a sub-folder?
            if (is_dir(APPPATH.'controllers/'.$segments[0]))
            {
                // Set the directory and remove it from the segment array
                $this->set_directory($segments[0]);
                $segments = array_slice($segments, 1);
    
    
                while(count($segments) > 0 && is_dir(APPPATH.'controllers/'.$this->directory.$segments[0]))
                {
                    // Set the directory and remove it from the segment array
                    $this->set_directory($this->directory . $segments[0]);
                    $segments = array_slice($segments, 1);
                }
    
                if (count($segments) > 0)
                {
                    // Does the requested controller exist in the sub-folder?
                    if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().str_replace('-', '_', $segments[0]).'.php'))
                    {
                        if ( ! empty($this->routes['404_override']))
                        {
                            $x = explode('/', $this->routes['404_override']);
    
                            $this->set_directory('');
                            $this->set_class($x[0]);
                            $this->set_method(isset($x[1]) ? $x[1] : 'index');
    
                            return $x;
                        }
                        else
                        {
                            show_404($this->fetch_directory().$segments[0]);
                        }
                    }
                }
                else
                {
                    // Is the method being specified in the route?
                    if (strpos($this->default_controller, '/') !== FALSE)
                    {
                        $x = explode('/', $this->default_controller);
    
                        $this->set_class($x[0]);
                        $this->set_method($x[1]);
                    }
                    else
                    {
                        $this->set_class($this->default_controller);
                        $this->set_method('index');
                    }
    
                    // Does the default controller exist in the sub-folder?
                    if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.'.php'))
                    {
                        $this->directory = '';
                        return array();
                    }
    
                }
    
                return $segments;
            }
    
    
            // If we've gotten this far it means that the URI does not correlate to a valid
            // controller class.  We will now see if there is an override
            if ( ! empty($this->routes['404_override']))
            {
                $x = explode('/', $this->routes['404_override']);
    
                $this->set_class($x[0]);
                $this->set_method(isset($x[1]) ? $x[1] : 'index');
    
                return $x;
            }
    
    
            // Nothing else to do at this point but show a 404
            show_404($segments[0]);
        }
    }
    

    Now one has to place this file like application/core/MY_Router.php and make sure he has subclass_prefix is defined as $config['subclass_prefix'] = 'MY_'; in application/config/config.php

    Few extra lines of code has been added in method _validate_request():

    while(count($segments) > 0 && is_dir(APPPATH.'controllers/'.$this->directory.$segments[0]))
    {
        // Set the directory and remove it from the segment array
        $this->set_directory($this->directory . $segments[0]);
        $segments = array_slice($segments, 1);
    }
    

    It is used so that one can make use of multi-level subdirectory inside controllers directory, whereas normally we can use single level subdirectory inside controllers folder and can call it in url. One can remove this code if it not necessary but it has no harm on the normal flow.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 383k
  • Answers 383k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer HTML5 includes an embedded SQL database. You could write a… May 14, 2026 at 10:53 pm
  • Editorial Team
    Editorial Team added an answer Check out @Perspx's PXNavigationBar. The source is available on Github.… May 14, 2026 at 10:53 pm
  • Editorial Team
    Editorial Team added an answer I finally was able to solve the problem. I realized… May 14, 2026 at 10:53 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.