Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Custom exception class
  • Hi.

    I'm trying to create a custom exception class in /packages/auth/classes/auth/exception.php like below.

    >>
    <?php
    /**
     * Fuel is a fast, lightweight, community driven PHP5 framework.
     *
     * @package    Fuel
     * @version    1.7
     * @author     Fuel Development Team
     * @license    MIT License
     * @copyright  2010 - 2013 Fuel Development Team
     */

    namespace Auth;


    class SimpleUserUpdateException extends \FuelException {}

    class SimpleUserWrongPassword extends \FuelException {}

    class OpauthException extends \FuelException {}

    class SimpleUserRestException {
        // Do something
    }
    <<

    And the exception is called from /packages/auth/classes/auth/login/simpleauth.php in function update_user().
    The exception is REST api exception (return error message to calling client) because my app updates user information through ajax requests.

    >>
    if (array_key_exists('username', $values))
    {
    $matches = \DB::select()
    ->where('username', '=', trim($values['username']))
    ->where('id', '!=', $current_values[0]['id'])
    ->from(\Config::get('simpleauth.table_name'))
    ->execute(\Config::get('simpleauth.db_connection'));
    if (count($matches))
    {
    throw new \SimpleUserRestException('Username is already in use.');
    }
    $update['username'] = $values['username'];
    unset($values['username']);
    }
    <<

    The exception is not called saying "Class 'SimpleUserRestException' not found".
    Why isn't it called? Please teach me how.

    Thanks in advance.

    Yossy
  • As a general rule, NEVER modify the framework. You'll going to regret that big time when it's time to upgrade, and you'll have to start merging changes.

    And there is no need for it. Not only are there plenty of places you can put your classes that doesn't interfere with the framework code, in this case the Auth exceptions there is afaik only one method that throws the same exception in multiple places, and that method uses the error code to identify the exact error message that has occured.

    And to answer your question: you have defined your class in the Auth namespace, but you call it from the global namespace. This requires the class to be known to the autoloader, and aliased from Auth to global. It doesn't know it, because you haven't added the new classes to the package bootstrap.

    But again, don't do it, don't change the package, bad idea.
  • Thanks for such a quick reply.
    All right, i wont do any modification on the package.

    Thanks again.
  • Another issue I spot is that in FuelPHP, files must contain only a single class, and the namespace and classname must map to the filename.

    This is not the case here, the autoloader will not be able to map SimpleUserWrongPassword to your exception.php. Moving it like this will still cause it to fail. If you want to do it this way, you have to inform the autoloader what classes are defined in that file, using the add_classes() method. You do this in the bootstrap file.

Howdy, Stranger!

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

In this Discussion