Go to content Go to navigation Go to search

People

Businesses

CakePHP remember Me? (part 2) 4 December 2006

So I was having a conversation with Mariano Iglesias and mrwooster(username) on the cakePHP list and Mariano made an instant and awesome suggestion:

Looking at your code, I would have to say that it is not recommended to add
models at the AppController level. Your code is just asking to be made a
component :)
Something like app/controllers/components/remember_me.php:

class RememberMeComponent extends Object 
{ 
        var $components = array('Session', 'Cookie'); 

        var $uses = array('User'); 

        function remember() 
        { 
                if(!$this->Session->check('User') && 
$this->Cookie->check('login')) 
                { 
                        $cookie_arr = $this->cookie->read('login'); 
                        $someone = $this->User->findByEmail($cookie_arr[0]); 

                        if($someone['User']['password'] == $cookie_arr[1]) 
                        { 
                                $this->Session->write('User', 
$someone['User']); 
                        } 
                } 
        } 

}

Then on your AppController you add RememberMe as a component, and on your beforeFilter() you do:

$this->RememberMe->remember();

I will do this. Shortly I will have a component that can remember people using rossoft’s cookie component.

Sphere: Related Content

Comments

  1. i’m confused with why it is not recommended to have a model at the appContoller level and is also not recommended in a component,

    ... quoted on this page http://bakery.cakephp.org/articles/view/61 it says “ ... I found out that using a Model in a Component is not a recommended practice and should be done only in special situations” ...

    but i think this component is one of those special cases.

    copongcopong    Dec 5, 05:54 AM    #
     
  2. Okay, so the problem here was not putting a model in the app_controller level, but putting so much stuff that really should have been in a component in the first place.

    But yes, you should avoid putting models in the app_controller level. I think that this particular component is a special case when it comes to putting models in the component. There is a point at which following arbitrary rules can be taken too far.

    I think though, that this doesn’t excuse what I did initially. I believe that I have now struck a proper balance between what should be done and what must happen to get this to work.

    Walker Hamilton    Dec 5, 07:16 PM    #
     
  3. This is funny. Did you really ever tried to use this component?

    You can’t get to Model from a component. At least through $uses variable.

    zgollum    Jan 25, 10:57 AM    #
     
  4. I did. I used it for quite a while. I haven’t used it though, since they built the Cookie component into the core of 1.2.

    p.s. Don’t be a jackass.

    Walker Hamilton    Mar 22, 10:29 AM    #
     

Leave a Comment