简体   繁体   中英

Laravel 5 Package, Class not found

I have a package folder in which I'm creating a class to help me with an API. But I'm getting the following error

Class 'Notflip\\Teamleader\\Company' not found

I'm trying to access a the Company class by using

$companies = new Teamleader\Company();

This is my folder structure

在此输入图像描述

This is my composer.json in my project root

    "psr-4": {
        "App\\": "app/",
        "Notflip\\Teamleader\\": "package/Notflip/Teamleader/src"
    }

This is my Company class

<?php namespace Notflip\Teamleader;

use Notflip\Teamleader;

class Company extends Teamleader {

    public function all()
    {
        return $this->api("getCompanies", ['amount' => 20,'pageno' => 0]);
    }

}

You're confusing yourself because you have Teamleader in the namespace definition, and then a Teamleader file and folder. Notflip\\Teamleader\\Company refers to a company file in the src folder.

I'd suggest you edit your composer file to this:

"psr-4": {
    "App\\": "app/",
    "Notflip\\": "package/Notflip/Teamleader/src"
}

Then rename Teamleader.php to Bootstrap.php or Main.php, or anything like that. For this example I'll rename it to Bootstrap.php. Then in your Company.php file you should have this:

<?php namespace Notflip\Teamleader;

use Notflip\Bootstrap;

class Company extends Bootstrap {

    public function all()
    {
        return $this->api("getCompanies", ['amount' => 20,'pageno' => 0]);
    }

}

Finally your call to company should look like this:

$companies = new \Notflip\Teamleader\Company; 

Standard practice with Laravel is to ignore the brackets at the end of a new class call if you're not passing in any arguments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM