PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, January 21, 2022

[FIXED] Why won't my Composer package autoload?

 January 21, 2022     composer-php, php     No comments   

Issue

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


Solution

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/"}


Answered By - Alex Howansky
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing