Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel api route class to found error

I'm currently working on api jwt based authentication off a boiler plate on SitePoint. So far I've gotten everything working but I'm stuck on this point.

My Controller looks like this:

namespace App\Api\V1\Controllers;

use Illuminate\Http\Request;
use Dingo\Api\Routing\Helpers;

use Symfony\Component\HttpKernel\Exception\HttpException;
use JWTAuth;
use App\Http\Controllers\Controller;
use App\Order;
// use App\Api\V1\Requests\LoginRequest;
use Tymon\JWTAuth\Exceptions\JWTException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;

in the body I have this function:

  public function checkThis()
  {
    $currentUser = JWTAuth::parseToken()->authenticate();
    $orders = App\Order::first();
    echo $orders;
    function() {
    echo "stll here";
    };
  }

Under my api route I have this in a middleware:

$api->get('orderlist', 'App\\Api\\V1\\Controllers\\OrderController@checkThis');

When I run this in postman I get the following error: "message": "Class 'App\Api\V1\Controllers\App\Order' not found",

I've tried everything I can think of and it still keeps happening. When I take it out of the controller and run it directly in the routes it works. I'm a newbie to Laravel and PHP so I'm kinda of stuck.

like image 971
FabricioG Avatar asked Dec 04 '25 04:12

FabricioG


1 Answers

All thing below assusing that you want to invoke the App\Order::first();

In function checkThis , you can replace App\Order::first() by

Order::first() //aliasing version

or replace App\Order::first() by

\App\Order::first(); //fully qualified Name version

By php manual

Example #1 importing/aliasing with the use operator
<?php
namespace foo;
use My\Full\Classname as Another;

// this is the same as use My\Full\NSname as NSname   <-- very important 
use My\Full\NSname;

Notice that

// this is the same as use My\Full\NSname as NSname   <-- very important 
    use My\Full\NSname;

another php manual

and Inside a namespace, when PHP encounters an unqualified Name in a class name, function or constant context, it resolves these with different priorities. Class names always resolve to the current namespace name. Thus to access internal or non-namespaced user classes, one must refer to them with their fully qualified Name

php manual : fully qualified Name

Fully qualified name

This is an identifier with a namespace separator that begins with a namespace separator, such as \Foo\Bar. The namespace \Foo is also a fully qualified name.

So if you want to invoke the function App\Order::first ,just Order::first,for the reason that

use App\Order;     
equal 
use App\Order as Order ;

the aliasing is Order instead of App\Order . And the Fully qualified name is \App\Order instead of App\Order.

On the other hand , when you invoke App\Order::first(); it means that you are invoking

App\Api\V1\Controllers\App\Order::first();

So it can't not find the class;

Here is my demo

file1.php

<?php
namespace App;
class  Order{
public static function   first(){
echo "i am first";
  }
}

file2.php

<?php
namespace App\Api\V1\Controllers;
include 'file1.php';

use App\Order ;
\App\Order::first();
// and you can do it by 
//  Order::first();  they are equal in this place 

when i run command php file2.php it echo i am first

like image 181
Mattia Dinosaur Avatar answered Dec 05 '25 20:12

Mattia Dinosaur