简体   繁体   English

Controller中的Laravel ReflectionException

[英]Laravel ReflectionException in Controller

I'm trying to inject the class HotelsTransformer without success with the next code: 我正在尝试使用下一个代码注入HotelsTransformer类,但没有成功:

UserTransformer UserTransformer

<?php 
namespace App\Transformers;

class UserTransformer extends Transformer
{
   ...
}

HotelsTransformer HotelsTransformer

<?php 
namespace App\Transformers;

class HotelsTransformer extends Transformer
{
   ...
}

ApiHotelsController ApiHotelsController

<?php 

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use \App\Hotel;
use \App\Transformers\HotelsTransformer;

class ApiHotelsController extends ApiController
{
   protected $HotelsTransformer;

   public function __construct(HotelsTransformer $HotelsTransformer)
   {
       $this->HotelsTransformer = $HotelsTransformer;
       dd($this->HotelsTransformer);
   }

When I inject UserTransformer, it's all OK, but when I change UserTransformer with HotelsTransformer it throws me this error. 当我注入UserTransformer时,一切正常,但是当我用HotelsTransformer更改UserTransformer时,它会抛出这个错误。

在此输入图像描述

I don't know why is this happening, because I cloned UserTransformer and change its name but same error persists. 我不知道为什么会发生这种情况,因为我克隆了UserTransformer并更改了其名称,但同样的错误仍然存​​在。

Check your following namespace. 检查以下命名空间。 May be it does not exist or namespace path is not correct 可能是它不存在或命名空间路径不正确

use \App\Transformers\HotelsTransformer;

尝试运行composer dumpauto命令。

Try running these commands from your terminal (from the root directory of your project) 尝试从终端运行这些命令(从项目的根目录)

// use sudo if it asks for the root permission
composer update
composer dump-autoload
php artisan config:clear

Rerun the app/project and try again. 重新运行应用程序/项目,然后重试。

These 2 commands will refresh composer loaded classes and clear the cache to make the project run freshly, hope it helps.! 这两个命令将刷新作曲家加载的类并清除缓存以使项目新鲜运行,希望它有所帮助。

Ok, I have solved it, the filename was wrong, changed: app/Transformers/HotelsTranformer.php to: app/Transformers/HotelsTranformer.php 好的,我已经解决了,文件名错了,改了: app / Transformers / HotelsTranformer.phpapp / Transformers / HotelsTranformer.php

1 hour spent on that like a crazy, good job. 花上1个小时就像一个疯狂的好工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM