简体   繁体   English

如何使用 cakephp 插件中的自定义异常?

[英]How to use custom exception from cakephp plugin?

I have some custom exception for my application.我的应用程序有一些自定义异常。 I placed them to app where it is running well.我将它们放在运行良好的应用程序中。 Now I want to move them to one of my plugin and use exception from their.现在我想将它们移动到我的插件之一并使用它们的异常。

I have implemented custom exception as mentioned here: http://book.cakephp.org/2.0/en/development/exceptions.html我已经实现了这里提到的自定义异常: http : //book.cakephp.org/2.0/en/development/exceptions.html

But, I am looking for same solution if I use exception classes from cakephp plugin.但是,如果我使用 cakephp 插件中的异常类,我正在寻找相同的解决方案。

Any suggestion...???有什么建议吗……???

Lets say you want to put your exceptions in Custom plugin /app/Plugin/Custom/Lib folder:假设您想将例外放在自定义插件 /app/Plugin/Custom/Lib 文件夹中:

// Location: /app/Plugin/Custom/Lib/CustomException.php
<?php
class CustomException extends CakeException {};

Now you need to load Custom plugin in /app/Config/bootstrap.php:现在你需要在 /app/Config/bootstrap.php 中加载自定义插件:

// Location: /app/Config/bootstrap.php
CakePlugin::load('Custom');

And wherever you want to use CustomException :无论您想在何处使用CustomException

<?php
App::uses('CustomException', 'Custom.Lib');

class ApiController extends AppController {   

    public function demo() {
        throw new CustomException("Just testing");
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM