Using Doctrine ORM with namespaces - php

Official guide explains how to use Doctrine ORM with /src directory and it works okay, however, I have a project with a structure like this:
vendor/
src/
Entities/
Category.php
public/
.htaccess
index.php
bootstrap.php
cli-config.php
composer.json
And I want to have namespace App so I can do this from public/index.php:
use App\DB\Entities\Category;
How should I configure autoload option and bootstrap.php to do this? Composer file currently has this autoloader:
"autoload": {
"psr-4": "/src/Entities"
}

You should update your autoload section in the composer.json file with this configuration:
"autoload" : {
"psr-4" : {
"App\\DB\\Entities\\" : "src/entities/",
}
}
And your entity class should look like this:
<?php
namespace App\DB\Entities;
class Category
{
function __construct(){
}
...
}

Ok, following what composer's documentation says you should have something like this:
"autoload": {
"psr-4": {
"App\\": "./src"
}
}
and Category.php must be created like this:
<?php
namespace App\Entities;
class Category
{
}
Folder structure example

Related

PHP class_exists() cannot find a class that truly exists

I am trying to write a CakePHP Authentication plugin and am following and structuring it after this repository: https://github.com/ADmad/cakephp-jwt-auth
I am still at the early stages, trying to get my plugin to load during cakePHPs constructAuthenticate() method. I have narrowed down my issue to this method never finding my class when it calls class_exists()
I have Project structure as follows:
App/
plugins/
src/
Controller/
AppController.php
Model/
vendor/
Admad/
cakephp-jwt-auth/
src/
Auth/
JwtAuthenticate.php
composer.json
nates/
cakephp-total-auth/
src/
Auth/
TotalAuthenticate.php
composer.json
TotalAuthenticate is the class I'm trying to load, and it's namespace as defined in TotalAuthenticate.php is:
namespace nates\TotalAuth\auth;
After some debugging I have found that the Path being passed to classs_exists() is:
nates\TotalAuth\Auth\TotalAuthenticate
I have compared all of this info to the Admad/JwtAuth plugin and the relative paths all match up, and that plugin loads just fine so I'm really at a loss at whats going on here and why my plugin won't load.
My Autoload in App/composer.json Looks as such:
`"autoload": {
"psr-4": {
"App\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"App\\Test\\": "tests/",
"Cake\\Test\\": "vendor/cakephp/cakephp/tests/"
}
},`
And my Plugins composer.json :
`"autoload": {
"psr-4": {
"nates\\TotalAuth\\": "src"
}
},
"autoload-dev": {
"psr-4": {
// "ADmad\\JwtAuth\\Test\\": "tests"
}`
PSR-4 autoloading standard requires that the namespace matches the file structure case-sensitive. You define your namespace in composer.json with capitals nates\TotalAuth, but in your class as nates\totalauth\....
Make sure all casings match and the casings match the file structure.

Class file not found by composer and psr-4

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

Why won't my Composer package autoload?

I've been testing getting my packages up to Packagist with a simple class I made. Whenever I require it in a different project, it says the class cannot be found. Is something wrong with my composer.json autoload block?
Here's my project repo file structure:
- src
- prodikl
- View.php
- .gitignore
- composer.json
And here's my composer.json:
{
"name":"prodikl/simple-view",
"description":"A simple to use, basic View object.",
"require" : {
"php" : ">=5.3.0"
},
"autoload": {
"psr-4": {"prodikl": "src/"}
}
}
And finally, in my View.php:
<?php
namespace prodikl;
class View
{
...
}
But whenever I require it into a project and do require "vendor/autoload.php" and use use prodikl\View; it it keeps saying not found
You just need to point your autoloader down one more directory:
"autoload": {
"psr-4": {"prodikl": "src/prodikl/"}
}
This means "classes that belong to the \prodikl namespace can be found in the src/prodikl/ directory."
You might need trailing backslashes on the namespace name too, not sure how picky Composer is about it:
"psr-4": {"prodikl\\": "src/prodikl/"}

PSR-4 autoloading trouble

I want to load HomeController class from lib directory:
root/
-lib/
--/HomeController.php
-vendor/
-composer.json
-index.php
Composer.json
"autoload": {
"psr-4": {
"Lib\\": "lib/"
}
}
HomeController.php
namespace Lib;
class HomeController {}
index.php
var_damp(new \Lib\HomeController.php);
It doesn't find the class.
But if I put HomeController.php inside Controllers directory:
root/
-lib/
--/Controllers/HomeController.php
And update the namespaces: index.php to var_damp(new \Lib\Controllers\HomeController.php); and HomeController.php to:
namespace Lib\Controllers;
class HomeController {}
It works perfect.
It's weird, I can't find any docs talking about it. I don't need additional directories, in this case I want the HomeController class directly inside lib directory.
How can I make it works inside lib folder?
I think the trailing slash in the path reference is your problem. Change the autoload section in composer.json to this:
"autoload": {
"psr-4": {
"Lib\\": "lib"
}
}
...then run composer dump-autoload.

composer autoloader psr-0 namespaces

I have create a custom composer package but I am having troubles to set the correct autoload options for it.
All my classes are under MyNamespace/Common namespace. So for example for including my ArrayHelper class I do use Mynamespace/Common/Helper/ArrayHelper.
This is the relevant part of my composer.json:
"autoload": {
"psr-0": { "MyNamespace\\": "" }
}
I have read this: composer.json / autoload
Any help?
You have to navigate the file location of your namespace.
"autoload": {
"psr-0": { "MyNameSpace": "./<path to your parent directory>" }
}
For example, this is my directory structure:
composer.json
source
\-Data
|-Controller
\-Repository
Then, in the composer.json file:
"autoload": {
"psr-0": { "MyNameSpace": "source/Data" }
}
Then, I can define classes in these namespaces:
/* namespace for classes in controller directory */
namespace MyNameSpace\Controller;
/* namespace for classes in repository directory */
namespace MyNameSpace\Repository;

Categories