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

[eluser]Jonas G[/eluser]
I use a single admin controller that holds all the functions I need. I then use switch statements for further control:

Code:
class Admin extends Controller
{

    function __construct()
    {
        parent::Controller();
        if (!$this->jonas_auth->valid_admin()) {
            redirect('index');
        }
    }

    function blog($action=false, $id=false)
    {
        $this->load->model('Blog_model');
        
        $blog_table = $this->config->item('blog_table');
        
        $this->load->view('common/header');
        switch ($action) {
            case 'add':

                break;
        
            case 'edit':

                break;

            case 'publish':

                break;

            case 'unpublish':

                break;

            case 'edit_comment':

                break;

            case 'delete_comment':

                break;
                
            case 'delete':

                
                break;
            
            default:

                break;
        }
        
        $this->load->view('common/footer');
    }

Note: I have removed the code from the cases so it wouldn't take up space.
#12

[eluser]TheFuzzy0ne[/eluser]
Nice idea. That would never have occurred to me. If the function gets too big, I guess you could always export the code into a private controller function, prefixed with an underscore. Thanks for sharing.
#13

[eluser]JulianM[/eluser]
Hi

I'm not sure if I understood well your problem, but I recommend you to look at ezauth library.

For an admin tool, I recommend you the following:

- Create a custom MY_Controller that writes the _remap function for ezauth
- In your Admin controller, protect with ezauth which pages require authorization
- Instead of using a switch for separate CRUD operations, I'd recommend to separate in different operations, just like almost all examples of MVC works.

You will have something like these functions:

add()
edit($id)
save()

Also, you will need an edit view page that will be shared by add and edit functions.

When add or edit functions are called in your controller, then the "edit" view is displayed (and populated from database only for edit case). When the form is submitted, it calls to save function and you do the validation tasks and proper model calls for save into database or whatever.


[quote author="TheFuzzy0ne" date="1235723497"]Nice idea. That would never have occurred to me. If the function gets too big, I guess you could always export the code into a private controller function, prefixed with an underscore. Thanks for sharing.[/quote]
#14

[eluser]M4rc0[/eluser]
Hello!

Sorry for bumping this but one thing got to my mind:

TheFuzzy0ne posted this interesting code:

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');
        }
    }
}

May I ask, in your opinion, why extending a controller and not creating a helper with this functions?
Is there any advantages?

Just wondering if I took the right approach.
I'm using a helper with a method "is_logged_in", because then I can use that in my views to display different menus and etc

I would like to hear any opinion

Smile
#15

[eluser]TheFuzzy0ne[/eluser]
[quote author="M4rc0" date="1239305832"]May I ask, in your opinion, why extending a controller and not creating a helper with this functions?
Is there any advantages?[/quote]

It depends how you look at it. I just needed something quick and simple, and I needed the validation procedures called upon before the controller method itself was called, so it just made sense to me to extend the controller and add the functionality to the constructor. Another perk is that it's one less file to load.

[quote author="M4rc0" date="1239305832"]Just wondering if I took the right approach.
I'm using a helper with a method "is_logged_in", because then I can use that in my views to display different menus and etc[/quote]

There's no such thing as "the right approach", and there's never only one good way to do things, it usually comes down to personal style and taste. If it's working for you, it's simple to maintain and you can't see any faults with it, then do what you know. I like to go under the assumption that there's always room for improvement.

[url="http://ellislab.com/forums/viewthread/109123/#550882"]JayTee[/url] said it best:

[quote author="JayTee" date="1237581730"]
Always code as if the person who ends up maintaining your code is a violent psychopath who knows where you live.[/quote]
#16

[eluser]M4rc0[/eluser]
Alright. Thanks TheFuzzy0ne !

Big Grin




Theme © iAndrew 2016 - Forum software by © MyBB