Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Extending Packages (ex. Auth)
  • Background: I'm currently working on porting a fairly complex app to fuel. The app requires frequent querying for the current user's groups. I'd like to extend the Auth package to include a kohana-like check_group() function. Is there a simple way to extend the Auth class, or am I better off just making an auth helper to do this?
  • Isn't this working?
    class My_A11n extends A11n {}
    
  • @kelSO.B
    I'll try and have the docs on extending the Auth drivers ready by thursday for the RC release. The Auth package is considered a "core" package which means you can extend it from you app.
    Create a file in fuel/app/classes/auth/group/mygroup.php like this:
    <?php
    
    class Auth_Group_MyGroup extends Auth_Group_Driver {
    
    }
    

    Or if you want to use the SimpleGroup driver but add/replace a method in it, create the file in fuel/app/classes/auth/group/simplegroup.php:
    class Auth_Group_SimpleGroup extends Auth\Auth_Group_SimpleGroup {
    
    }
    
    But in this case it works the same as replacing a core class you have to register it in the autoloader, that's explained in the docs. @daGrevis
    He's talking about the Auth package so I assume he meant Fuel's native Auth and not yours.
  • Hello, sorry to bump this post, but thought it would be better for it to have a complete information. so I've created APPPATH classes/auth/login/simpleauth.php the content of this file is as follows:
    <?php
    
    class Auth_Login_SimpleAuth extends Auth\Auth_Login_SimpleAuth {
    
     public function create_user($username, $password, $email, $group = 1, $refered_by = '', $question = '', $answer = '', $notify = 0)
    {
        // content here
    }
    
    }
    

    My APPPATH.'bootstrap.file has this line: (probably here will be an error..)
     'Auth' => APPPATH.'classes/auth/login/simpleauth.php',
    

    and now I get the error like this: ErrorException [ Error ]: Class 'Auth\Auth_Login_SimpleAuth' not found [url http://d.pr/DALj ] Maybe someone can point me on what I am doing wrong? Thanks!
  • Thanks Jelmer, that's nice and straightforward. You mentioned that this works because Auth is a "core" package. Does this mean it won't work for 3rd-party packages?
  • You're saying Auth should point to the simpleauth login driver. I'll try to expand in the morning, but the class name should match the file you're pointing to.
  • How it works always depends on how it was implemented. Auth being a "core" package in this way means you can extend a driver in the original namespace to the global namespace under the same name in a way that it'll be used by the original sibling classes as well. Something that's not possible when you don't register the package as a "core" extension.
    In general it will always be possible to replace classes from packages or to extend them in the global namespace, but to do it in a way that the original class is still accessable and the sibling classes will use your extension it needs to work like I described for the core extensions.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion