Welcome Guest, Not a member yet? Register   Sign In
Login class available in different classes
#1

[eluser]mvdg27[/eluser]
Hi,

I'm new to CodeIgniter, but so far I really like what I see. I'm now working on my first "test application" and run into a little problem.

I started of with expanding the welcome controller, adding some functions to it and displaying several items in the view. Now I want to add a little login form to the page. In itself this is not difficult and I got that working.

However I'd like to create another class (e.g. Blog) now, and still have the login functionality available. Again adding the form to the view is not the problem (using a template for it). But it doesn't seem to make sense to reproduce the login code in the Blog class.

Actually I'd like to create a different class, especially for the login part. My question now is, where to place that class, while still conforming to CodeIgniters standards and how to make that class available in both Welcome and Blog?

Thanks in advance!
-Cheers, Michiel
#2

[eluser]adamp1[/eluser]
What you want is to create a basic auth library. Now a library is a collection of functions which perform actions. For example log a user in.

You then need a controller to show the login form to the user.

So a basic layout of the auth library would be as follows (Userlib.php):
Code:
class Userlib {
  function Userlib()
  {
    // Constructor function
  }

  // Checks a user is logged in
  function check()
  {
    // Check if a user is logged in

    // If not send them to the login form
    redirect('auth/login','location');
  }

  // Perform the login, i.e. check the users data is valid
  // If not redirect back to the login form
  function login()
  {
    // Perform login
  }
}

Then you would have an auth controller (auth.php):
Code:
class Auth extends Controller {
  function Auth()
  {
    $this->load->library('userlib');
  }

  function login()
  {
    // Show login form
  }
}

Then to make sure the user is logged in when accessing the blog/welcome controller you would do the following:
Code:
class Blog extends Controller {
  function Blog()
  {
    $this->load->library('userlib');

    // Check the user is logged in
    $this->userlib->check();
  }

  function index()
  {
    // Blog code goes here
  }
}

I hope my example is right and can help a little. The reason this method should work is your seperating the code to perform the different tasks out.

So all the code to check a user is logged in, to log the user in is in the Userlib library.

Then when you want to make sure a user is logged in, load the library and call the check method. If they are logged in, nothing will happen and the controller will load. If they aren't logged in then they will be sent to the login form.
#3

[eluser]mvdg27[/eluser]
Thanks adamp1,

I got it working now! The more I work with CodeIgniter, the more I see how easy it is to set up a fairly complex application!

Cheers! -Michiel
#4

[eluser]adamp1[/eluser]
With a MVC framework its so easy to write web applications and write them so they are modular. It may take you a bit to get used to how CodeIgniter does things, and you will be saying "But I could do this faster without CI". When your used to I though you want to go back.

Not only is it easy to write code fast but you can write good code. Iv been using CI now for 6 months and can't imagine not using it now.

Good luck, if you have any more issues do ask here, people will always be willing to help you.
#5

[eluser]Kurai[/eluser]
I have to thank you too. I put a similar question just some time ago, and this is quite the answer I was looking for.
I'm still a newbie, but learning fast. Codeigniter is not only pretty simple. It has a nice community too Smile
#6

[eluser]adamp1[/eluser]
Of course my example is very simplistic. The same principles should always be used when wanting to add large scale functionality to a site.

I would have written a bit more code for showing how to check for the user login but I'm sure that's simple enough to work out (Have a project in for tomorrow so its all hands on).

If you want to learn how to do it then I would advise continuing to write your own auth class. But there are some on the forums people have written, FreakAuth is one. Problem with using them is you wouldn't learn and they can offer too much when you only want something small. When I needed my first auth system I used FreakAuth but then did the same as you and wrote my own, big advantage I found is I understand how I wrote it, what methods I can use and such. While with someone else's your always consulting the documentation.

Good luck anyway




Theme © iAndrew 2016 - Forum software by © MyBB