Welcome Guest, Not a member yet? Register   Sign In
Controller Names with Dashes or Underscores?
#11

[eluser]Dam1an[/eluser]
@narcisha: The URL friendly part of the URL is normally the 3rd part, such as the name of a blog post etc (it may appear as the first or second, but its not really)

I've never found the need to use hyphens or underscores in controller names/functions, just in the parameters
#12

[eluser]aidehua[/eluser]
Forget search engine friendliness for a minute. I think

Quote:example.com/about-us
is more human-friendly than

Quote:example.com/about_us

For now I'm using routes to achieve this:

Code:
$route['about-us'] = "about_us";

But it could get tedious if I have dozens of pages to set up like that:

Code:
$route['about-us'] = "about_us";
$route['contact-us'] = "contact_us";
$route['terms-and-conditions'] = "terms_and_conditions";
etc.

(You might say that these are trivial examples, and the pages could simply be re-named "about", "contact", and "terms" with no loss of usability - but there will be cases when I really do want to keep the dashes in the URI.)

And what if I'm creating pages (and URIs) dynamically through a content management system? Should I be writing a new line to my routes.php file automatically when I create a new page through the CMS? That feels a bit clunky.

If I were clever with regular expressions, maybe I could write something in routes.php that does this:

Code:
$route['[string-with-dashes-in-first-or-second-uri-segment'] = "[string-with-underscores-in-first-or-second-uri-segment]";

But I'm not clever with regular expressions.

(Anyone?)

Or would it be better to do this by customising the router library?
#13

[eluser]Stephen G[/eluser]
Hi :-) Tongue :-) :-) :-)

I made this change to the systems routers file and it seems to work great!!! =O)

Code:
//STEVE CHANGED
// from: $segments = $this->_validate_request($segments);
// to: $segments = $this->_validate_request(str_replace("-", "_", $segments);
        
$segments = $this->_validate_request(str_replace("-", "_", $segments));
#14

[eluser]sumanchalki[/eluser]
Hi guys!

I found a solution of this and posted in stackoverflow.com. Here is the link.

http://stackoverflow.com/questions/24281...05#2432405
#15

[eluser]e-mike[/eluser]
Code:
<?php
class MY_Router extends CI_Router
{
    function _set_request($segments = array()) {
        parent::_set_request(str_replace('-', '_', $segments));
    }
}
?>

Put this file MY_Router.php inside /application/libraries (CI1) or /application/core (CI2)
Remember that this will effect all segments, not only module, controller and method.

Alternative to this extend is to add each segment to router.php
$route['this-is-a-module-or-controler'] = 'this_is_a_module_or_controller';

As you can see the extend method would be easier to use. You can choose to make the function also to handle only the first two or three segments so that the other segments are not affected with the _ replacement.
#16

[eluser]Unknown[/eluser]
Hello everyone, here is my solution on CI version: 2.0.2

.... extend Route


Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

//PATCHES 404_override issues with autoloading libraries, see 3 lines below

class MY_Router extends CI_Router {
  
  
  
   /**
     * Validates the supplied segments.  Attempts to determine the path to
     * the controller.
     *
     * @access    private
     * @param    array
     * @return    array
     */
    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]).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
            {
                // 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.EXT))
                {
                    $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;
        }


       /** @Daryl: redirects to a central controller, allows you to avoid writing a controller for each URL (class / method)  **/
       $this->set_class('static_content');
       $this->set_method('index');    
       return array('static_content', 'index', $segments[0]);
      
        
        
        // Nothing else to do at this point but show a 404
        show_404($segments[0]);
    }
    
    /**
    * @Daryl: override
    *
    */
    function set_class($class)
    {
       $class_n = str_replace(array('/', '.'), '', $class);  
       $class_n = str_replace('-', '_', $class_n);  
       //echo $class_n."<br />";
       $this->class = $class_n;
    }
    
    /**
    * @Daryl: override
    *
    */
    function set_method($method)
    {
        $method_n = str_replace('-', '_', $method);
        //echo $method_n."<br />";
        $this->method = $method_n;
    }
    
    
}




Theme © iAndrew 2016 - Forum software by © MyBB