Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
HMVC Module Exceptions
  • I would like to keep my apps modular structure but am curious where the best place to put exceptions would be?
    Currently I have a module called content and it is working perfectly, calling my content_model and such, and I would like to have my exception class inside this module, but where? The only place it seems to work is if it is placed in app/classes/contentexception.php app/classes/contentexception.php <?php
    class ContentException extends Exception { /**
    * Process the application error
    *
    * @return void
    */
    public function handle()
    {
    //exception
    }
    } Inside modules/content/classes/content.php throw new \ContentException();
  • Exceptions are classes like any other, so they are namespaced as any other.
    namespace Content;
    
    class ContentException extends \Exception {}
    

    Within the module you can just throw ContentException (as it is in the same namespace), outside the module namespace you will have to use \Content\ContentException.
  • Thank you so much for the response, I really appreciate it. What you said makes sense, but what I am curious about, is where is the best place to put the exception classes, within the module context. I ended up taking them on to the end of the file /modules/content/classes/controller/content.php, but I would love if I could put them in their own file. Let me know if this makes sense. Thank you again for your input.
  • You should store every class in a separate file, so that the autoloader can load it when it's requested. All classes go in the classes folder, same for app, modules or packages. So ContentException goes into modules/content/classes/contentexception.php.
  • Thank you very much, I had tried that before, but not with the correct namespace, it worked this time.

Howdy, Stranger!

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

In this Discussion