1

I want to create and use a custom exception class in my CakePhp Application.

So I created a DuplicateConfigurationException.php with the following class skeleton:

<?php
namespace Cake\Exception;
class DuplicateConfigurationException extends Exception{

} ?>

I a controller, where I wish to raise the Exception, I added

use Cake\Exception\DuplicateConfigurationException;

and within a function I call

throw new DuplicateConfigurationException();

Following suggestions throughout the interwebs, I have tried to place the php file in the following locations, but neither of them seems to work:

src/Exception src/Exceptions src/Lib src/Lib/Error src/Lib/Error/Exceptions

I always get an error:

Error: Class 'Cake\Exception\DuplicateConfigurationException' not found
File /host/var/www/src/Controller/StructuresController.php
Line: 246

What else do I need to do to make Cake recognize my custom exception?

I'm well aware of Loading custom class in CakePHP3, but since this exception is not a separate library I would rather not place it within vendor?

Community
  • 1
  • 1
Stephan Richter
  • 1,139
  • 11
  • 31
  • The linked question has nothing to do with placing things in a `vendor` folder, that was just a comment from a rather unexperienced CakePHP/PHP user (no offense). Apart from the false conclusion that `Library` is somehow a reserved name, the question shows how it should be done, how the namespace should look like, and what folder path matches the namespace (PSR-4). – ndm Feb 17 '17 at 14:51
  • I'm very sorry to report, that this comment was not very helpfil. The linked question does neither show a conclusive solution (rather depicts an example that was not working). I think renaming the folder to Berry would not suit my needs. Also it gives an example of a namespace, that not seems to work in combination with the given directory setup. However, you poked me into the right direction, i finally was able to work it out. – Stephan Richter Feb 18 '17 at 00:23
  • So in other words, it was helpful enough. As mentioned, the conclusion is false, and the example in the question shows a perfectly valid PSR-4 style namespace/folder setup, `App\Library\Config` matches `src/Library/Config.php` just fine, it's the same scheme that works for you. – ndm Feb 19 '17 at 16:30

2 Answers2

3

A bit late but I think it might be useful for other users with the same question to have some further explanations.

In fact, with your solution, you rely on native PHP SPL Exception class located in global namespace.

To use Cake's basic Exception class, you missed to add

use Cake\Core\Exception\Exception;

in src/Exceptions/DuplicateConfigurationException.php for loading Cake Exception class constructor. See Cake's book

Your code is working because Cake is handling SPL exceptions the same way than its own Exception class. If you've wanted to go further with a custom handler for instance, it may have broken logic.

Note that class IniPermissionsException extends \Cake\Core\Exception\Exception {}; is also working. In this case, you must prepend \ as the root namespace when calling a class in an extends statement because you need to provide full namespace path.

To swim like a dolphin in Cake's namespaces, just go to API reference.

Full updated code for src/Exceptions/DuplicateConfigurationException.php :

<?php

namespace App\Exceptions;

use Cake\Core\Exception\Exception;

class DuplicateConfigurationException extends Exception {}

?>
Bertrand
  • 1,840
  • 14
  • 22
1

Ok, after some fiddling I managed to get it working:

in src/Exceptions/DuplicateConfigurationException.php

<?php 
namespace App\Exceptions;
class DuplicateConfigurationException extends \Exception{

} ?>

in the controller:

use App\Exceptions\DuplicateConfigurationException;
...
function somefunction(){
   throw new DuplicateConfigurationException();
}

Apparently the namespace should be App\<Folder> and App\<Folder>\<Classname>, respectively.

And I had to prepend Exception with a backslash, since it is used in a namespaced context: http://www.php.net/manual/en/language.namespaces.global.php

Still, I'm not sure where the namespace conventions for CakePhp 3 are documented.

Stephan Richter
  • 1,139
  • 11
  • 31