user3348771
user3348771

Reputation:

PHP Composer autoloader does not load files in include path

Assume there are two projects "project_a" and "project_b". I am setting the include path dynamically in index.php of project_a via set_include_path to be able to use the files of project_b lying in folder /Users/Me/develop/project_b/controller.

project_a's index.php content is:

set_include_path(get_include_path().':/Users/Me/develop/project_b');
require 'vendor/autoload.php';

$c = new projectbns\Controller\MyController();

composer.json content is:

{
    "require": {},
    "autoload": {
        "psr-4": {
            "projectbns\\Controller\\": "controller/"
        }
    },
    "config": {
        "use-include-path": true
    }
}

And finally the content of MyController.php in project_b is:

namespace projectbns\Controller;

class MyController {
    public function __construct() {
        die(var_dump('Hi from controller!'));
    }
}

But when I call project_a's index.php I only get this error:

Fatal error: Uncaught Error: Class 'projectbns\Controller\MyController' not found in /Users/Me/develop/project_a/index.php:8 Stack trace: #0 {main} thrown in /Users/David/Me/develop/project_a/index.php on line 8

What am I missing?

Thanks in advance, David.

P.S.: Yes I have to set the include path dynamically for specific reasons.

Upvotes: 2

Views: 3727

Answers (3)

user3348771
user3348771

Reputation:

I now solved my problem by setting up "an own composer" in project b (project_b gets its own composer.json, then in terminal: composer install). This has the effect that composer will generate a vendor/autoload.php file in project b which can be required in project a via absolute path:

require_once '/Users/Me/develop/project_b/vendor/autoload.php';

This way no include path modifications are needed and project b can handle autoloading its classes by itself (which seems a little bit more modular to me).

Upvotes: 1

Geee
Geee

Reputation: 2249

Here is the simple working demo. hope you like my stuff.

Your directory structure should be:

    - Main
        - index.php
        - composer.json
        - vendor
        - Libs
            - botstrap.php

index.php : Initial first landing file (in the root dir.)

<?php
    ini_set("display_errors", 1);
    error_reporting(E_ALL);

    // Include autoloading file
    require "vendor/autoload.php";

    // User target class file namespace
    use Library\Libs\Bootstrap as init;

    // Create object of the bootstrap file.
    $bootstrap = new init();

    /*
     * Object of the bootstrap file will call constructor by default.
     * if you want to call method then call with bellow code
     */

    $bootstrap->someFunc();

?>

Bootstrap.php

<?php
    namespace Library\Libs;

    class Bootstrap {
        function __construct() {
            echo "Class Construcctor called..<br/>";
        }
        public function someFunc()
        {
            echo "Function called..:)";
        }
    }
?>

composer.json

    {
    "name": "root/main",
    "autoload": {
        "psr-4": {
            "Library\\Libs\\": "./Libs",
        }
    }
}

Don't forget to dump autoload after all. hope this will be helpful to you.

Greetings!

Upvotes: 2

Michał G
Michał G

Reputation: 2292

ok , so try to change psr-4 to psr-0 and then

composer dumpautoload -o 

Upvotes: 1

Related Questions