尝试获取数据并通过变换器返回时出现错误Class 'App\\Api\\V1\\Controllers\\Auth\\ApiController' not found
。使用ApiController进行扩展。我将APIcontroller放到App\\Api\\V1\\Controllers\\Front
文件夹中。
LoginController Code并将其扩展为ApiController:
<?php
namespace App\Api\V1\Controllers\Auth;
use Auth;
use Carbon\Carbon;
use Tymon\JWTAuth\JWTAuth;
use App\Http\Controllers\Controller;
use App\Api\V1\Requests\LoginRequest;
use Tymon\JWTAuth\Exceptions\JWTException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use App\User;
use App\UserDeviceData;
use Illuminate\Http\Request;
use App\Transformers\UserTransformer;
class LoginController extends ApiController // extends to API controller
{
public function login(LoginRequest $request, JWTAuth $JWTAuth)
{
$credentials = $request->only(['email', 'password']);
try {
$token = Auth::guard()->attempt($credentials);
if(!$token) {
return response()->json([
'message' => "Email and password do not match",
'status_code' => 403,
]);
}
$user = Auth::user();
$user->last_login = Carbon::now();
$user->save();
$user = Auth::user();
$user->UserDeviceData()->firstOrCreate([
'device_id' => $request->device_id
]);
} catch (JWTException $e) {
return response()->json([
'message' => "Internal server error",
'status_code' => 500,
]);
}
return $this->response->collection($user, new UserTransformer);
}
}
Api控制器代码:并设置namespace App\Api\V1\Controllers\Front;
<?php
namespace App\Api\V1\Controllers\Front;
use App\Support\Response;
use App\Support\Parameters;
abstract class ApiController extends Controller
{
protected $response;
protected $parameters;
public function __construct(Response $response, Parameters $parameters)
{
$this->response = $response;
$this->parameters = $parameters;
}
}
我的代码中有什么问题?
答案 0 :(得分:1)
由于$jsonPayload = [];
if (isset($obj['answer']) && (!empty($obj['answer']) || $obj['answer'] == 0)) {
$jsonPayload[$form_fields[$i]->id] = $obj;
}
return json_encode($jsonPayload);
和ApiController
使用的是不同的命名空间,您需要在LoginController
中use
ApiController
。
LoginController
您还忘记了use App\Api\V1\Controllers\Front\ApiController;
中的use
Controller
:
ApiController