1

In my controller I am trying to use a PHP file that is located within the App directory.

My controller looks like

    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));

The SourceCredentials Class is within the API.PHP file, located in App\MyClass\API, this is API.PHP

namespace App\MyClass;

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

When I make a POST request to the controller using my routes, I get this error -

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

Am I making some mistakes here? I have named the namespace correctly for the API.PHP and I am using that namespace in my controller. Please help.

8
  • Note: SourceCredentials is a class, not a function. Commented Oct 12, 2017 at 14:56
  • Yes, but my API.PHP file has more than one classes, and SourceCredentials is one of them Commented Oct 12, 2017 at 15:03
  • 1
    Break that file up and save each class in it's own file with the same name as the class (including the same casing). Then make sure you have the correct namespaces in your files and you will be able to load the classes you want with Laravels auto loader. Commented Oct 12, 2017 at 15:05
  • Thank you. I did that, it works but I have another problem. I cannot use the function related to that class. I am already extending the controller class, and I cannot extend another class to use that function Commented Oct 12, 2017 at 15:21
  • I don't understand what you mean. If you have a new (different) issue, please create a new question with all the information and examples. Commented Oct 12, 2017 at 15:25

2 Answers 2

4

Your use is :

use App\MyClass\API;

while your class name is :

SourceCredentials

Correct your use like so :

use App\MyClass\SourceCredentials;
// or if you want to youse the API name
use App\MyClass\SourceCredentials as API;
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. But my API.php file has multiple classes, like UserCredentials, API Service and so on. What is the best way to use all the classes?
Which psr are you following ? And I don't think it's a good idea having a lot of classes in a file ! Remember Single Responsibility is a good principle that will save you a lot of time later!
@tereško you should follow teeyo's advice, your code would likely benefit following a nice conventional approach since you have chosen to use Laravel which is very opinionated and its major benefit is it focuses on the convention for file and class names over explicit configuration.
0

You have forgotten the use of App\MyClass\SourceCredentials;

use App\MyClass\SourceCredentials;

Your controller should look like :

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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.