簡體   English   中英

在Laravel中的自定義類中調用時找不到類

[英]Class not found when called in custom class in laravel

我有一個名為

Helper.php,我把它放進了作曲家。

"autoload": {
        "files": [
            "app/Http/Helper.php",
            "app/Notification.php"
        ]
    },

您可以看到,我還放置了一個模型,即Notification模型。 但是當我在Helper.php中調用此Notification時,它說未找到Notification類。

function notify()
{
    $notify = new App\Notifcation;
    $notify = new Notifcation; //Also tried this

}

首先,您不需要在作曲家中添加它。

其次,檢查兩次編寫的內容,因為可能會有錯別字使程序無法執行

從composer.json中刪除"app/Notification.php"並轉儲自動加載。 然后像這樣使用。

function notify()
{
    $notify = new App\Notification;
}

如果將此通知模型添加到composer中,則即使不使用它也會始終自動加載,從而給項目帶來不必要的壓力。

希望這可以幫助

您在Notifcation有錯字。 嘗試這個:

function notify()
{
    $notify = new Notification;
}

在您的Helper.php中使用 Use App \\ Notification編寫比

$notify = new Notification();

或者您可以使用以下命令:

$notify = new \App\Notification;

以防萬一檢查您的名稱空間

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM