Class not found error in Lithium Framework (li3) - php

I am using Lithium Framework. I am trying to call class into a controller, that class is placed inside libraries folder.... But it is showing this error
Fatal error: Class 'app\libraries\Test' not found in /home/ali.mehdi/AvonTPH/app/controllers/SessionsagentController.php on line 34
In libraries folder I created Test.php having following code.
<?php
namespace app\libraries;
class Test{
public static function getTest(){
return "Hi";
}
}
Also Inside my controller.. I used following using statements:
use app\libraries\Test;
But Why Test Class not found... Am I missing something? Any Help would be appreciated.

As #Matei Mihai has commented, in your app\config\bootstrap\libraries.php add the line Libraries::add('.'), though I'd advise against this (see below).
Be aware that the best practice is to put your classes into a package and not drop them in the libraries folder.

Related

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.

PHP namespace confusion, class not found

I have two classes in the same folder in own files. But when I am trying to extends one to another it is giving namespace and class not found error.
Info: It is the first time I am extending class using namespace. Also nested namespace is new to me. DB\CRUD So may be I am doing
completely wrong with namespace.
Error message:
Fatal error: Class 'DB\AT_Database' not found in /var/www/...
DB class
File: AT_Database.php
namespace DB;
class AT_Database
{
...
}
CRUD class
File: AT_CRUD.php
namespace DB\CRUD;
use DB\AT_Database;
class AT_CRUD extends AT_Database
{
public function __construct()
{
}
}
This may be silly mistake or may be I have overlooked it (which I should not as a programmer) and that is loading sequence of the class.
May be it's not worth to have as an answer but just adding so by chance in future it can help to someone who make such mistake.
As I mentioned in one of my comment, I am using glob to auto load all class files to include.
foreach ( glob( $this->classes_dir . "/*.php" ) as $class ) {
include_once $class;
}
Now my file names are AT_CRUD.php and AT_Database.php. Here I realized that php loads files in alphabetical order. So when I extends AT_Database class into AT_CRUD its never found.
This is just because php loads AT_CRUD first than AT_Database so either I have to instantiate the class into or to use something like dependancy injection as #prehfeldt mention in his comment.

getting error when adding a class in codeigniter

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

php use one direction back

I'm trying to get working use.
My file is in /application/controllers/indexController.php
use application\models\Database;
class IndexController extends Controller {
public function indexAction() {
$db = new Database();
$this->view->render('index','template');
}
}
My Database class is in /application/models/Database.php so i wrote use application\models\Database
But it tells
Fatal error: Class 'application\models\Database' not found in
Z:\home\localhost\www\application\controllers\IndexController.php on
line 7
How i can get it worked?
use is not magical.
You need an autoloader to tell it where to find a certain namespace.
So right now your code is looking for a class called application\models\Database in the same directory as your currently executing file.
I think what you meant to do is:
include 'application\models\Database.php';

PHP Zend Framework: How do I make ZF Tool work with extended Controller_Actions?

I'm working with a Zend Framework project and using the ZF tool from the command line. After setting up some initial structure, I extended the Zend_Controller_Action class with something like MySite_Controller_Action and made the existing controllers point to that. So now I have something like:
class IndexController extends MySite_Controller_Action
{ ... }
and
abstract class MySite_Controller_Action extends Zend_Controller_Action
{ ... }
The problem is that now when I attempt to run a command like
Bash$ zf create action edit Index
I get an error like this:
Creating an action named edit inside controller at /Library/WebServer/Documents/MySite/application/controllers/IndexController.php
PHP Fatal error: Class 'MySite_Controller_Action' not found in /Library/WebServer/Documents/MySite/application/controllers/IndexController.php on line 3
Fatal error: Class 'MySite_Controller_Action' not found in /Library/WebServer/Documents/OurMods/application/controllers/IndexController.php on line 3
Can anyone offer up any ideas? I've done a little searching, but I don't even know where to start on this one.
NOTE: I HAVE loaded a 'MySite' namespace via the application.ini file as follows:
autoloadernamespaces.MySite = "MySite_"

Categories