Welcome Guest, Not a member yet? Register   Sign In
Limit number of functions in class and URI
#1

[eluser]DavidHopkins[/eluser]
Hello all.

I am trying to make a new class that displays dynamic content depending on the end part of a url.

The url i want to use is - http://apply.Mydomain.co.uk/index.php/home/woszJ27

the woszJ27 is the unique part that can change.

The problem is my home.php class file thinks that woszJ27 is a function and so trys to load it and i get a 404.

Is there a way to change a class so that it knows the second part of the URI is a variable rather than another function ?

Many Thanks

David
#2

[eluser]CroNiX[/eluser]
Yes, use a route or the _remap() function. By default, CI expects the first segment to be the controller, the 2nd segment to be the method and any other parameters go after. If you want to change the default behavior, you need to use one of the methods I listed.

Code:
$route['home/(:any)'] = 'home/the_method_to_process/$1';  
$route['home'] = 'home';

in controller home.php:
Code:
class Home extends CI_Controller {
  public function __construct()
  {
    parent::__construct();
  }
  
  public function index()
  {
    //for if you go to just "home"
  }

  public function the_method_to_process($parameter = '')
  {
    //the 2nd parameter will be passed automatically from the route
  }
}
#3

[eluser]InsiteFX[/eluser]
_remap way:
Code:
// ./application/config/routes.php

$route['(.*)'] = 'home/index/$1';  // must be the last route defined!

// Home Controller index method

// --------------------------------------------------------------------

/**
* index()
*
* index - default method called.
*
* @access public
* @return void
*/
public function index($param = NULL)
{
    if ( ! empty($param))
    {
        // your code here for passed in parameters!
    }

    // your normal code here!
}

// Home Controller _remap method

// --------------------------------------------------------------------

/**
* _remap()
*
* Remaps the URL segments.
*
* @access public
* @param string
* @param array
* @return mixed
*/
public function _remap($method, $params = array())
{
    if (method_exists($this, $method))
    {
        return call_user_func_array(array($this, $method), $params);
    }

    show_404();
}




Theme © iAndrew 2016 - Forum software by © MyBB