简体   繁体   中英

Uncaught Error "Class not found" in Laravel 5.8

I am trying to call my model file from another folder. I have provided both of these file structure.

I am getting this error:

Uncaught Error: Class 'App\\Models\\Providers' not found in /Applications/XAMPP/xamppfiles/htdocs/pro/app/Scripts/Providers/1/Scrape.php:17

I am calling the model file from a script folder located :

app/Scripts/Providers/1/Scrape.php

In this class I have the below :

namespace App\Scripts\Providers\1;
use App\Models;

Model file is located :

app/Models/Providers.php

Within this file I have the below:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

I have not shared the full content that I have in both of these files. If you would like to see the full content of these files please let me know.

This is how the Scrape.php looks like

<?php
namespace App\Scripts\Providers\1;
use App\Models\Providers;

class Scrape {
    public function __construct() {
        $test = new \App\Models\Providers();
        die(print_r($test, true));
    }
}

$obj = new Scrape();

You can't have a namespace that starts with a number.

Namespaces follow the same basic rules for variable names :

A valid variable name starts with a letter or underscore , followed by any number of letters, numbers, or underscores

(Emphasis mine).

Thus, your declaration

namespace App\Scripts\Providers\1

is basically invalid .

From that point forward, all bets are off.

First, change your namespace to a valid identifier (and I would advise choosing something more reasonable and recognizable than numbers , you can have descriptive names and there is simply no reason not to):

namespace App\Scripts\Providers\GroupWhatever

Logically, you'll have to rename the folder where this file resides. It used to be

app/Scripts/Providers/1/Scrape.php

so rename that directory to

app/Scripts/Providers/GroupWhatever/Scrape.php

(In both cases, replace GroupWhatever with something that makes sense for your application and domain).

From that point forward, if the class \\App\\Models\\Providers exists at app/Models/Providers.php , it should work.

Important:

Another problem that there could exist, is that is not very clear what Scripts/Scrape.php is or how is it called.

This should work if you are executing Scrape.php from within Laravel, by calling a Laravel controller or console application.

If you are calling this script directly (eg by executing php app/Scripts/Providers/1/Scrape.php (or the corrected app/Scripts/Providers/GroupWhatever/Scrape.php ) this simply won't work, since the autoloading logic is not run at all.

If you are executing your script manually, on top of the above changes you need to include composer autoload script, which is located at vendor/autoload.php .

Basically, add this line close to the top of your Scrape.php :

require_once dirname( __DIR__ ) . '/../../../vendor/autoload.php';

(I think I put the appropriate amount of go-up-one-dir path-segments, but you make sure it matches the correct path in your installation).

Once that is in place, the autoloader will be run, and classes will be found.

In your Scrape.php , change your namespace to:

<?php
namespace App\Scripts\Providers\p1;

From PHP manual comment ,

namespace (even nested or sub-namespace) cannot be just a number, it must start with a letter. For example, lets say you want to use namespace for versioning of your packages or versioning of your API:

namespace Mynamespace\\1; // Illegal

Instead use this: namespace

Mynamespace\\v1; // OK

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