Welcome Guest, Not a member yet? Register   Sign In
General question regarding MY_Controller
#1

[eluser]überfuzz[/eluser]
I'm building a site with a login situated in the site header. At the moment I'm having a my controller looking for submit(login). If that submit is fired action is taken. My question is if there is a nice way of doing this. My my-controller feels more and more bloated as I go along building the login system.

Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

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

        // -------- head meta etc. -----------
        $this->load->model('systemdata');
        $this->data['link_tags'] = $this->systemdata->get_link_tags();
        $this->data['page_name'] = $this->systemdata->get_title();
        //strings to <title>
        $url_segments = $this->uri->segment_array();
        if($url_segments) { $this->data['page_title'] = ucfirst(implode( " - "  , array_reverse($url_segments)). " - " ); }
        else { $this->data['page_title'] = ''; }
        $this->data['meta'] = $this->systemdata->get_meta();
        $this->data['link_css'] = $this->systemdata->get_link_css();
        $this->data['link_ico'] = $this->systemdata->get_link_ico();
        $this->data['layout'] = $this->systemdata->get_site_images_filespec();
        $this->data['logo'] = $this->systemdata->get_logo();
        //------------- END head meta etc -------------------------------------

        //-------------- menu ------------------------------------------------
        $this->data['menu'] = $this->systemdata->get_menu();

        //-------------- every page ------------------------------
        $this->data['image_title'] = $this->systemdata->get_image_title();

        //the name of logged user
        if($this->session->userdata('logged_user'))
        {
            $user_array = $this->session->userdata('logged_user');
            $this->data['f_name'] = $user_array[0]['f_name'];
            $this->data['e_name'] = $user_array[0]['e_name'];
        }

        //Try to login
        if($this->input->post('login'))
        {
            $login = array($this->input->post('user'), $this->input->post('code'));
            $this->load->library('auth');
            if($this->auth->process_login($login))
            {
                //decide where they'll end up..?
                redirect('/admin/home');
            }
            else
            {
                redirect('login');
            }
        }

        //logout
        if($this->input->post('logout'))
        {
            $this->load->library('auth');
            if($this->auth->logout())
            {
                redirect('home');
            }
        }

        //use the restrict with access-level later on.
        if($this->uri->segment(1) == 'admin')
        {
            //creat restrict method in auth
            $temp_user = $this->session->userdata('logged_user');
            if(empty($temp_user))
            {
                redirect('home');  //is it possible to send to 403?
            }
        }
    }

}
#2

[eluser]Flemming[/eluser]
I must be missing something but why not have a login controller?

EDIT : sorry, looks like you have an admin controller that you redirect to if login fails. So why not just have your login form post directly to the login contoller?

I'm missing something, definitely ... aren't I!
#3

[eluser]eoinmcg[/eluser]
As Flemming says a login controller would be a much cleaner approach.

I'd also recommend refactoring the various activities in the constructor into smaller task based methods.

Cheers,
Eoin
#4

[eluser]überfuzz[/eluser]
I can't really say why I didn't build it around a login-controller. My bad! I'll do that, action=login etc. Thanx both of you for pointing that out.

[quote author="eoinmcg" date="1258044340"]I'd also recommend refactoring the various activities in the constructor into smaller task based methods.[/quote]

Seems sound... I'll be more organized in the login controller, I promise.
#5

[eluser]BrianDHall[/eluser]
I like using my_controller for login stuff, but what I'd recommend if you wanted to do it is use more functions.

So if 'login', execute $this->_login(), etc. Slims the constructor and makes it more logical, and allows $this->_logout() to be called directly from other scripts if desired (and I've occasionally run into wanting to do that).
#6

[eluser]überfuzz[/eluser]
[quote author="BrianDHall" date="1258060175"]I like using my_controller for login stuff, but what I'd recommend if you wanted to do it is use more functions.

So if 'login', execute $this->_login(), etc. Slims the constructor and makes it more logical, and allows $this->_logout() to be called directly from other scripts if desired (and I've occasionally run into wanting to do that).[/quote]Quite right, but Flemming has got a point. You could do the logout with a normal controller, just as easy as if it's situated in the my-controller. Only difference would be the url, but then again you could redirect from the logout method. Have the logout form pointing at the logout url.




Theme © iAndrew 2016 - Forum software by © MyBB