Welcome Guest, Not a member yet? Register   Sign In
Functions have underscores, but I want the url to have dashes instead. What is the best solution?
#11

[eluser]pbreit[/eluser]
Is this a reasonable feature request for CodeIgniter? Considering that CodeIgniter touts friendly URLs as a feature, I would think this common and friendly URL structure would be supported.
#12

[eluser]Jelmer[/eluser]
CI has it's defaults. If you're going to built in alternatives for everything people might possibly want you're going to waste loads of resources. And as I've shown it's easily changed using _remap() so CI does support it - just not out of the box.
#13

[eluser]pbreit[/eluser]
I wasn't suggesting that CodeIgniter support everything people might want. I was just wondering if it might make sense for it to support a very common, user-friendly URL structure. To a newbie, it's not obvious why it would not be supported.
#14

[eluser]Unknown[/eluser]
This will convert only class names (controllers) and class method names (functions) in the url from dashes to underscores as per CodeIgniter's segment-based approach: "example.com/class/function/ID". You can remove the appropriate section to apply to only class or method names, refer to "LINE ADDED" and "LINE MODIFIED" in the code below. CodeIgniter v1.7.2.

Extend the native Router library class

Add file contents below to: "system/application/libraries/MY_Router.php" where "MY_" is the $config['subclass_prefix'] in "system/application/config/config.php", modifying the paths, filename and class name if applicable.

You could add config parameters to check,
eg: $config['uri_class_dashes_to_underscores'] = TRUE;
and
eg: $config['uri_function_dashes_to_underscores'] = TRUE;

to "system/application/config/config.php" and the check using: eg: $this->config->item('uri_class_dashes_to_underscores');

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter
*
* An open source application development framework for PHP 4.3.2 or newer
*
* @package        CodeIgniter
* @author        ExpressionEngine Dev Team
* @copyright    Copyright (c) 2008 - 2009, EllisLab, Inc.
* @license        http://ellislab.com/codeigniter/user-guide/license.html
* @link        http://codeigniter.com
* @since        Version 1.0
* @filesource
*/

// ------------------------------------------------------------------------

/**
* MY Router Class
*
* Parses URIs and determines routing
*
* @package        CodeIgniter
* @subpackage    Libraries
* @author        ExpressionEngine Dev Team
* @category    Libraries
* @link        http://ellislab.com/codeigniter/user-guide/general/routing.html
*/
class MY_Router extends CI_Router {
    /**
     * Set the Route
     *
     * This function takes an array of URI segments as
     * input, and sets the current class/method
     *
     * @access    private
     * @param    array
     * @param    bool
     * @return    void
     */
    function _set_request($segments = array())
    {
        $segments = $this->_validate_request($segments);

        if (count($segments) == 0)
        {
            return;
        }

        $this->set_class($segments[0]);

        if (isset($segments[1]))
        {
            // A scaffolding request. No funny business with the URL
            if ($this->routes['scaffolding_trigger'] == $segments[1] AND $segments[1] != '_ci_scaffolding')
            {
                $this->scaffolding_request = TRUE;
                unset($this->routes['scaffolding_trigger']);
            }
            else
            {
                // A standard method request
                // LINE MODIFIED TO ALLOW CLASS METHODS (FUNCTIONS) IN THE URL TO BE CONVERTED FROM DASHES TO UNDERSCORES //
                // $this->set_method($segments[1]);
                $this->set_method(str_replace('-', '_', $segments[1]));
                // ***************************************************************************************************** //
            }
        }
        else
        {
            // This lets the "routed" segment array identify that the default
            // index method is being used.
            $segments[1] = 'index';
        }

        // Update our "routed" segment array to contain the segments.
        // Note: If there is no custom routing, this array will be
        // identical to $this->uri->segments
        $this->uri->rsegments = $segments;
    }

    // --------------------------------------------------------------------

    /**
     * Validates the supplied segments.  Attempts to determine the path to
     * the controller.
     *
     * @access    private
     * @param    array
     * @return    array
     */
    function _validate_request($segments)
    {
        // LINE ADDED TO ALLOW CLASS NAMES (CONTROLLERS) IN THE URL TO BE CONVERTED FROM DASHES TO UNDERSCORES //
        $segments[0] = str_replace('-', '_', $segments[0]);
        // *************************************************************************************************** //

        // Does the requested controller exist in the root folder?
        if (file_exists(APPPATH.'controllers/'.$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)
            {
                // LINE ADDED TO ALLOW CLASS NAMES (CONTROLLERS) IN THE URL TO BE CONVERTED FROM DASHES TO UNDERSCORES //
                $segments[0] = str_replace('-', '_', $segments[0]);
                // *************************************************************************************************** //

                // Does the requested controller exist in the sub-folder?
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$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]);
    }

}
// END MY Router Class

/* End of file MY_Router.php */
/* Location: ./system/libraries/MY_Router.php




Theme © iAndrew 2016 - Forum software by © MyBB