Skip to content
This repository

Feature use dashes #642

Closed
wants to merge 3 commits into from

4 participants

Joe Cianflone Timothy Warren Joseph Wynn Andrey Andreev
Joe Cianflone

Switch between dashes or underscores for your urls. I had submitted a pull request for this a while back, but it had a couple of bugs and some extra code in there that should not have been pushed, so I'm resubmitting.
Couple of things people had questions on it before:

  • If use_dashes is set to true urls with underscores will bring up a 404, so no duplicate content issues.
  • variables are not checked so you can still have something like x.com/controller-name/func/_id101 if necessary
  • folders aren't checked either so name them however you want.
Timothy Warren

Personally, I think it would be better if the underscore urls redirected to the dashed url, rather than throwing a 404.

Joseph Wynn

A 301 redirect makes sense, but maybe this should be configurable as some people might prefer the 404.

Commits look much cleaner this time, except for the extra line in the Welcome controller?

Joe Cianflone

@timw4mail I didn't want to have the urls just redirect because that is a duplication of content. Search engines would ding you for having 2 pages with the exact same content and you would probably have issues with analytics too.

@wildlyinaccurate I could make this configurable my thinking for a 404 was, I guess, regarding a new website. If someone just went to the wrong link, you wouldn't redirect them to the right page and if no one knows your using CI they wouldn't think of typing in underscores. But for the purposes of someone updating their site, I could make this configurable.

Didn't notice the Welcome controller until I already submitted it. I'm still pretty new to Git/GitHub, is there any way to remove that page from the pull request?

Timothy Warren

@JoeClanflone No, a redirect is not content. A 301 or permanent redirect generally helps more than hurts.

Joe Cianflone

@timw4mail, ok. I'll make that a config option. I'll go with 404 or 301 as the options

Joseph Wynn

@JoeCianflone it's been a while since I've had to do this so I could be wrong - make sure your changes are backed up somewhere just in case! Doing a git reset --hard <SHA1-commit-id> should essentially delete the commit. Then you can re-apply your changes (if necessary) and commit only the files relevant to this pull request.

The commit ID in this instance is b0544fa

Joe Cianflone

@wildlyinaccurate that worked....I did have to recopy my changes in and do a -f on the push, but it worked. I've updated the docs and now you can toggle between 404 and 301 redirects with a config variable.

Joe Cianflone

Anyone have any more feedback?

Andrey Andreev
Collaborator

This could be achieved with a simple str_replace() when setting the class and method to be executed. The question is - do we want it?

Timothy Warren

@narfbg One of the things I've noticed lately is how much of a pain it is to enter in underscores on mobile devices. Dashes are much easier.

Andrey Andreev
Collaborator

Done.

Andrey Andreev narfbg closed this November 01, 2012
Michael Zimmer nonchip referenced this pull request from a commit in nonchip/CodeIgniter November 01, 2012
Andrey Andreev Allow use of dashes in controller/method URI segments
Supersedes PR #642
379438f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 3 unique commits by 1 author.

Nov 01, 2011
routes can now use dashes instead of underscores b0544fa
Nov 04, 2011
removed the welcome page from the commit and added in 301 redirects bf094b4
Nov 05, 2011
Merge branch 'develop' into feature-use-dashes 98784ad
This page is out of date. Refresh to see the latest.
43  application/config/config.php
@@ -86,6 +86,49 @@
86 86
 
87 87
 /*
88 88
 |--------------------------------------------------------------------------
  89
+| Use Dashes
  90
+|--------------------------------------------------------------------------
  91
+|
  92
+| This option allows you to use dashes instead of underscores in the url
  93
+| so your URLs would look like this:
  94
+|
  95
+| http://test.com/foo-controller/bar-function
  96
+|
  97
+| instead of this:
  98
+|
  99
+| http://test.com/foo_controller/bar_function
  100
+|
  101
+| variables are unaffected so you're still able to do something like this
  102
+| if it's necessary:
  103
+|
  104
+| http://test.com/foo-controller/bar-function/baz_var123
  105
+|
  106
+| This will really only look at folders, controllers and functions
  107
+| 
  108
+| NOTE: PHP does not support dashes as part of a function names, so still 
  109
+| use underscores in your function name.  
  110
+|
  111
+| http://codeigniter.com/user_guide/general/urls.html
  112
+*/
  113
+$config['use_dashes'] = TRUE;
  114
+
  115
+
  116
+/*
  117
+|--------------------------------------------------------------------------
  118
+| Use Dashes Redirect
  119
+|--------------------------------------------------------------------------
  120
+|
  121
+| Tells the system how to handle urls that have underscores in them if
  122
+| you've turned use_dashes to true
  123
+|
  124
+| 301 - this will do a 301 redirect from the underscored url to the dashed one
  125
+| 404 - will just deliver a 404 page not found error
  126
+|
  127
+*/
  128
+$config['use_dashes_redirect'] = '301';
  129
+
  130
+/*
  131
+|--------------------------------------------------------------------------
89 132
 | Default Language
90 133
 |--------------------------------------------------------------------------
91 134
 |
1  application/controllers/welcome.php
@@ -46,6 +46,7 @@ public function index()
46 46
 	{
47 47
 		$this->load->view('welcome_message');
48 48
 	}
  49
+	
49 50
 }
50 51
 
51 52
 /* End of file welcome.php */
97  system/core/Router.php
@@ -223,6 +223,50 @@ function _set_default_controller()
223 223
 
224 224
 	// --------------------------------------------------------------------
225 225
 
  226
+
  227
+   /**
  228
+    * Count the number of folders before the controller
  229
+    * 
  230
+    * This function will check if there are any folders
  231
+    * before the controller and correctly sets
  232
+    * the number of segments we need to check are using
  233
+    * dashes
  234
+    * 
  235
+    * @access private
  236
+    * @param array
  237
+    * @return int 
  238
+    */
  239
+   function _get_folder_count($segments = array())
  240
+   {
  241
+   
  242
+      $segment_count = count($segments);
  243
+      
  244
+      // if it's less than 3 segments we 
  245
+      // will naturally just look at all
  246
+      // of them so we can just kick out
  247
+      
  248
+      $folder_count = 0;
  249
+      $dir_check = APPPATH . '/controllers';
  250
+      
  251
+      for ($i = 0; $i < $segment_count; $i++)
  252
+      {
  253
+
  254
+         $dir_check .= '/'.$segments[$i];
  255
+
  256
+         if (is_dir($dir_check) === TRUE) 
  257
+         {
  258
+            $folder_count++;
  259
+         }
  260
+      
  261
+      }
  262
+      
  263
+      return $folder_count;
  264
+
  265
+   }
  266
+
  267
+   
  268
+   // --------------------------------------------------------------------
  269
+   
226 270
 	/**
227 271
 	 * Set the Route
228 272
 	 *
@@ -236,12 +280,61 @@ function _set_default_controller()
236 280
 	 */
237 281
 	function _set_request($segments = array())
238 282
 	{
239  
-		$segments = $this->_validate_request($segments);
240  
-
  283
+		// Check if you actually have any segments first
  284
+		// and if you don't just go straight to the 
  285
+		// default controller
241 286
 		if (count($segments) == 0)
242 287
 		{
243 288
 			return $this->_set_default_controller();
  289
+		}			
  290
+
  291
+		// Lets check and see if you're using dashes instead of underscores
  292
+		if ($this->config->item('use_dashes') === TRUE)
  293
+		{		   
  294
+         $segment_temp = $segments;
  295
+         $folder_count = $this->_get_folder_count($segment_temp); //1
  296
+         		   
  297
+		   for ($i = 0; $i < $folder_count; $i++)
  298
+		   {
  299
+		      unset($segment_temp[$i]);
  300
+		   }
  301
+		   		   
  302
+         $loop_length = (count($segment_temp) > 2 ? 2 : count($segment_temp)) + $folder_count;
  303
+         
  304
+         
  305
+         $trigger_redirect = FALSE;
  306
+         $full_uri = $_SERVER["REQUEST_URI"];
  307
+         
  308
+         for ($j = $folder_count; $j < $loop_length; $j++)
  309
+         {
  310
+            
  311
+            if (strpos($segment_temp[$j], '_') !== FALSE)
  312
+            {
  313
+               if ($this->config->item('use_dashes_redirect') == '404')
  314
+               {
  315
+                  show_404($segments);
  316
+               }
  317
+               else if ($this->config->item('use_dashes_redirect') == '301')
  318
+               {
  319
+                  $trigger_redirect = TRUE;
  320
+
  321
+                  $replace = str_replace('_', '-', $segment_temp[$j]);
  322
+
  323
+                  $full_uri = str_replace($segment_temp[$j], $replace, $full_uri);
  324
+               }               
  325
+            }
  326
+         
  327
+            $segments[$j] = str_replace('-', '_', $segments[$j]);	
  328
+         }
  329
+         
  330
+         if ($trigger_redirect === TRUE) 
  331
+         {
  332
+            header("Location: $full_uri", TRUE, 301);
  333
+         }
  334
+
244 335
 		}
  336
+				
  337
+		$segments = $this->_validate_request($segments);
245 338
 
246 339
 		$this->set_class($segments[0]);
247 340
 
44  user_guide_src/source/general/urls.rst
Source Rendered
@@ -32,6 +32,50 @@ The :doc:`URI Class <../libraries/uri>` and the :doc:`URL Helper <../helpers/url
32 32
 easy to work with your URI data. In addition, your URLs can be remapped
33 33
 using the :doc:`URI Routing <routing>` feature for more flexibility.
34 34
 
  35
+Using dashes instead of underscores
  36
+===================================
  37
+
  38
+The CodeIgniter coding convention for Controller and function names is to use
  39
+underscores in between words::
  40
+
  41
+   class Foo_Bar_Controller {
  42
+      
  43
+      public function baz_function() { ... }
  44
+   }
  45
+   
  46
+
  47
+This translates into your urls as such::
  48
+
  49
+   http://example.com/foo_bar_controller/baz_function
  50
+
  51
+
  52
+A lot of developers like to use dashes as separators between words in 
  53
+their URLs.  If you like dashes better than underscores you can set the
  54
+config variable use_dashes to TRUE::
  55
+
  56
+   $config['use_dashes'] = TRUE;
  57
+   
  58
+
  59
+Now, controllers and functions that have underscores will be mapped to 
  60
+URLs using dashes.  So the above example would now be::
  61
+
  62
+   http://example.com/foo-bar-controller/baz-function
  63
+   
  64
+
  65
+Also, there are also two different ways to control what happens if someone
  66
+types the wrong URL::
  67
+
  68
+   $config['use_dashes_redirect'] = '301|404';
  69
+
  70
+
  71
+301 will perform a 301 redirect from all URLs that have underscores to URLs with dashes.
  72
+
  73
+404 throw a 404 error and redirect the user to your 404 error page.
  74
+
  75
+.. note:: this only effects functions and controllers, neither folders nor 
  76
+   variables will be checked.
  77
+
  78
+
35 79
 Removing the index.php file
36 80
 ===========================
37 81
 
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.