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.
Related
I am using Laravel 5.8 and I'm attempting to modify a package class from the Vendor directory. To acheive this, I have created a new class which extends the Vendor class, and I can replace the named functions within it- all working great.
However, the original class 'uses' a class, which I have mimicked in my new class, as follows:
use VendorName\PackageName\OriginalController
// use VendorName\PackageName\SomeClass as StoreRequest; How can I replace this...
use App\Http\Requests\NewRequestClass as StoreRequest; // ... with this..? (not working)
class NewController extends OriginalController {
private function somefunction(StoreRequest $request){ // This doesn't work; it is still using the StoreReqest defined in OriginalController
// ...
}
}
See comments- Is it possible to override this?
Its generally not possible modify/delete class or function. Not without extensions like Classkit. But i am not really a fan of this type of code. But you can Check these questions, which might help:
Redefining PHP class functions on the fly?
Deleting entire PHP Class
Redefining PHP function?
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.
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;
I am using a Facedtector class for my work. I added the class in my Codeigniter libraries folder and just called from my controller class. In my controller I also loaded the class but run time it is showing following error:
An Error Was Encountered
Non-existent class: FaceDetector
Please anyone can help me about this.
Thanks
Add your class file inside you library folder of your main application folder.
And then you can use the auto load function of codeigniter
application/config/autoload.php
at the above location and then you can access the class directly or other way of using this use
$this->load->library('Your_Class_Name', 'Path\to\your\class');
This way you can access your class in codeigniter
You can follow below link for autoload of a class library.
https://ellislab.com/codeigniter/user-guide/general/autoloader.html
Hello guys i created Entity folder inside application/model/. In Entity folder i created file Mj_user.php and give class name as same Mj_user.
but when i try to access that class using
$User= new Entity\Mj_user;
it's giving me error
Fatal error: Class 'Entity\Mj_user' not found in C:\xampp\htdocs\project\application\controllers\user.php on line 15
what should i do Please help me..but when i remove Mj_ then put only file name as User.php. Its working properly..Please help me
When creating a new class you have to be careful, the name of the class must be:
class Mj_user extends CI_Model {
function __contruct()
{
partent::construct();
}
}
The file must be mj_user.php and for using this model you must load it before start using it
it can be preloaded for all in the configuration or you can load it whenever you need it like this
$this->load->view('mj_user');
And in the error seems that is looking for a file called user.php and should be mj_user.php