Reputation: 1021
I'd like to
In the following example fudev is the subdirectory off the root, and preview is the directory that codeigniter is in.
Currently: http://devsite.com/fudev/preview/index.php/controllername/method_name.
I wish have:
http://devsite.com/fudev/preview/method-name
Any ideas? I had the following in config/routes.php to change the method name to hyphens
$route['controllername/method-name'] = 'controllername/method_name';
..But that's only any good when I include controllername, which I want to omit.
Any ideas?
Thanks in advance..
Jon.
Upvotes: 1
Views: 1041
Reputation: 7902
You can change the url to accept hyphens by extending the Router class.
Create a file called MY_Router.php in application/core (assuming you haven't changed the default extension prefix which is set in your config.
The code for this file is;
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
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 _validate_request($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);
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')) {
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.'.php')) {
$this->directory = '';
return array();
}
}
return $segments;
}
// Can't find the requested controller...
show_404($segments[0]);
}
}
This will remap all hyphens to underscores for your controllers, no need to mess around with htaccess.
Upvotes: 0