Welcome Guest, Not a member yet? Register   Sign In
Using MY_Router.php to allow for hyphenated URL's
#1

[eluser]beneck[/eluser]
I am trying to find a way to make more SEO friendly URL's with CI.

I come from the train of thought that Hyphens are easier delimiters in a URL than underscores are.

Preferred:
http://mysite.com/controller/method-name
OR
http://mysite.com/controller-name/method-name

Default CI:
http://mysite.com/controller/method_name
OR
http://myseite.com/controller_name/method_name

Obviously CI is simply mapping the method name, and in PHP you can't have hyphens in method/function names. I came across a thread that suggested I extend CI_Router with MY_Router.

The idea is to to a string replace in the first two segments only and change out hyphens for underscores (this way, the rest of the URL is left alone and maps as planned).

I have created the following class and placed in the core directory.

However, the class is not being instantiated by itself. I was able to use the autoload feature to load it, however the routes aren't being passed through it. I verified in the config.php file that "MY_" is the prefix for custom files.

Is it in the right directory (core)?
Do I need to autoload it?
How do I make sure it's used?


Here is the code in my MY_Router.php file.

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

class MY_Router extends CI_Router {

function __construct()
{
  parent::__construct();
}

function _set_request($segments)
{
  //Fix only the first 2 segments
  for ($i=0; $i < 2; $i++)
  {
   if(isset($segments[$i]))
   {
    $segments[$i] = str_replace('-', '_', $segments[$i]);
   }
  }
  // run the original _set_request method, passing it our updated segments
  parent::_set_request($segments);
}
}
/* end of file: My_Router.php */
#2

[eluser]CroNiX[/eluser]
Well, only things extended from /system/core would go in /application/core. That library is in /system/libraries, so your extension would go in /application/libraries.
#3

[eluser]beneck[/eluser]
In my install... Router.php is located in system/core/Router.php.

There is no Router.php file in the system/library/ folder.
#4

[eluser]InsiteFX[/eluser]
It's very simple:

If your extending from system/core it go's in application/core.
If your extending from system/libraries it go's in application/libraries.
#5

[eluser]beneck[/eluser]
So based on what you are saying, my file IS in the correct folder. So why is it not instantiated and used by the system? Is there something else I need to do?
#6

[eluser]InsiteFX[/eluser]
You can try adding this and see if it works to your config.php file at the bottom!

Code:
/*
| -------------------------------------------------------------------------
| Native spl_autoload_register() - by Kenneth Vogt
| -------------------------------------------------------------------------
|
| Here is an updated version of Phil Sturgeon’s code:
|
| Thanks to Phil Sturgeon and Kenneth Vogt.
|
| NOTE:
| Requires PHP 5.3.+
| -------------------------------------------------------------------------
| MODIFIED by InsiteFX
| As of CI 3.0 Dev - The constant EXT has been removed modified
| to use '.php' now instead of EXT.
| should work for all version of CI and PHP 5.3
| -------------------------------------------------------------------------
| Place at the bottom of your ./application/config/config.php file.
| -------------------------------------------------------------------------
*/
spl_autoload_register(function($class)
{
if (strpos($class, 'CI_') !== 0)
{
  if (file_exists($file = APPPATH . 'core/' . $class . '.php'))
  {
   include $file;
  }
  elseif (file_exists($file = APPPATH . 'libraries/' . $class . '.php'))
  {
   include $file;
  }
}
});




Theme © iAndrew 2016 - Forum software by © MyBB