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

为什么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
    {
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongmu1996 2014-06-15 03:40

    maybe you forget to update your composer.json: add app/Acme to classmap

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

    and run composer dump-autoloadon your terminal

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题