Defining new Exceptions on Yii2 - php

I'm developing some web services with Yii2 framework.
When I was writting one of the functionalities of a controller, I defined a custom exception that extends Exception on the same file as controller.
class CustomException extends Exception {}
Then I saw that I need the same exception for other controllers.
And I think: DRY!
I created a file CustomExceptions.php on \components with:
namespace app\components;
The problem is that now I'm importing this exception with 'use' key:
use \app\components\CustomException;
It seems that everything works well, and my IDE (JetBrains) is able to find the custom exception code.
But then when I run this code It fails.
I don't know why but It seems that It can't find this class.
What I'm doing wrong?
Thanks!!

I found this: yii2: Proper way to throw new exception
So u can ovveride UserException:
<?php
namespace app\components;
use yii\base\UserException;
class CustomException extends UserException
{
}

Try to
use app\components\CustomException;
instead of
use \app\components\CustomException;

Related

laravel FatalThrowableError "calss not found"

I am new with laravel and I add "steffenbrand/dmn-decision-tables" package then I created the function to use it
like this step steffenbrandDmn
but when I use the function don't work and throw an exception
Class 'App\Http\Controllers\HitPolicy' not found
canyoneone help me???
You have created a policy and you are using it within your controller, but haven't imported.
So in your controller class, above the class declaration put:
use App\Policies\HitPolicy;
or the path to the HitPolicy class wherever is created.

Custom Exception not found in Cakephp3

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?
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 {}
?>
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.

How Can I Set Global Validation Rule - YII2

I have started with YII2 just today. i have installed it successfully and also created required Model, Controllers & views in that using CRUD.
Now my question is as below:
I want to create one Global Validator Rule which we can use in throughout the Models in system.
What i have tried so far:
For that i have created one new model file(FormValidator) in common folder and generate one custom rule method in it.
common/model/FormValidator.php
namespace common\models;
use Yii;
use yii\base\Model;
class FormValidator extends \yii\db\ActiveRecord {
}
then i tried to extend this model file to my frontend model file. but its not working.
frontend/model/Customerprofile.php
namespace frontend\models;
use Yii;
use common\models\FormValidator;
class Customerprofile extends FormValidator{
}
It's throw below error message:
PHP Fatal Error – yii\base\ErrorException
Class 'common\models\FormValidator' not found
I don't know what i have missing in all these process, if anyone of you help me out from this, would be much appreciated!
Thanks in Advance.
If you want to extend some master class for model you have to simply extend CActiveRecord and then your models will extend this masterclass.

Adding PHP namespaces into an existing ZF1 application

I'm trying to integrate PHP namespaces into an existing Zend Framework project (v1.12). When I add namespacing at the top of a working controller, it doesn't work anymore and the application throws an Invalid controller class error. Here's my controller definition :
namespace MyProject\Controller;
use MyProject\Controller\MyRestController;
class MyFooController extends MyRestController
{
}
and the init method within the Bootstrap.php:
protected function _initAutoload()
{
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace('MyProject');
return $autoloader;
}
Just a guess (have not used ZF for quite some time): Zend will not accept any class as a controller, just those extended from the framework's base controller class. As you don't extend from the frameworks base controller class you see the error.
If that is the reason, take care you initially extended from the base framework controller class or you implemented the needed interface.
namespace MyProject\Controller;
class MyRestController extendes Zend_Framework_Base_Controller_Class_Name_Here
{
...
p.s. the use MyProject\Controller\MyRestController; looks superfluous as that class is in that namespace already. Let's review your code:
namespace MyProject\Controller;
This sets the namespace of the file. That means, non-FQCN will resolve into it. For example:
new MyRestController();
Resolves to the following FQCN:
new MyProject\Controller\MyRestController
Which - oha! - is exactly what you wrote in use:
use MyProject\Controller\MyRestController;
Which means, that this use clause is superfluous, the extend in:
class MyFooController extends MyRestController
Would go to it anyway at first. Because it's the same namespace.
I am facing similar problem now. For me this looks like that Zend cannot properly resolve namespaced controller name. So when I put for example IndexController into namespace \Basic\Controller, it will be not loaded because Zend want to load \IndexController class, which does not exist.
I am thinking about extending standard zend router class, which has method getControllerName.
Then I can set this in bootstrap by:
$router = new \My\Namespaced\Router();
$front = Zend_Controller_Front::getInstance();
$front->setRouter($router);
I didn't tried that code yet but this should work.

What sort of exceptions should I be throwing from inside Kohana 3?

I've seen the code examples on this article, but throwing Controller_Exception_404 produces an error.
I've just been throwing plain exceptions. I remember in Kohana 2.3 there were different ones you could throw, depending on the situation.
Does anyone have a list of what exceptions should be thrown when?
I think the exception you want is Kohana_Request_Exception. Here's a list of all the exceptions Kohana defines (generated using grep -iR "class .*Exception" .):
class Validate_Exception extends Kohana_Validate_Exception {}
class Kohana_Validate_Exception extends Kohana_Exception {
class Kohana_Request_Exception extends Kohana_Exception { }
class Kohana_Exception extends Exception {
class Kohana_View_Exception extends Kohana_Exception { }
If you want a 404 response code, I think you'll also have to do this in your controller
$this->request->status = 404;
I don't know what the "official" best practice is, but this is what I've found by playing around.

Categories