Welcome Guest, Not a member yet? Register   Sign In
Creating an admin area
#3

[eluser]TheFuzzy0ne[/eluser]
[quote author="Mr Scientist" date="1233959884"]Hi all!
This is my first post to the CI forums.
[/quote]

Then let me be the first to welcome you to the forum.

[quote author="Mr Scientist" date="1233959884"]
I've just started learning CI after doing a bit of dabbling in PHP. I'm finding CI a fantastic library to use and the documentation second to none.
[/quote]

You've made a very good choice. CodeIgniter is the only PHP framework I've found where I could start using it immediately without spending weeks studying documentation. Every other framework I found was a real struggle to learn so I just gave up.

[quote author="Mr Scientist" date="1233959884"]
- In order to have the URL mysite.com/admin do I have to use a re-write rule.
[/quote]

I wouldn't have thought so. You should be able to create a directory named "admin" in your controllers directory, and setup some controllers inside it. The first one would be your default controller, which will most likely serve as the admin index page, or perhaps a login page.

[quote author="Mr Scientist" date="1233959884"]
- In order to have the functions that I would like to use only accessible behind the admin area do I have to nest these within the admin controller (an example would be greatly appreciated)?
[/quote]

I extended the controller with two methods.

./system/application/libraries/MY_Controller:
Code:
<?php

class MY_Controller extends Controller {
    
    function MY_Controller()
    {
        parent::Controller();

    }
    
    function _admin_restricted_area()
    {
        if (! $this->user->is_admin())
        {
            show_404();
        }
    }
    
    function _user_restricted_area()
    {
        if (! $this->user->is_logged_in())
        {
            redirect('/forums/member/login');
        }
    }
}

My controllers extend MY_Controller, and usually contain one of these lines in the constructor:

Code:
$this->_admin_restricted_area(); // Shows 404 error if the user is not an admin.

or

Code:
$this->_user_restricted_area(); // Sends the user to the login page if they're not logged in.

Example of a controller for an area that requires a user to be logged in:
Code:
<?php

class Homepage extends MY_Controller {

    function Homepage()
    {
        parent::Controller(); // Load the parent constructor.
        $this->_user_restricted_area(); // Check the user is logged in and act accordingly.
    }

    function index()
    {
        $this->load->view('index');
    }
}

Example of a controller for an area that requires an administrative privileges:
Code:
class Homepage extends MY_Controller {

    function Homepage()
    {
        parent::Controller(); // Load the parent constructor
        $this->_admin_restricted_area(); // Check the user is admin and act accordingly.
    }

    function index()
    {
        $this->load->view('index');
    }
}

I also have a user library which handles logins, and sets up the users cookies, but I won't post that; it's a bit too big, but hopefully you get the idea or at least a little inspiration.


Messages In This Thread
Creating an admin area - by El Forum - 02-06-2009, 10:38 AM
Creating an admin area - by El Forum - 02-12-2009, 04:12 PM
Creating an admin area - by El Forum - 02-12-2009, 04:57 PM
Creating an admin area - by El Forum - 02-12-2009, 06:58 PM
Creating an admin area - by El Forum - 02-13-2009, 12:27 PM
Creating an admin area - by El Forum - 02-23-2009, 03:14 AM
Creating an admin area - by El Forum - 02-23-2009, 05:25 AM
Creating an admin area - by El Forum - 02-24-2009, 02:58 AM
Creating an admin area - by El Forum - 02-24-2009, 06:04 AM
Creating an admin area - by El Forum - 02-25-2009, 07:08 AM
Creating an admin area - by El Forum - 02-25-2009, 07:23 AM
Creating an admin area - by El Forum - 02-26-2009, 08:31 PM
Creating an admin area - by El Forum - 02-27-2009, 07:21 AM
Creating an admin area - by El Forum - 04-09-2009, 08:37 AM
Creating an admin area - by El Forum - 04-09-2009, 09:49 AM
Creating an admin area - by El Forum - 04-09-2009, 09:54 AM



Theme © iAndrew 2016 - Forum software by © MyBB