Welcome Guest, Not a member yet? Register   Sign In
Controller in subfolders and with index pages on every subfolder
#1

[eluser]bartgrrr[/eluser]
Hi,

Lets say next controller directory structure:

controllers/sparen/juniorsparen/berekenen
controllers/sparen/juniorsparen/faq
controllers/sparen/spaarrekening/berekenen
controllers/sparen/spaarrekening/faq
controllers/pensioen/pensioensparen/berekenen
controllers/pensioen/pensioensparen/faq

where juniorsparen is my controller and berekenen and faq are my controller methods, so the corresponding urls are:
http://www.mysite.com/sparen/juniorsparen/berekenen
http://www.mysite.com/sparen/juniorsparen/faq

where spaarrekening is my controller and berekenen and faq are my controller methods, so the corresponding urls are:
http://www.mysite.com/sparen/spaarrekening/berekenen
http://www.mysite.com/sparen/spaarrekening/faq

where pensioensparen is my controller and berekenen and faq are my controller methods, so the corresponding urls are:
http://www.mysite.com/pensioen/pensioensparen/berekenen
http://www.mysite.com/pensioen/pensioensparen/faq


What I want is the following:

http://www.mysite.com/sparen
goes to an index page with 2 links to an index page of juniorsparen and spaarrekenig

http://www.mysite.com/sparen/juniorsparen
goes to an index page with 2 links to berekenen and faq (of juniorsparen)

http://www.mysite.com/sparen/spaarrekening
goes to an index page with 2 links to berekenen and faq (of spaarrekening)

http://www.mysite.com/oensioen
goes to an index page with a link to pensioensparen (of pensioen)

http://www.mysite.com/oensioen/pensioensparen
goes to an index page with 2 links to berekenen and faq (of pensioensparen)


The problem is in the MY_Router class, it only permits controllers at the lowest/highest level of my directory structure.

I don't know how to fix it!

The code of the MY_ROUTER class:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


/**
* Controller
*/
class MY_Router extends CI_Router
{

function __construct()
{
  parent::__construct();
  log_message('debug', "MY_Router Class Initialized");
}

/**
  * Validates the supplied segments.  Attempts to determine the path to
  * the controller.
  *
  * @access private
  * @param array
  * @return array
  */
function _validate_request($segments)
{
  if (count($segments) == 0)
  {
   return $segments;
  }

  // Does the requested controller exist in the root folder?
  if (file_exists(APPPATH.'controllers/'.$this->directory.$segments[0].'.php'))
  {
   log_message('debug', $this->directory.$segments[0].' is a controller.');
   return $segments;
  }

  // Is the controller in a sub-folder?
  if (is_dir(APPPATH.'controllers/'.$this->directory.$segments[0]))
  {
   log_message('debug', $this->directory.$segments[0].' is a sub-folder.');
   // Set the directory and remove it from the segment array
   $this->set_directory($segments[0],$this->directory);
   $segments = array_slice($segments, 1);

   return $this->_validate_request($segments);
  }


  // If we've gotten this far it means that the URI does not correlate to a valid
  // controller class.  We will now see if there is an override
  if ( ! empty($this->routes['404_override']))
  {
   $x = explode('/', $this->routes['404_override']);

   $this->set_class($x[0]);
   $this->set_method(isset($x[1]) ? $x[1] : 'index');

   return $x;
  }


  // Nothing else to do at this point but show a 404
  show_404($segments[0]);
}

/**
  *  Set the directory name
  *
  * @access public
  * @param string
  * @return void
  */
function set_directory($dir, $base='')
{
  $this->directory = $base.str_replace(array('/', '.'), '', $dir).'/';
}
}

/* End of file core/MY_Router.php */
#2

[eluser]Pert[/eluser]
You should be able to use <b>config/routes.php</b> file.

Maybe create a <b>controllers/landing.php</b> with method for each landing page and then route it with something like

Code:
$route['sparen'] = 'landing/sparen';
$route['sparent/juniousparen'] = 'landing/sparen_juniorsparen';
...

I have to say it might not work as I can't test it right now, but that would be the first thing I'd try if I had similar issue myself.
#3

[eluser]bartgrrr[/eluser]
Thanks Pert. It's works just fine with this kind of solution!!!!




Theme © iAndrew 2016 - Forum software by © MyBB