doujian3401 2014-04-22 00: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.

  1. ReflectionException
  2. Class Acme\Transformers\LessonTransformer does not exist

I updated my composer.json file like following.

  1. "autoload": {
  2. "classmap": [
  3. "app/commands",
  4. "app/controllers",
  5. "app/models",
  6. "app/database/migrations",
  7. "app/database/seeds",
  8. "app/tests/TestCase.php",
  9. "app/Acme"
  10. ],
  11. "psr-0":{
  12. "Acme" : "app/Acme"
  13. }

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

  1. use Acme\Transformers\LessonTransformer;
  2. class LessonsController extends \BaseController {
  3. /**
  4. * @var Acme\Transformers\LessonTransformer
  5. */
  6. protected $lessonTransformer;
  7. function __construct(LessonTransformer $lessonTransformer){
  8. $this->lessonTransformer = $lessonTransformer;
  9. }
  10. /**
  11. * Display a listing of the resource.
  12. *
  13. * @return Response
  14. */
  15. public function index()
  16. {
  17. $lessons = Lesson::all();
  18. return Response::json([
  19. 'lessons' => $this->lessonTransformer->transformCollection($lessons->toArray())
  20. ], 200);
  21. }
  22. /**
  23. * Display the specified resource.
  24. *
  25. * @param int $id
  26. * @return Response
  27. */
  28. public function show($id)
  29. {
  30. $lesson = Lesson::find($id);
  31. if(!$lesson){
  32. return Response::json([
  33. 'error' => [
  34. 'mesage' => 'Lessons does not exits'
  35. ]
  36. ], 404);
  37. }
  38. return Response::json([
  39. 'lesson' => $this->lessonTransformer->transform($lesson)
  40. ], 200);
  41. }
  42. /**
  43. * Map over the lesson collection and cast it in an array,
  44. * so that we can map over it.
  45. *
  46. * Transform a collection of lesson
  47. *
  48. * @param array $lessons
  49. * @return array
  50. */
  51. public function transformCollection(array $lessons){
  52. return array_map([$this,'transform'], $lessons);
  53. }
  54. /**
  55. * Transform a single object(lesson)
  56. * Instead of returning everything, we return some selected data.
  57. * We replaced the keys that exposed the DB structure with some
  58. * other keys because if we wish to change the keys of data in
  59. * future then we can change it without affecting the DB structure.
  60. *
  61. * @param array $lesson
  62. * @return array
  63. */
  64. public function transform($lesson){
  65. return [
  66. 'title' => $lesson['title'],
  67. 'body' => $lesson['body'],
  68. 'active' => (boolean)$lesson['some_bool']
  69. ];
  70. }
  71. }

展开全部

  • 写回答

2条回答 默认 最新

  • douxing1353 2014-04-22 01: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

    1. <?php
    2. namespace Acme\Transformers;
    3. class Transformer
    4. {

    app/src/Acme/Transformers/LessonTransformer.php

    1. <?php
    2. namespace Acme\Transformers;
    3. class LessonTransformer extends Transformer
    4. {

    app/controllers/LessonsController.php

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部