CodeIgniter Forums
Modular Separation Mixed with main controller folder having sub folders - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Modular Separation Mixed with main controller folder having sub folders (/showthread.php?tid=56851)

Pages: 1 2


Modular Separation Mixed with main controller folder having sub folders - El Forum - 01-25-2013

[eluser]xtremer360[/eluser]
I don't know where to go to from here. As of right now I have the following file system. I also have included below a MY_Router.php file code. My question is asking if anyone thinks I should be handling the controllers differently because of the two MY_Router files I have. I don't have any modules yet so I just created some dummy modules for this post.

Code:
/application
    /controllers
        /usermanagement
            login.php
            register.php
        /cpanel
            dashboard.php
    /core
        MY_Controller.php (my own controller)
        MY_Loader.php (file from wiredesignz Modular Separation folder)
        MY_Router.php (file from wiredesignz Modular Separation folder)
        MY_Router.php (Code I grabbed from another topic post that is included below)
    /modules
        /blog
            /controllers
                blog.php
            /models
                blog_model.php
            /views
                blog_view.php
        /site
            /controllers
                site.php
            /models
                site_model.php
            /views
                site_view.php


This was my original MY_Router.php file that I still have loaded that was used so I could organize a few controllers into sub folders.
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* MY_Router Class
*
* @author        Damien K.
*/
class MY_Router extends CI_Router
{
    // --------------------------------------------------------------------

    /**
     * OVERRIDE
     *
     * 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/'.$segments[0].EXT))
        {
            return $segments;
        }

        // Is the controller in a sub-folder?
        if (is_dir(APPPATH.'controllers/'.$segments[0]))
        {
            // @edit: Support multi-level sub-folders
            $dir = '';
            do
            {
                if (strlen($dir) > 0)
                {
                    $dir .= '/';
                }
                $dir .= $segments[0];
                $segments = array_slice($segments, 1);
            } while (count($segments) > 0 && is_dir(APPPATH.'controllers/'.$dir.'/'.$segments[0]));
            // Set the directory and remove it from the segment array
            $this->set_directory($dir);
            // @edit: END

            // @edit: If no controller found, use 'default_controller' as defined in 'config/routes.php'
            if (count($segments) > 0 && ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
            {
                array_unshift($segments, $this->default_controller);
            }
            // @edit: END

            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]);
                    // @edit: Fix a "bug" where show_404 is called before all the core classes are loaded
                    $this->directory = '';
                    // @edit: END
                }
            }
            else
            {
                // Is the method being specified in the route?
                if (strpos($this->default_controller, '/') !== FALSE)
                {
                    $x = explode('/', $this->default_controller);

                    $this->set_class($x[0]);
                    $this->set_method($x[1]);
                }
                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 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]);
    }

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

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

}



Modular Separation Mixed with main controller folder having sub folders - El Forum - 01-25-2013

[eluser]wiredesignz[/eluser]
[quote author="xtremer360" date="1359103247"]... two MY_Router files I have...

Code:
/application
/core
        MY_Router.php (file from wiredesignz Modular Separation folder)
        MY_Router.php (Code I grabbed from another topic post that is included below)
[/quote]

WTF? You cannot have two files with the same file name in the same directory.


Modular Separation Mixed with main controller folder having sub folders - El Forum - 01-25-2013

[eluser]xtremer360[/eluser]
Okay well anymore anything else because that doesn't help me any.


Modular Separation Mixed with main controller folder having sub folders - El Forum - 01-25-2013

[eluser]Aken[/eluser]
Putting a controller file into a single-level subfolder is allowed by default in 2.1.3 and at least a couple previous versions. So if your router is allowing only that functionality, then it is completely unnecessary.

Beyond that, I have never used HMVC, so I have no idea what would conflict or how it works by default.


Modular Separation Mixed with main controller folder having sub folders - El Forum - 01-25-2013

[eluser]xtremer360[/eluser]
Still looking for more help on the subject.


Modular Separation Mixed with main controller folder having sub folders - El Forum - 01-25-2013

[eluser]xtremer360[/eluser]
Any additional ideas?


Modular Separation Mixed with main controller folder having sub folders - El Forum - 01-25-2013

[eluser]Aken[/eluser]
Honestly, man, you have SO much going on here, it's almost impossible for us to be able to narrow anything down with everything you've added and changed to CI. You'd probably be better off hiring someone to take a look directly.


Modular Separation Mixed with main controller folder having sub folders - El Forum - 01-25-2013

[eluser]xtremer360[/eluser]
I was hoping anyone had an idea.


Modular Separation Mixed with main controller folder having sub folders - El Forum - 01-25-2013

[eluser]Aken[/eluser]
Actually, looking back, you haven't even said what problem you're having, if any. You just said "if anyone has any ideas". That's even more difficult to figure out what you want.

You need to get rid of one of the MY_Router files, though. That just isn't going to work, and no one can help you do anything if that stays like that. So figure that out, and then come back and explain in better detail what you need help with.


Modular Separation Mixed with main controller folder having sub folders - El Forum - 01-25-2013

[eluser]xtremer360[/eluser]
Well the setup of the files is what I'm mainly focused on. Because I will be working with modules but I also need to take into account login and just the dashboard by itself and registration. Do I put those separately into the applications/controllers folder or make modules out of those or something. After looking around I haven't sen anyone post anything about their setups when its used.