Welcome Guest, Not a member yet? Register   Sign In
Multiple level controllers
#1

[eluser]fauria81[/eluser]
Hi!

Im developing an application using Codeigniter 1.7.1.

I have a backend system, and would like to do something like:

localhost/codeigniter/admin/news/modify/112

for modifying the post with id 112, or

localhost/codeigniter/admin/users/add

for adding a new user.

Any suggestions on which approach does best when dealing with things like that?

Thank you very much!
#2

[eluser]TheFuzzy0ne[/eluser]
If I understand you correctly, this is what I use:-

./system/application/libraries/MY_Router.php
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Router extends CI_Router {
    
    /**
     * Validates the supplied segments.  Attempts to determine the path to
     * the controller.
     *
     * @access    private
     * @param    array
     * @return    array
     */    
    function _validate_request($segments)
    {
        
        // Does the requested controller exist in the root directory?
        if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
        {
            return $segments;
        }

        // Maybe it's in a sub-directory?
        while (count($segments) > 0)
        {    
            if (file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
            {
                return $segments;
            }
            
            $this->set_directory($this->fetch_directory().$segments[0]);
            $segments = array_slice($segments, 1);
        }
        
        // We're out of options - is there a default controller in the current directory?
        if (file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
        {
            $this->set_class($this->default_controller);
            $this->set_method('index');
            
            return array();
        }
        
        // Can't find the requested controller...
        show_404(substr($this->fetch_directory(), 0, -1));
    }
}

It allows you to have controllers as many deep as you're like.

An example of the above structure might be:

Code:
codeigniter
  +-admin
      +-homepage.php
      +-users
          +-homepage.php
          +-add.php
      +-news
          +-homepage.php
          +-modify.php

Replace "homepage" with the name of your default controller. If you call on a URL such as:

localhost/codeigniter/admin/
localhost/codeigniter/admin/news
or
localhost/codeigniter/admin/users

The appropriate homepage.php will be called upon, so none of those horrible 404 errors. The only rule that applies, is that if you place a file with the same name as a subdirectory, that subdirectory will not be traversed, so it's a bit of common sense really.

For example.

Code:
codeigniter
  +-admin
      +-homepage.php
      +-users
          +-homepage.php
          +-add.php
      +-news
          +-homepage.php
          +-modify.php
          +-modify # This directory will never be hit
              +-homepage.php # nor will this
              +-another_controller.php # or this

Hope this helps.

EDIT: I found I have to use _remap in each of my controllers, because each of my controllers actually represents a controller method, and the directory name represents what would normally be the controller name, but it's only a few extra lines of code.
#3

[eluser]fauria81[/eluser]
Wow. Thank you very much for your detailed answer, this is exactly what i was looking for!




Theme © iAndrew 2016 - Forum software by © MyBB