Jass
Jass

Reputation: 4105

Class not found when using custom class

I am using excel reader from https://github.com/nuovo/spreadsheet-reader and it is in app folder.

Now when I try to access it from HomeController.php using following code.

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;

use App\spreadsheet_reader\php_excel_reader\excel_reader2;
use App\spreadsheet_reader\SpreadsheetReader;

class HomeController extends Controller
{
    public function index()
   {
        require_once(base_path().'/app/spreadsheet_reader/php_excel_reader/excel_reader2.php');

       $Reader = new \App\spreadsheet_reader\SpreadsheetReader(base_path().'/UnRegisterClient.xlsx');
   }
}

Then it gives me following error.

Class 'App\spreadsheet_reader\SpreadsheetReader' not found

Any suggesstion how I can solve this? I mean how I can use my custom class?

Upvotes: 1

Views: 4551

Answers (2)

Saumini Navaratnam
Saumini Navaratnam

Reputation: 8850

I think the SpreadsheetReader is not defined in the namespace. You should call just new \SpreadsheetReader(...) or add use SpreadsheetReader and then call it new SpreadsheetReader()

Upvotes: 2

Amit Gupta
Amit Gupta

Reputation: 17668

Put your external files in the folder app/Libraries (first, create the Libraries folder) then just autoload the folder with that file.

For example, add this folder in the array or “classmap” in composer.json :

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

Then run composer dump-autoload in your command line.

Upvotes: 4

Related Questions