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

时间:2017-10-12 14:54:33

标签: php laravel laravel-5 autoload

在我的控制器中,我正在尝试使用位于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类位于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'找不到

我在这里犯了一些错误吗?我已经为API.PHP正确命名了命名空间,我在控制器中使用了该命名空间。请帮忙。

2 个答案:

答案 0 :(得分:4)

您的使用是:

use App\MyClass\API;

,而您的班级名称是:

SourceCredentials

纠正你的用法:

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

答案 1 :(得分:0)

您忘记了使用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));