doujian3401 2014-04-22 08:32
浏览 65
已采纳

为什么laravel 4.1给出类不存在命名空类的错误?

I'm having an issue in laravel 4.1. I'm following a tutorial series. I have "Acme/Transformers" folder in "app" directory which has two class "Transformer.php" and "LessonTransformer.php". When i tried to access "LessonTransformer.php" in LessonController class of Controller directory i'm getting the following issue.

ReflectionException
Class Acme\Transformers\LessonTransformer does not exist

I updated my composer.json file like following.

"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/models",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php",
        "app/Acme"
    ],
    "psr-0":{
        "Acme" : "app/Acme"
    }

and ran

"composer dump-autoload -o"

but it made no change in the output. I have no ideas what's going on. Please help me.

Here's is my LessonsController.php class

use Acme\Transformers\LessonTransformer;

class LessonsController extends \BaseController {

    /**
     * @var Acme\Transformers\LessonTransformer
     */
     protected $lessonTransformer;

     function __construct(LessonTransformer $lessonTransformer){
         $this->lessonTransformer = $lessonTransformer;
     }

     /**
      * Display a listing of the resource.
      *
      * @return Response
      */
     public function index()
     {
         $lessons = Lesson::all();
         return Response::json([
             'lessons' => $this->lessonTransformer->transformCollection($lessons->toArray())
         ], 200);
     }

     /**
      * Display the specified resource.
      *
      * @param  int  $id
      * @return Response
      */
     public function show($id)
     {
          $lesson = Lesson::find($id);

          if(!$lesson){
               return Response::json([
                        'error' => [
                            'mesage' => 'Lessons does not exits'
                        ]
               ], 404);
          }

          return Response::json([
               'lesson' => $this->lessonTransformer->transform($lesson)
          ], 200);
      }

      /**
       * Map over the lesson collection and cast it in an array,
       * so that we can map over it.
       *
       * Transform a collection of lesson
       * 
       * @param  array $lessons
       * @return array
       */
      public function transformCollection(array $lessons){
          return array_map([$this,'transform'], $lessons);
      }

      /**
       * Transform a single object(lesson)
       * Instead of returning everything, we return some selected data.
       * We replaced the keys that exposed the DB structure with some
       * other keys because if we wish to change the keys of data in 
       * future then we can change it without affecting the DB structure.
       * 
       * @param  array $lesson
       * @return array
       */
      public function transform($lesson){
          return [
                'title'     => $lesson['title'],
                'body'      => $lesson['body'],
                'active'    => (boolean)$lesson['some_bool']
          ];
      }
 }
  • 写回答

2条回答 默认 最新

  • douxing1353 2014-04-22 09:30
    关注

    Have you added the

    namespace Acme\Transformers;
    

    line at the top of the LessonTransformer.php file? Also check for plurality of the filenames. Often mistakes come from a file named LessonTransformer.php and the class named LessonsFormatter (mind the s).

    EDIT

    These should be the top lines of your files:

    app/src/Acme/Transformers/Transformer.php

    <?php
    namespace Acme\Transformers;
    
    class Transformer
    {
    

    app/src/Acme/Transformers/LessonTransformer.php

    <?php
    namespace Acme\Transformers;
    
    class LessonTransformer extends Transformer
    {
    

    app/controllers/LessonsController.php

    <?php
    
    use Acme\Transformers\LessonTransformer;
    
    class LessonsController extends BaseController
    {
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思