Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.2 Class not found but class is there with namespace

UPDATE 01/26/16 10:30pm EST: Through a lot of Google searching I discovered that I was misunderstanding how to utilize namespaces and custom classes. If anyone else is having this issue read this tutorial: http://www.techigniter.in/tutorials/how-to-add-custom-class-in-laravel-5/ It's short and very easy to understand. It helped resolve this issue and move me along to my next error... :D

ISSUE: Attempting to freshly install Laravel 5 and convert my Laravel 4 code to Laravel 5.

REQUEST: Please help me find the error and provide detailed instructions on how to correct it.

ERROR: FatalErrorException in additionalPCs.php line 4: Class 'App\Library\AdditionalPCs\additionalComputer' not found

Notes: I have put the additionalComputer.php file in both its own directory App\Libary\additionalPCs and directly into the App\Libary directory. Both places produce the same error. I am using namespaces. (possibly incorrectly)

Composer.json

"autoload": {
    "classmap": [
        "database"
    ],
    "psr-4": {
        "App\\": "app/"
    }
},

IndexController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Library\additionalPCs;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class IndexController extends Controller
{
    Protected $layout = 'master';
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        /** Wayne - 03-02-2014 - Moved for loop to a method within its own class. */
        $numberofpcs = new additionalPCs();
        $addtpcs=$numberofpcs->display();
        $this->layout->content = View::make('index')->with('addtpcs', $addtpcs)->with('businesstypelist', businesstype::dropdown())->with('contracttermlist',ContractTerm::dropdown());
    }
}

additionalPCs.php

<?php
namespace App\Library;

class additionalPCs extends additionalComputer {
    public function display() {
        return $this->displayMenu();    
    }
}

additionalComputer.php (I have also attempted use App\Library\additionalComputer;)

<?php
namespace App\Library;

use App\Library\AdditionalPCs\additionalComputer;

class additionalPCs extends additionalComputer {
    public function display() {
        return $this->displayMenu();    
    }
}
like image 991
scrfix Avatar asked Jan 27 '16 03:01

scrfix


Video Answer


2 Answers

Update after your post update:

You want to use additionalComputer, so you have to import his namespace, like so:

<?php
    namespace App\Library;

    use App\Library\additionalComputer;

    class additionalPCs extends additionalComputer {
        public function display() {
            return $this->displayMenu();    
        }
    }

(Added namespace import for additionalComputer)

Original post:

You have this line in your library:

namespace App\Library\AdditionalPCs;

To use AdditionalPCs (in for example your controller), change:

use App\Library\AdditionalPCs;

To

use App\Library\AdditionalPCs\AdditionalPCs;

The first AdditionalPCs is from your namespace, the second is your class name. Your class AdditionalPCs is in the sub namespace AdditionalPCs.

Important: it's new AdditionalPCs() (see the beginning A instead of a), it must be your class name exactly! That's a general rule!

Be care with your names (case sensitivity). It's better to use the code conventions from PSR-2: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md

like image 122
schellingerht Avatar answered Oct 27 '22 16:10

schellingerht


Namespace of each class is the container directory of the that class not the class file itself.

In your additionalPCs.php file for the namespace, get rid of \AdditionalPCs, it should just be:

namespace App\Library;
like image 27
Makan Avatar answered Oct 27 '22 17:10

Makan