Welcome Guest, Not a member yet? Register   Sign In
controller nested folders
#21

[eluser]iqdavidh[/eluser]
Well, I have the same trouble... When I have the controller in a folder 2 levels down:

controllers/folder_level1/folder_lever2/


It doesn´t work!!! (a error 404 apears)

If I move the controller to the folder_level1 it works!!!!

Somebody has solves this?
#22

[eluser]simshaun[/eluser]
Your answer is right above my last post.
You can only have 1 subfolder!

If you want to solve that, you need to modify the core to look down in more than one folder.
#23

[eluser]Jagar[/eluser]
Actually with the help of http://ellislab.com/forums/viewthread/107451/#541043 you can have unlimited nested folders, view the code.

You can either use the posted code, or use mine, which I customized it to fit my needs, it may even help you.

Code:
<?php

/**
* Router Extension to allow controllers in unlimited nesting of folders.
* Discussion thread at: http://ellislab.com/forums/viewthread/56100/
* @author Peter Goodman
* @copyright Copyright 2007 Peter Goodman, all rights reserved.
*
* CMS capability by Adam Jackett www.darkhousemedia.com, 2009
*/
class MY_Router extends CI_Router {
    
    function _set_route_mapping() {
        parent::_set_route_mapping();
        
        // re-routed url
        if($this->rsegments != $this->segments) {
            array_unshift($this->rsegments, $this->directory);
        }
    }
    
    function _pluck_directory($segments) {
        $this->directory = '';
        
        foreach($segments as $segment) {
            $segment = trim($segment);
            if($segment != '') {
                if(is_dir(APPPATH .'controllers/'. $this->directory . $segment)) {
                    $this->directory .= array_shift($segments) .'/';
                } else {
                    break;
                }
            } else {
                array_shift($segments);
            }
        }
        
        // quick an easy forced reindexing
        $segments = array_values($segments);
        
        // put the entire directory path back into the segment as the first
        // item
        $dir = trim($this->directory, '/');
        if(!empty($dir)) {
            array_unshift($segments, $dir);
        }
        
        $this->segments = $segments;
        
        return $segments;
    }
    
    /*function _validate_request($segments) {
        return parent::_validate_request($this->_pluck_directory($segments));
    }*/
    
    function _validate_request($segments) {
        $segments = $this->_pluck_directory($segments);
        $found = TRUE;
        
        // Does the requested controller exist in the root folder?
        if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
        {
            return $segments;
        }
        
        // Is the controller in a sub-folder?
        if (is_dir(APPPATH.'controllers/'.$segments[0]))
        {        
            // Set the directory and remove it from the segment array
            $this->set_directory($segments[0]);
            $segments = array_slice($segments, 1);
            
            if (count($segments) > 0)
            {
                // Does the requested controller exist in the sub-folder?
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
                {
                    //show_404($this->fetch_directory().$segments[0]);
                    $found = FALSE;
                }
            }
            else
            {
                $this->set_class($this->default_controller);
                $this->set_method('index');
            
                // Does the default controller exist in the sub-folder?
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
                {
                    $this->directory = '';
                    return array();
                }
            
            }
            
            //return $segments;
            if($found){
                return $segments;
            }else {
                show_404($this->fetch_directory().$segments[0]);
            }
        }

        // Can't find the requested controller...
        //show_404($segments[0]);
        $this->set_directory('');
        $this->set_class('home'); #Whatever the default controller is
        $this->set_method('index');
        return array();
    }

}
#24

[eluser]iqdavidh[/eluser]
yea!!!!!!!

it works!!!!

thanks everybody
#25

[eluser]TheFuzzy0ne[/eluser]
The above post is spam. Can we delete it so we aren't indexing their Web site via Google.

Edit: The above post is not, I repeat not spam. Big Grin
#26

[eluser]Thorpe Obazee[/eluser]
It's been reported and it will be deleted as far as I know.
#27

[eluser]Dam1an[/eluser]
Fuzzy, you're post will look kind of stupid once the post is deleted, as you'll be refering to a valid post as spam lol (note to self, always quote a spammer when referencing them)
#28

[eluser]TheFuzzy0ne[/eluser]
I
I have problems navigating around code that's any more than a few pages in length, so I now try to export every controller method into it's own controller, and use _remap(). So my structure would look something like this.

Code:
controllers
  +-admin
      +-forums
      |   +-homepage.php
      |   +-delete.php
      |   +-create.php
      |   +-update.php
      |   +-show.php
      |
      +-users
      |   +-homepage.php
      |   +-delete.php
      |   +-create.php
      |   +-update.php
      |   +-show.php
      |
      +-homepage.php
      +-another_controller.php
The standard CodeIgniter Router does not go more than one directory deep, so I needed an alternative.

Here's my take on the subject.

./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();
        }
        
        # I was thinking of also imeplementing something like this:
        
        #// Is there a file named "index" in the current directory?
        #if (file_exists(APPPATH.'controllers/'.$this->fetch_directory()."index".EXT))
        #{
        #    $this->set_class($this->default_controller);
        #    $this->set_method('index');
        #    
        #    return array();
        #}
        
        # ... and/or this:
        
        #// Is there a file with the same name as the current directory
        #if (file_exists(APPPATH.'controllers/'.$this->fetch_directory().basename($this->fetch_directory()).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));
    }
}

The last two if statements was something I was thinking of implementing, so that apps can be more portable, and not have to know what the name of the default controller is.

However, if you create a subdirectory with the same name as a PHP file that resides in the same directory, the directory will never be traversed. Blatantly obvious, but hey...

And also, it's recommended that each controller directory contains a default controller. Again, this is more to do with common sense than anything else.

Feel free to give this library a bash. It's been designed to be fully compatible with your current structure, so long as your structure works with CodeIgniter already.

Comments and questions welcome.

@Dam1an: I've edited my post above to reflect the current situation accordingly. Smile
#29

[eluser]wiredesignz[/eluser]
@Fuzzy, You should not use "View" as a controller class name, sorry about that. (see Reserved Words)

It sounds like you should be using Modular Extensions or Matchbox to manage your code.

Note: You do not need to use the HMVC components in Modular Extensions but it does allow you better organization in large applications.
#30

[eluser]Dam1an[/eluser]
I think the 'not calling a controller function view' thing only applies to PHP4




Theme © iAndrew 2016 - Forum software by © MyBB