Laravel 无法在控制器中使用自定义 App PHP 文件

Laravel cannot use custom App PHP files in controllers

在我的控制器中,我尝试使用位于 App 目录中的 PHP 文件。

我的控制器看起来像

    namespace App\Http\Controllers;

    use Illuminate\Http\Request;
    use App\MyClass\API;
    public function getservices(Request $request)
    {
        $sourcename = request('sName');
        $password = request('password');
        $siteID = request('siteID');

       // initialize default credentials
        $creds = new SourceCredentials($sourcename, $password, 
        array($siteID));

SourceCredentials Class 在 API.PHP 文件中,位于 App\MyClass\API,这是 API.PHP

namespace App\MyClass;

class SourceCredentials
{
    public $SourceName;
    public $Password;
    public $SiteIDs;
    //some other stuffs

当我使用我的路由向控制器发出 POST 请求时,出现此错误 -

Class 'App\Http\Controllers\SourceCredentials' not found

我是不是犯了一些错误?我已经为 API.PHP 正确命名了名称空间,并且在我的控制器中使用了该名称空间。请帮忙。

您的用途是:

use App\MyClass\API;

而您的 class 名字是:

SourceCredentials

像这样更正您的用法:

use App\MyClass\SourceCredentials;
// or if you want to youse the API name
use App\MyClass\SourceCredentials as API;

您忘记了 App\MyClass\SourceCredentials;

的用法
use App\MyClass\SourceCredentials;

您的控制器应如下所示:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\MyClass\API;
use App\MyClass\SourceCredentials;
public function getservices(Request $request)
{
    $sourcename = request('sName');
    $password = request('password');
    $siteID = request('siteID');

   // initialize default credentials
    $creds = new SourceCredentials($sourcename, $password, 
    array($siteID));