Welcome Guest, Not a member yet? Register   Sign In
Arrange Controllers in Sub Folders
#1

[eluser]Unknown[/eluser]
Hi,

I know CI allows us to arrange our controllers in sub folders. That is nice. Is it possible to arrange the controllers in Sub-Sub-Folders and deeper? If yes, how? If no, what is the alternative?

Extra Info:
I want to have my folder structure as follows:
controllers/shop/products.php
controllers/shop/customers.php
Controllers/shop/admin/login.php
Controllers/shop/admin/products.php
and
controllers/jobs/vacancies.php
controllers/jobs/seekers.php
Controllers/jobs/admin/login.php
Controllers/jobs/admin/applications.php
and More folders and then more sub folders...
#2

[eluser]InsiteFX[/eluser]
CodeIgniter only allows to go one folder deep!

If you need more you can look into modules using the WIKI HMVC.

InsiteFX
#3

[eluser]reidz[/eluser]
im overiding Router library in CI with this code, used this code as your own library then solved
sorry i forgot from where i got this code, so credit goes to original coder
Code:
<?php

/*
* Custom router function v 0.1
*
* Add functionality : read into more than one sub-folder
*
*/

Class MY_Router extends CI_Router
{
    Function MY_Router()
    {
        parent::CI_Router();
    }

    function _validate_request($segments)
    {
        if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
        {
            return $segments;
        }

        if (is_dir(APPPATH.'controllers/'.$segments[0]))
        {
            $this->set_directory($segments[0]);
            $segments = array_slice($segments, 1);

            /* ----------- ADDED CODE ------------ */

            while(count($segments) > 0 && is_dir(APPPATH.'controllers/'.$this->directory.$segments[0]))
            {
                // Set the directory and remove it from the segment array
            $this->set_directory($this->directory . $segments[0]);
            $segments = array_slice($segments, 1);
            }

            /* ----------- END ------------ */

            if (count($segments) > 0)
            {
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
                {
                    show_404($this->fetch_directory().$segments[0]);
                }
            }
            else
            {
                $this->set_class($this->default_controller);
                $this->set_method('index');

                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
                {
                    $this->directory = '';
                    return array();
                }
            }
            return $segments;
        }

        show_404($segments[0]);
    }
}

?>

hope it works for you
#4

[eluser]InsiteFX[/eluser]
Personally, I would use HMVC keeps your code nice and neat!

InsiteFX
#5

[eluser]Unknown[/eluser]
Thanks @reidz

It really works. Here is how to use the code:
Created a file named MY_Router.php in the application/libraries/ and paste the code into it. That is it!

[quote author="reidz" date="1290799391"]im overiding Router library in CI with this code, used this code as your own library then solved
sorry i forgot from where i got this code, so credit goes to original coder
Code:
<?php

/*
* Custom router function v 0.1
*
* Add functionality : read into more than one sub-folder
*
*/

Class MY_Router extends CI_Router
{
    Function MY_Router()
    {
        parent::CI_Router();
    }

    function _validate_request($segments)
    {
        if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
        {
            return $segments;
        }

        if (is_dir(APPPATH.'controllers/'.$segments[0]))
        {
            $this->set_directory($segments[0]);
            $segments = array_slice($segments, 1);

            /* ----------- ADDED CODE ------------ */

            while(count($segments) > 0 && is_dir(APPPATH.'controllers/'.$this->directory.$segments[0]))
            {
                // Set the directory and remove it from the segment array
            $this->set_directory($this->directory . $segments[0]);
            $segments = array_slice($segments, 1);
            }

            /* ----------- END ------------ */

            if (count($segments) > 0)
            {
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
                {
                    show_404($this->fetch_directory().$segments[0]);
                }
            }
            else
            {
                $this->set_class($this->default_controller);
                $this->set_method('index');

                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
                {
                    $this->directory = '';
                    return array();
                }
            }
            return $segments;
        }

        show_404($segments[0]);
    }
}

?>

hope it works for you
[/quote]




Theme © iAndrew 2016 - Forum software by © MyBB