2
votes

I'm trying to develop my first package but I already struggle trying to do so. I've followed two tutorials and done everything exactly as described however I'm getting:

Class 'Krenor\Ldap\LdapServiceProvider' not found

My folder structure :

project
- app  
- ...
- packages
-- krenor
--- ldap-auth
---- src
----- LdapServiceProvider.php (within src/)
---- composer.json (outside src/)

My composer file looks like this :

{
    "name": "krenor/ldap-auth",
    "description": "Authentication via LDAP and Laravel 5.1.x",
    "type": "project",
    "license": "MIT",
    "authors": [
        {"name": "MyName","email": "MyMail"}
    ],
    "require": {
        "php": ">=5.5",
        "laravel/framework": "5.1.*"
    },
    "autoload": {
      "psr-4": {
        "Krenor\\Ldap\\": "src/"
      }
    }
}

And my Provider File :

<?php

namespace Krenor\Ldap;

use Illuminate\Support\ServiceProvider;

class LdapServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        dd('I am alive');
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        dd('I am alive, too!');
    }
}

After creating the json file I've went into my project root folder and did composer dump-autoload
Also I've made sure that I added 'Krenor\Ldap\LdapServiceProvider' to the providers
array in /config/app.php.

Am I missing something here?

1
your LdapServiceProvider.php file should under correct folder as the namespace. In this case should be src/Krenor/Ldap/LdapServiceProvider.phpyangqi
Istn't my psr4 section already pointing with the namespace "Krenor/LDAP" to the src/ folder..?Krenor
that line is specifying the base directory of the namespace Krenor/Ldap, which is the src/. And starting from the base line, you should have the correct structure, which what I pointed out earlier.yangqi
@yangqi that's PSR-0, this is completely correct PSR-0 configuration.Wouter J

1 Answers

0
votes

(sorry my english is very bad...)

your autoload section (in composer.json) has wrong path

The line:

"Krenor\\Ldap\\": "src/"

Must Be

"Krenor\\Ldap\\": "packages/krenor/ldap-auth/src"

That's for composer.json inside of your package in develop. After, when you upload to packagist (or alternative) and you or anybody install them, the namespace will be point to "src", but you don't need write this... Composer will do automatically.