Welcome Guest, Not a member yet? Register   Sign In
SEO Friendly URLS, Using Hyphens not Underscores
#1

[eluser]mteejay[/eluser]
Hi,

I have just started out using the CI framework in the last couple of days and am considering using it as a basis to develop applications and websites for my clients.

I have no problem understanding how to use the framework itself as it is fairly intuitive.

I understand about the the frameworks ability to use use hyphens in the uri segments, I realise that these are disallowed characters in the url but there are workarounds that I have seen on this forum including http://ellislab.com/forums/viewthread/124396/#644012 (this actually has a couple of solutions from using a .htaccess file to using a MY_Routes.php file and hooks).

The problem is I just can't get it to work on my sample project. I am setting up a file in the /controllers/ with the naming convention this_is_my_file.php and naming the controller something like class this_is_my_file extends CI_CONTROLLER blah blah blah, but whenever I type in the url http://www.sampleproject.com/this-is-my-file it cannot find the controller.

I was under the impression that MY_Router.php took care of this for me? I will add some code in below for a sample page on say...banking_finance.php
Code:
<?php

  if (!defined('BASEPATH'))
      exit('No direct script access allowed');

  class banking_finance extends CI_Controller
      {

      // load the constructor to the file (PHP4 compatibility)
      public function __construct()
          {
          parent::__construct();
          }

      public function index()
          {
          $this->load->view('view_banking_finance');
          }

I feel as though I am missing something obvious, the custom MY_Router.php should be extended automatically as it is (I got it from these forums):

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* This class override enhances routing.
*
* @author    Joshua Logsdon
* @email    [email protected]
* @filename    MY_Router.php
* @title    Router library override
* @url        http://www.joshualogsdon.com
* @version    1.0
*/
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())
    {
    
        //BEGIN - Joshua Logsdon
        for ( $i = 0; $i < 2; $i++ )
        {
            if ( isset($segments[ $i ]) && strpos($segments[ $i ], '-') !== FALSE )
            {
                $segments[ $i ] = str_replace('-', '_', $segments[ $i ]);
                
            }
        }
        //END - Joshua Logsdon
        
        $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
                $this->set_method($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;
        
    }
    
    

}
// END Router Class

/* End of file Router.php */
/* Location: ./application/libraries/MY_Router.php */

Please help a newbie!
#2

[eluser]mteejay[/eluser]
using ci 2.0.2 btw
#3

[eluser]mteejay[/eluser]
bump bump bump
#4

[eluser]Akinzekeel[/eluser]
Take a look at URI Routing in the user guide.

Here's an example:
Code:
$route["banking-finance"] = "banking_finance";
#5

[eluser]mteejay[/eluser]
Thank you for your help, exactly what I was after.




Theme © iAndrew 2016 - Forum software by © MyBB