I get this error when I try to use autoload and namespaces. All my namespace classes are under app/libs/
16-Dec-2016 04:30:50 Europe/Berlin] PHP Fatal error:
Class 'App\libs\App' not found in /Users/mysite/app/page1.php on line 26
Here is My code:
require '../public/vendor/autoload.php';
use App\libs\App;
use App\libs\Auth;
class Controller
{
public $app;
public function __construct()
{
#set_exception_handler([$this, 'exceptionHandler']);
$this->app = new App();
}
}
autoload normally includes files only under vendor folders. It does not load any other files, if you do not instructe to. You are probably using composer. if it is, you can add folders in composer.json file to include class files from other folders like App\libs. An example a composer.json file is:
{
"require": {
"twig/twig": "~1.0"
},
"autoload": {
"psr-4": {
"App\\": "App/"
}
}
}
In the examle above, it will autoload any files under App folder.
Finally you need to run: composer dump-autoload to make this working.
Related
I am having very hard times understanding how to use autoloading by psr-4.After loading vagrant and setting and testing all variables in Homestead.yaml I have prepared a file structure as the following:
\app
\folder
-- test.php
\vendor
\composer
-- autoload.php
-- index.php
-- composer.json
and the following are my codes:
index.php
<?PHP
namespace app;
require('vendor/autoload.php');
$object = new folder\test();
composer.json
"autoload":{
"psr-4":{
"app\\": "app"
}
}
test.php
<?php
namespace app\folder;
class test
{
function __construct ()
{
echo 'construction done right.';
}
}
But, after trying to visit the page, these are the error message displayed on the page:
(!) Fatal error: Uncaught Error: Class 'app\folder\test' not found in /home/vagrant/web/sites/app/index.php on line 6
( ! ) Error: Class 'app\folder\test' not found in /home/vagrant/web/sites/app/index.php on line 6
Would you help me understand and fix this error?
It doesn't work because you have told Composer that the classes from the app namespace are in the app subdirectory but there is no app subdirectory.
The entire application is stored in the app directory and it's name doesn't really matter for the application. The classes of the app namespace are stored in the current directory and the sub-namespaces are stored in subdirectories with the same name.
Accordingly, your composer.json file should read:
"autoload": {
"psr-4": {
"app\\": ""
}
}
Or, to be more clear, you can put . (the current directory) as the location of the app\ namespace:
"autoload": {
"psr-4": {
"app\\": "."
}
}
After you make the change run composer dump-autoloader in the main application directory and it will start working.
To fix it for your current setup, use the following:
"autoload":{
"psr-4":{
"app\\": ""
}
}
Your composer.json is in the app-directory, so there's no subdirectory named app to reference.
I would actually recommend to change your directory structure to the following:
\app
\src
\folder
-- test.php
-- index.php
\vendor
\composer
-- autoload.php
-- index.php
-- composer.json
And then in composer.json, set the following:
"autoload":{
"psr-4":{
"app\\": "src"
}
}
This makes sure that all files belonging to your 'app' namespace are contained within a single subdirectory.
Finally, I would recommend you to use a vendor namespace to prevent conflicts, and to use the naming guidelines from PSR-2.
I think you won't need PSR-4 just add classmap
classmap parameter for example :
"autoload": {
"classmap": [
"app"
]
}
After adding this run composer dump-autoload and you should see a number of classes being added.
Hope this helps.
in composer.json try to set
"autoload":{
"psr-4":{
"": "src/"
}
}
then do execute this command
composer dump-autoload -o
I have structure directory
autoload composer:
"autoload": {
"psr-4": {
"model\\": "src/"
}
},
my class
namespace model;
class ClientAgent
{
private $pdo;
public function __construct(\PDO $pdo)
{
$this->pdo = $pdo;
}
public function sentAgent()
{
}
}
in index.php i tried add
use model\ClientAgent; but it throw error, not found class? why?
Edit after answer
"autoload": {
"psr-4": {
"model\\": "src/model/"
}
},
my index.php
use model\ClientAgent;
$loader=require_once __DIR__ . '/../vendor/autoload.php';
$clientAgent =new ClientAgent($pdo);
error
Uncaught Error: Class 'model\ClientAgent' not found in C:\xampp\htdocs\Wieloagenty\index.php:15
My suggestion is to introduce a vendor prefix. That might be your developer name, the name of your company or the name of the application.
composer.json
"autoload": {
"psr-4": {
"YourApplication\\": "src/"
}
},
Now, every class inside the src folder and below needs this vendor prefix on it's namespace.
Let's take src\model\ClientAgent.php as example:
namespace YourApplication\Model;
class ClientAgent
{
Now, the FQCN (full qualified class name) is YourApplication\Model\ClientAgent and you might use it as part of a use statment.
// first require the Composer autoloader
require_once __DIR__ . '/../vendor/autoload.php';
// declare which other classes you are using
use YourApplication\Model\ClientAgent;
$clientAgent = new ClientAgent($pdo);
Important!
After the modifications (to classes and the composer.json file) please regenerate the Composer autoloader with php composer.phar dumpautoload -o.
Composer will scan the complete src folder including subfolders for classes (so you'll have all classes from src\models\ and src\views autoloading ready).
"model\\": "src/" will give you the folder src/ as the base for the model-namespace. So this would give you model\model\Classname.
Change it to:
"psr-4": {
"model\\": "src/model/"
}
When defining psr-4 autoloader in composer, you associate a folder with a specific namespace.
Any sub folders will be a sub-namespace. So if you create a folder in your "model"-folder, the namespace would be: model\new-foldername\Classname and so on.
Note: When ever you update your composer.json file, you always need to run the command: composer dump-autoload to make composer regenerate all it's cached files.
I am adding some custom classes to a project. I am struggling to get psr-4 autoloading to work.
I am placing my custom classes in a folder at [project root]/lib
Here is the autoload bit from my composer.json:
"autoload": {
"psr-4": {
"App\\": "app/src",
"Lib\\": "lib"
}
},
I have a file P_Database.php in the lib folder with the following declarations:
<?PHP
namespace Lib;
class P_Database
{
//
I call the class in a file with:
use Lib\P_Database;
require __DIR__ . '/../vendor/autoload.php';
$db = new P_Database();
I get a 500 error:
PHP Fatal error: Class 'Lib\\P_Database' not found....
I have run composer dump-autoload.
Can anyone point me in the right direction?
Actually Iam new to PHP. I am running this from a nearly empty folder (actually following along a Lara-casts tutorial: Design a Fluent API with TDD).
My directory structure looks like
src
Expression.php
tests
ExpressionTest.php
vendor
composer.json
composer.lock
Inside composer.json:
{
"require-dev": {
"phpunit/phpunit": "^5.2"
},
"autoload": {
"psr-4": {
"": "src/"
}
}
}
Inside ExpressionTest.php:
class ExpressionTest extends PHPUnit_Framework_TestCase
{
/** #test */
public function it_finds_a_string()
{
$regex = Expression::make()->find('www');
$this->assertRegExp($regex, 'www');
}
}
Inside Expression.php
<?php
class Expression
{
}
I then run composer dump-autoload and run phpunit but I still get:
"Fatal error: Class 'Expression' not found in
C:\Users\nobis\code\testing2\tests\ExpressionTest.php on line 8"
Is there something wrong with my syntax? My understanding of Composer is very basic. Thanks in advance.
PHPUnit does not know about Composer natively, therefore without configuring PHPUnit, it will not know about your autoloader setup.
try running PHPunit with --bootstrap vendor/autoload.php to tell it to load with your autoload file.
If that does not work, check your namespace value in your Composer configuration (i.e. "": "src/" might need to change.)
You need to include the autoloader at the top of your test. It is typically at
require __DIR__ . '/vendor/autoload.php';
You can also add a phpunit.xml file to your tell it where the autoloader is then it will run it before every test:
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/5.2/phpunit.xsd"
bootstrap="/path/to/bootstrap.php"
</phpunit>
This is my actual composer.json file
"autoload": {
"psr-0": {
"Raggaer": "app/"
}
}
My file structure is the following:
app
bootstrap
controllers
views
Inside bootstrap I got app.php and start.php
Start.php
$app = new App($uri);
App.php
namespace Raggaer\Bootstrap;
class App { .. }
And im getting the following error
Fatal error: Class 'Raggaer\Bootstrap\App' not found