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.
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'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/"}
stucked my heads in some errors.i dont getting where i have done my mistake.
I am using laravel 5 and installed it.I want to use l5-repository so i installed https://github.com/prettus/l5-repository this repository using composer commnad:
composer require prettus/l5-repository
and i made all changes as per installation document and its working fine.
after installing repository using composer my directory structure is as below:
curovis
|-- composer.json
|-- composer.lock
|-- app
|-- bootstarp
|-- config
|-- database
`-- vendor
|-- composer
`-- prettus
`-- l5-repository
|-- src
| `-- Prettus
| `-- Repository
`-- composer.json
after this as per doc i have made following entry in /var/www/curovis/config/app.php:
Prettus\Repository\Providers\RepositoryServiceProvider::class,
and its working fine.
Now i want to change composer.json of root directory entry as following:
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"Prettus\\Repository\\": "vendor/prettus/l5-repository/src/Prettus"
}
},
and use composer update command. it also works fine.
now i want use same repo with another name so i have change composer.json with follwing:
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"RepoTest\\Repository\\": "vendor/repotest/l5-repository/src/RepoTest"
}
},
and add RepoTest\Repository\Providers\RepositoryServiceProvider::class, in app.php file.run composer update command. then it gives following error:
FatalErrorException in /var/www/curovis/vendor/laravel/framework/src/Illuminate/Foundation/ProviderRepository.php line 146: Class 'RepoTest\Repository\Providers\RepositoryServiceProvider' not found
i cant understand why laravel is looking for /var/www/curovis/vendor/laravel/framework/src this path instead of provided as "RepoTest\\Repository\\": "vendor/repotest/l5-repository/src/RepoTest" in composer.json.
is anything i am missing or any error in composer.
Thanks for help.
You NEVER add autoloading for the packages you added inside your main composer.json. The path "vendor" should never appear there.
I recognize you are trying to add a package, then modify it and use that instead. You changed the autoloading prefix from "Prettus" to "RepoTest", but did you also change the namespace in the PHP files? Simply renaming the path does not affect the PHP class names and namespaces, so if you rename a file, and inside that file there is no matching class defined, autoloading will fail.
Whatever it is you are trying to do, I think it is a good idea to ask about that instead of asking to fix problems you think are necessary because of the way you do solve your original problem. If you want to know how to modify an existing project and use your variant of it: Ask about it.
sovled above error by changing composer entry:
when i have see autoload_classmap.php and autoload_psr4.php files of /vendor/composer/ folder autoload_classmap.php file does not contain namespace that i require.
so i have made following change in my composer.json:
"autoload": {
"classmap": [
"database","vendor/repotest/src/Repotest/Repository/"
],
"psr-4": {
"App\\": "app/",
"Repotest\\Repository\\": "vendor/repotest/src/Repotest/Repository/"
}
},
so by making entry in "classmap": make entry in autoload_classmap.php and working fine now.
Thanks #sven for your help.
Example:
"autoload": {
"classmap": [
"database"
],
"files": [
"app/helper.php"
],
"psr-4": {
"App\\": "app/"
}
}
Default Composer file to load.
I'm looking at examples, and I cannot get my code to work.
Directory Structure
app
src
company
FileExport
FileExport.php
FileExportInterface.php
Validator
vendor
...
My composer.json
"require": {
"monolog/monolog": "1.9.1",
"ilya/belt": "2.1.1"
},
"autoload": {
"psr-4": {"Company\\": "src"}
}
Namespace is Company\FileExport.
Classes in vendor work fine, but not mine. I've run composer update as well.
Your autoload should look like so
"autoload": {
"psr-4": {"Company\\": "src/company/"}
}
I just need to autoload some classes, and I don't like the psr-0 namespace insanity (no offense).
This used to work just fine in my project:
"psr-0": {
"": [
"app/controller/",
"app/model/"
]
}
For some reason it doesn't work anymore, even though I'm using the same Composer version. I need it for a new project that is also using Silex. Could this be a conflict with Silex?
I know about the "classmap" option, but it's kind of useless because it requires that I run "composer install" every time I add a new class.
Any ideas?
Try to use "primitive" JSON properties; not an array (like in your example).
This works for me with psr-4 like you say, with "": "app/":
{
"autoload": {
"psr-4": {
"Robbie\\": "core/",
"": "app/"
}
},
"require": {
"monolog/monolog": "1.2.*"
}
}
This gives me the Robbie namespace under the directory core, as an example of sources not controlled by composer, the 3rd party (vendor) Monolog namespace and my default or non-namespace for sources underneath the app directory.
After a composer update, all of them are available when including the generated autoload.php:
<?php
require_once 'vendor/autoload.php';
// ...
?>
Use classmap in instead of psr-4:
"autoload": {
"classmap": ["models/"]
}
If you just want to regenerate the autoload file use composer dump-autoload.