Welcome Guest, Not a member yet? Register   Sign In
Controller Subfolder Structure - Prevent Root Access
#1

[eluser]haydenp[/eluser]
Hiya, possibly a silly question

I'm using a controller subfolder structure as follows:

controllers/admin/login.php

I have the default welcome.php controller sitting in the root of the controllers folder as follows:

controllers/welcome.php

I have the welcome.php controller set as my default_controller in the routes file.

The problem I have is ... if I access: http:/www.mywebsite.com/index.php/admin/ I get the welcome controller displaying the welcome page.

- Why is this ... I would have though a 404 would display as no controller is being specified?
- How can I prevent this and rather show a 404?

Many thanks
#2

[eluser]Edmundas KondraĊĦovas[/eluser]
If you have a controller in a subfolder, I think you need to specify http:/www.mywebsite.com/index.php/admin/login rather than just http:/www.mywebsite.com/index.php/admin.

And if you want to display an error message when Code Igniter loads default controller, make something like this:

Code:
class welcome_controller extends Controller
{
    function welcome_controller()
    {
        parent::Controller();
    }

    function index()
    {
        // Show the error
    }

}

I'm not completely certain that this is what you want, sorry If I'm wrong. Wink
#3

[eluser]mdcode[/eluser]
I think that the easiest way to do this is to just copy the standard index.html file that is in all of you other standard CI directories to each of your new sub-directories. This would negate the need to create another Controller page, and would deny direct directory access.

In addition, don't forget to add in this line at the very top of all of your created files:
Code:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
#4

[eluser]xwero[/eluser]
insert following before all other code in the index method of the default controller
Code:
if(is_dir(pathinfo(__FILE__,PATHINFO_DIRNAME).$this->uri->uri_string()))
{
    show_404();
}
I didn't use the FCPATH constant because it's going to change in the following version of CI.
#5

[eluser]haydenp[/eluser]
Thanks for the feedback all

xwero, your method did exactly what I needed, thanks. I'm new to CI, would you mind giving a brief breakdown of what the code you provided is doing?

I'm still trying to get my head around why my default controller was being called when trying to access the root of a controllers subfloder

http:/www.mywebsite.com/index.php/admin/login/ ... this works perfectly as expected

BUT

http:/www.mywebsite.com/index.php/admin/ ... was executing my default controller when I WOULD HAVE expected a 404 to display (which is what I want)

Many thanks!
#6

[eluser]xwero[/eluser]
The router separates the directory, class and method. If the url only matches a directory it sets the default controller and method as the one that needs to displayed.

My code checks the directory based on the path of index.php, FCPATH, and the segments fetched by the uri class.
#7

[eluser]haydenp[/eluser]
Thanks xwero, with you now!




Theme © iAndrew 2016 - Forum software by © MyBB